e925f5ff6cc089b7ea5a76cb735116cab9285c76
[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 #For both apps  
36 outGlobal = open("Extractor/outGlobal.groovy", "w+")
37 lockIsSet = 0
38 ContactIsSet = 0
39 SwitchIsSet = 0
40
41
42 def ExtractorFunc(F, outApp, outConstructorApp, Temp):
43         global outGlobal
44         global lockIsSet
45         global ContactIsSet
46         global SwitchIsSet
47
48         while (Temp != "EOF"):
49                 #Extract the global objects for input
50                 if (Temp == "input"):
51                         Object = ""
52                         Type = ""
53                         Temp = GetToken(F) #name or "name"
54                         #input name: "name", type: "type",...
55                         if (Temp == "name"):
56                                 Temp = GetToken(F) #"name"
57                                 Object = Temp
58                                 GetToken(F) #type
59                                 Temp = GetToken(F) #"type"
60                                 Type = Temp             
61                         #input "name", "type",...
62                         else:
63                                 Object = Temp
64                                 Temp = GetToken(F) #"type"
65                                 Type = Temp
66                         Temp = GetToken(F)
67                         Title = ""
68                         Required = ""
69                         Multiple = ""
70                         while (Temp != "input" and Temp != "}"):
71                                 if (Temp == "title"):
72                                         Temp = GetToken(F)
73                                         Title = Temp
74                                 elif (Temp == "required"):
75                                         Temp = GetToken(F)
76                                         Required = Temp
77                                 elif (Temp == "multiple"):
78                                         Temp = GetToken(F)
79                                         Multiple = Temp
80                                 Temp = GetToken(F)                      
81                         if (Type == "capability.lock"):
82                                 if (Title != ""):
83                                         print(Object+", "+Title)
84                                 if (Multiple != "" and Multiple == "true" and lockIsSet != 1):
85                                         lockIsSet = 1
86                                         g = raw_input("Enter the number of locks to control: (1, 2, or 3)\n") 
87                                         outGlobal.write("//Global Object for class lock!\n")
88                                         outGlobal.write("@Field def lockObject = new Locking(sendEvent, ")
89                                         outGlobal.write("%s)\n" % g)
90                                 elif ((Multiple == "" or Multiple == "false") and lockIsSet != 1):
91                                         lockIsSet = 1
92                                         outGlobal.write("//Global Object for class lock!\n")
93                                         outGlobal.write("@Field def lockObject = new Locking(sendEvent, 1)\n")
94                                 outApp.write("//Object for class lock!\n")
95                                 outApp.write("def %s\n" % Object)
96                                 outConstructorApp.write("%s = obj.lockObject\n" % Object)
97                         #elif (Type == "capability.alarm"):
98
99                         #elif (Type == "capability.battery"):
100
101                         #elif (Type == "capability.beacon"):
102
103                         #elif (Type == "capability.carbonMonoxideDetector"):
104
105                         #elif (Type == "capability.colorControl"):
106
107                         elif (Type == "capability.contactSensor"):
108                                 if (Title != ""):
109                                         print(Object+", "+Title)
110                                 if (Multiple != "" and Multiple == "true" and ContactIsSet != 1):
111                                         ContactIsSet = 1
112                                         g = raw_input("Enter the number of contact sensors to control: (1, 2, or 3)\n") 
113                                         outGlobal.write("//Global Object for class contactSensor!\n")
114                                         outGlobal.write("@Field def contactObject = new Contacting(sendEvent, ")
115                                         outGlobal.write("%s)\n" % g)
116                                 elif ((Multiple == "" or Multiple == "false") and ContactIsSet != 1):
117                                         ContactIsSet = 1
118                                         outGlobal.write("//Global Object for class contactSensor!\n")
119                                         outGlobal.write("@Field def contactObject = new Contacting(sendEvent, 1)\n")
120                                 outApp.write("//Object for class contactSensor!\n")
121                                 outApp.write("def %s\n" % Object)
122                                 outConstructorApp.write("%s = obj.contactObject\n" % Object)
123                         #elif (Type == "capability.doorControl"):
124
125                         #elif (Type == "capability.energyMeter"):
126
127                         #elif (Type == "capability.illuminanceMeasurement"):
128
129                         #elif (Type == "capability.accelerationSensor"):
130
131                         #elif (Type == "capability.motionSensor"):
132
133                         #elif (Type == "capability.musicPlayer"):
134
135                         #elif (Type == "capability.powerMeter"):
136
137                         #elif (Type == "capability.presenceSensor"):
138
139                         #elif (Type == "capability.relativeHumidityMeasurement"):
140
141                         #elif (Type == "capability.relaySwitch"):
142
143                         #elif (Type == "capability.sleepSensor"):
144
145                         #elif (Type == "capability.smokeDetector"):
146
147                         #elif (Type == "capability.stepSensor"):
148
149                         elif (Type == "capability.switch"):
150                                 if (Title != ""):
151                                         print(Object+", "+Title)
152                                 if (Multiple != "" and Multiple == "true" and SwitchIsSet != 1):
153                                         SwitchIsSet = 1
154                                         g = raw_input("Enter the number of switches to control: (1, 2, or 3)\n") 
155                                         outGlobal.write("//Global Object for class Switch!\n")
156                                         outGlobal.write("@Field def switchObject = new Switching(sendEvent, ")
157                                         outGlobal.write("%s)\n" % g)
158                                 elif ((Multiple == "" or Multiple == "false") and SwitchIsSet != 1):
159                                         SwitchIsSet = 1
160                                         outGlobal.write("//Global Object for class Switch!\n")
161                                         outGlobal.write("@Field def switchObject = new Switching(sendEvent, 1)\n")
162                                 outApp.write("//Object for class Switch!\n")
163                                 outApp.write("def %s\n" % Object)
164                                 outConstructorApp.write("%s = obj.switchObject\n" % Object)
165                         #elif (Type == "capability.switchLevel"):
166
167                         #elif (Type == "capability.temperatureMeasurement"):
168                 
169                         #elif (Type == "capability.thermostat"):
170                         
171                         #elif (Type == "capability.valve"):
172
173                         #elif (Type == "capability.waterSensor"):
174
175                         #elif (Type == "capability.touchSensor"):
176
177                         #elif (Type == "capability.imageCapture"):
178
179                         #elif (Type == "device.mobilePresence"):
180
181                         #elif (Type == "device.aeonKeyFob"):
182
183                         elif (Type == "mode"):
184                                 if (Title != ""):
185                                         print(Object+", "+Title)
186                                 g = raw_input("Enter the mode: ") 
187                                 outApp.write("//Global variable for mode!\n")
188                                 outApp.write("def %s = " % Object)
189                                 outApp.write("\"%s\"\n" % g)
190                         #elif (Type == "decimal"):
191
192                         #elif (Type == "text"):
193                         
194                         elif (Type == "number"):
195                                 if (Title != ""):
196                                         print(Object+", "+Title)
197                                 g = raw_input("Enter the number: ") 
198                                 outApp.write("//Global variable for number!\n")
199                                 outApp.write("def %s = " % Object)
200                                 outApp.write("%s\n" % g)
201                         #elif (Type == "time"):
202
203                         #elif (Type == "enum"):
204
205                         #elif (Type == "bool"):
206
207                         elif (Type == "phone"):
208                                 if (Title != ""):
209                                         print(Object+", "+Title)
210                                 g = raw_input("Enter the number to send notification to:\n") 
211                                 outApp.write("//Global variable for phone number!\n")
212                                 outApp.write("def %s = " % Object)
213                                 outApp.write("%s\n" % g)
214                         elif (Type == "contact"):
215                                 if (Title != ""):
216                                         print(Object+", "+Title)
217                                 g = raw_input("Enter the name of the recipients:\n") 
218                                 outApp.write("//Global variable for recipients!\n")
219                                 g = g.split()
220                                 outApp.write("def %s = " % Object)
221                                 outApp.write("%s\n" % g)
222                 #Extract the global object for functions
223                 elif (Temp == "def"):
224                         Temp = GetToken(F)
225                         NameofFunc = Temp
226                         if (GetToken(F) != "="): #We have a function to create object for
227                                 outApp.write("//Global Object for functions in subscribe method!\n")    
228                                 outApp.write("def %s = this.&" % NameofFunc)
229                                 outApp.write("%s\n" % NameofFunc)
230                 if (Temp != "input"):
231                         Temp = GetToken(F)
232         
233         F.close()
234         outApp.close()
235         outConstructorApp.close()
236
237 #For app1
238 F = open("Extractor/App1.groovy", "r")
239 outApp = open("Extractor/extractedObjectsApp1.groovy", "w+")
240 outConstructorApp = open("Extractor/extractedObjectsConstructorApp1.groovy", "w+")
241 Temp = GetToken(F)
242
243 ExtractorFunc(F, outApp, outConstructorApp, Temp)
244
245 #For app2
246 F = open("Extractor/App2.groovy", "r")
247 outApp = open("Extractor/extractedObjectsApp2.groovy", "w+")
248 outConstructorApp = open("Extractor/extractedObjectsConstructorApp2.groovy", "w+")
249 Temp = GetToken(F)
250
251 ExtractorFunc(F, outApp, outConstructorApp, Temp)
252
253 outGlobal.close()