"First commit!"
[smartthings-infrastructure.git] / Extractor / ExtractorScript.py
1 readyToReturn = 0
2 ToReturn = ""
3
4 def GetToken(f):
5         global readyToReturn
6         global ToReturn
7         Skip = ["(", "\"", ":", ",", "{", "}", ")", '\n', '\t', ' ', "/"]
8         S = ""
9         if (readyToReturn):
10                 readyToReturn = 0
11                 return ToReturn
12         ToReturn = ""
13         c = f.read(1)
14         while(True):
15                 if (c in Skip):
16                         if (S != ""):
17                                 if (c == "{" or c == "}"):
18                                         readyToReturn = 1
19                                         ToReturn = c
20                                         return S
21                                 else:
22                                         return S
23                                         
24                         else:
25                                 if (c == "{" or c == "}"):
26                                         return c
27                                 else:
28                                         c = f.read(1)
29                                         continue
30                 S += c
31                 c = f.read(1)
32                 if not c:
33                         return "EOF"
34                 
35                 
36         
37                 
38
39 F = open("Extractor/App.groovy", "r")
40 Out = open("Extractor/ExtractedObjects.groovy", "w+")
41 Temp = GetToken(F)
42
43 Objects = []
44 Functions = []
45
46         
47 while (Temp != "EOF"):
48         #Extract the global objects for input
49         if (Temp == "input"):
50                 Object = ""
51                 Type = ""
52                 Temp = GetToken(F) #name or "name"
53                 #input name: "name", type: "type",...
54                 if (Temp == "name"):
55                         Temp = GetToken(F) #"name"
56                         Object = Temp
57                         GetToken(F) #type
58                         Temp = GetToken(F) #"type"
59                         Type = Temp             
60                 #input "name", "type",...
61                 else:
62                         Object = Temp
63                         Temp = GetToken(F) #"type"
64                         Type = Temp
65                 Temp = GetToken(F)
66                 Title = ""
67                 Required = ""
68                 Multiple = ""
69                 while (Temp != "input" and Temp != "}"):
70                         if (Temp == "title"):
71                                 Temp = GetToken(F)
72                                 Title = Temp
73                         elif (Temp == "required"):
74                                 Temp = GetToken(F)
75                                 Required = Temp
76                         elif (Temp == "multiple"):
77                                 Temp = GetToken(F)
78                                 Multiple = Temp
79                         Temp = GetToken(F)                      
80                 if (Type == "capability.lock"):
81                         if (Title != ""):
82                                 print(Object+", "+Title)
83                         if (Multiple != "" and Multiple == "true"):
84                                 g = raw_input("Enter the number of locks to control: (1, 2, or 3)\n") 
85                                 Out.write("//Global Object for class lock!\n")
86                                 Out.write("@Field def %s = new locking(" % Object)
87                                 Out.write("%s)\n" % g)
88                         elif (Multiple == "" or Multiple == "false"):
89                                 Out.write("//Global Object for class lock!\n")
90                                 Out.write("@Field def %s = new locking(1)\n" % Object)
91                 #elif (Type == "capability.alarm"):
92
93                 #elif (Type == "capability.battery"):
94
95                 #elif (Type == "capability.beacon"):
96
97                 #elif (Type == "capability.carbonMonoxideDetector"):
98
99                 #elif (Type == "capability.colorControl"):
100
101                 elif (Type == "capability.contactSensor"):
102                         if (Title != ""):
103                                 print(Object+", "+Title)
104                         if (Multiple != "" and Multiple == "true"):
105                                 g = raw_input("Enter the number of contactSensors to monitor: (1, 2, or 3)\n") 
106                                 Out.write("//Global Object for class contactSensor!\n")
107                                 Out.write("@Field def %s = new contacting(" % Object)
108                                 Out.write("%s)\n" % g)
109                         elif (Multiple == "" or Multiple == "false"):
110                                 Out.write("//Global Object for class contactSensor!\n")
111                                 Out.write("@Field def %s = new contacting(1)\n" % Object)
112                 #elif (Type == "capability.doorControl"):
113
114                 #elif (Type == "capability.energyMeter"):
115
116                 #elif (Type == "capability.illuminanceMeasurement"):
117
118                 #elif (Type == "capability.accelerationSensor"):
119
120                 #elif (Type == "capability.motionSensor"):
121
122                 #elif (Type == "capability.musicPlayer"):
123
124                 #elif (Type == "capability.powerMeter"):
125
126                 #elif (Type == "capability.presenceSensor"):
127
128                 #elif (Type == "capability.relativeHumidityMeasurement"):
129
130                 #elif (Type == "capability.relaySwitch"):
131
132                 #elif (Type == "capability.sleepSensor"):
133
134                 #elif (Type == "capability.smokeDetector"):
135
136                 #elif (Type == "capability.stepSensor"):
137
138                 elif (Type == "capability.switch"):
139                         if (Title != ""):
140                                 print(Object+", "+Title)
141                         if (Multiple != "" and Multiple == "true"):
142                                 g = raw_input("Enter the number of switches to control: (1, 2, or 3)\n") 
143                                 Out.write("//Global Object for class switch!\n")
144                                 Out.write("@Field def %s = new switching(" % Object)
145                                 Out.write("%s)\n" % g)
146                         elif (Multiple == "" or Multiple == "false"):
147                                 Out.write("//Global Object for class switch!\n")
148                                 Out.write("@Field def %s = new switching(1)\n" % Object)
149                 #elif (Type == "capability.switchLevel"):
150
151                 #elif (Type == "capability.temperatureMeasurement"):
152                 
153                 #elif (Type == "capability.thermostat"):
154                         
155                 #elif (Type == "capability.valve"):
156
157                 #elif (Type == "capability.waterSensor"):
158
159                 #elif (Type == "capability.touchSensor"):
160
161                 #elif (Type == "capability.imageCapture"):
162
163                 #elif (Type == "device.mobilePresence"):
164
165                 #elif (Type == "device.aeonKeyFob"):
166
167                 elif (Type == "mode"):
168                         if (Title != ""):
169                                 print(Object+", "+Title)
170                         g = raw_input("Enter the mode: ") 
171                         Out.write("//Global variable for mode!\n")
172                         Out.write("@Field def %s = " % Object)
173                         Out.write("\"%s\"\n" % g)
174                 #elif (Type == "decimal"):
175
176                 #elif (Type == "text"):
177                         
178                 elif (Type == "number"):
179                         if (Title != ""):
180                                 print(Object+", "+Title)
181                         g = raw_input("Enter the number: ") 
182                         Out.write("//Global variable for number!\n")
183                         Out.write("@Field def %s = " % Object)
184                         Out.write("%s\n" % g)
185                 #elif (Type == "time"):
186
187                 #elif (Type == "enum"):
188
189                 #elif (Type == "bool"):
190
191                 elif (Type == "phone"):
192                         if (Title != ""):
193                                 print(Object+", "+Title)
194                         g = raw_input("Enter the number to send notification to:\n") 
195                         Out.write("//Global variable for phone number!\n")
196                         Out.write("@Field def %s = " % Object)
197                         Out.write("%s\n" % g)
198                 elif (Type == "contact"):
199                         if (Title != ""):
200                                 print(Object+", "+Title)
201                         g = raw_input("Enter the name of the recipients:\n") 
202                         Out.write("//Global variable for recipients!\n")
203                         g = g.split()
204                         Out.write("@Field def %s = " % Object)
205                         Out.write("%s\n" % g)
206         #Extract the global object for functions
207         elif (Temp == "def"):
208                 Temp = GetToken(F)
209                 NameofFunc = Temp
210                 if (GetToken(F) != "="): #We have a function to create object for
211                         Out.write("//Global Object for functions in subscribe method!\n")       
212                         Out.write("@Field def %s = this.&" % NameofFunc)
213                         Out.write("%s\n" % NameofFunc)
214         if (Temp != "input"):
215                 Temp = GetToken(F)
216
217
218 F.close()
219 Out.close()