1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | import Rappture |
---|
5 | import random |
---|
6 | import getopt |
---|
7 | |
---|
8 | |
---|
9 | def defaultHandler(child): |
---|
10 | childchildList = None |
---|
11 | defaultList = child.children(type="default") |
---|
12 | if (defaultList != []): |
---|
13 | value = defaultList[0].get() |
---|
14 | if (value == ""): |
---|
15 | childchildList = child.children() |
---|
16 | else: |
---|
17 | child.put("current",str(value)) |
---|
18 | else : |
---|
19 | if (child.element(as="type") != "components"): |
---|
20 | childchildList = child.children() |
---|
21 | return childchildList |
---|
22 | |
---|
23 | def numberHandler(child): |
---|
24 | value = "" |
---|
25 | |
---|
26 | units = child.get("units") or "" |
---|
27 | min = child.get("min") or "" |
---|
28 | max = child.get("max") or "" |
---|
29 | |
---|
30 | if ((min != "")): |
---|
31 | if (units != ""): |
---|
32 | min = Rappture.Units.convert(min,units,"off") |
---|
33 | else: |
---|
34 | min = float(min) |
---|
35 | |
---|
36 | if (max != ""): |
---|
37 | if (units != ""): |
---|
38 | max = Rappture.Units.convert(max,units,"off") |
---|
39 | else: |
---|
40 | max = float(max) |
---|
41 | |
---|
42 | if ((min != "") and (max != "")): |
---|
43 | value = random.uniform(min,max) |
---|
44 | if (units != ""): |
---|
45 | value = Rappture.Units.convert(str(value),units,"on") |
---|
46 | child.put("current",str(value)) |
---|
47 | else: |
---|
48 | defaultHandler(child) |
---|
49 | |
---|
50 | def integerHandler(child): |
---|
51 | value = "" |
---|
52 | |
---|
53 | units = child.get("units") or "" |
---|
54 | min = child.get("min") or "" |
---|
55 | max = child.get("max") or "" |
---|
56 | |
---|
57 | if ((min != "")): |
---|
58 | if (units != ""): |
---|
59 | min = Rappture.Units.convert(min,units,"off") |
---|
60 | else: |
---|
61 | min = int(min) |
---|
62 | |
---|
63 | if (max != ""): |
---|
64 | if (units != ""): |
---|
65 | max = Rappture.Units.convert(max,units,"off") |
---|
66 | else: |
---|
67 | max = int(max) |
---|
68 | |
---|
69 | if ((min != "") and (max != "")): |
---|
70 | value = random.randint(min,max) |
---|
71 | #if (units != ""): |
---|
72 | # value = Rappture.Units.convert(value,units,"on") |
---|
73 | child.put("current",str(value)) |
---|
74 | else: |
---|
75 | defaultHandler(child) |
---|
76 | |
---|
77 | def booleanHandler(child): |
---|
78 | value = random.randint(0,1) |
---|
79 | if (value == 1): |
---|
80 | value = "true" |
---|
81 | else: |
---|
82 | value = "false" |
---|
83 | child.put("current",str(value)) |
---|
84 | |
---|
85 | def printHelp(): |
---|
86 | print """simsim [-t <path> | -d | -h] |
---|
87 | |
---|
88 | -t | --tool <path> - use the tool.xml file specified at <path> |
---|
89 | -d | --use-defaults - use default values instead of generating |
---|
90 | random values based on min/max values for |
---|
91 | number and integer elements. |
---|
92 | -h | --help - print this help menu |
---|
93 | |
---|
94 | """ |
---|
95 | sys.exit() |
---|
96 | |
---|
97 | |
---|
98 | tool = "tool.xml" |
---|
99 | defaults = False |
---|
100 | |
---|
101 | longOpts = ["tool=","use-defaults","help"] |
---|
102 | shortOpts = "t:dh" |
---|
103 | opts, args = getopt.getopt(sys.argv[1:], shortOpts, longOpts) |
---|
104 | |
---|
105 | for o, v in opts: |
---|
106 | if o in ("-t", "--tool"): |
---|
107 | tool = v |
---|
108 | elif o in ("-d", "--use-defaults"): |
---|
109 | defaults = True |
---|
110 | elif o in ("-h", "--help"): |
---|
111 | printHelp() |
---|
112 | |
---|
113 | lib = Rappture.library(tool) |
---|
114 | |
---|
115 | childList = lib.children("input") |
---|
116 | |
---|
117 | i = 0 |
---|
118 | while i < len(childList): |
---|
119 | child = childList[i] |
---|
120 | i = i + 1 |
---|
121 | type = child.element(as="type") |
---|
122 | if ((type == "number") and (not defaults)): |
---|
123 | numberHandler(child) |
---|
124 | elif ((type == "integer") and (not defaults)): |
---|
125 | integerHandler(child) |
---|
126 | elif ((type == "boolean") and (not defaults)): |
---|
127 | booleanHandler(child) |
---|
128 | else: |
---|
129 | addList = defaultHandler(child) |
---|
130 | if addList != None: |
---|
131 | childList = childList + addList |
---|
132 | |
---|
133 | Rappture.result(lib) |
---|
134 | |
---|