Commit #7: Events thread-based + new easier Extractor.py + our own Timer class
[smartthings-infrastructure.git] / Extractor / ExtractorScript.py
index e925f5ff6cc089b7ea5a76cb735116cab9285c76..2d4493cee90f6bd2226fb2a4084121766bdc2341 100644 (file)
@@ -1,10 +1,12 @@
+import os
 readyToReturn = 0
 ToReturn = ""
 
 def GetToken(f):
        global readyToReturn
        global ToReturn
-       Skip = ["(", "\"", ":", ",", "{", "}", ")", '\n', '\t', ' ', "/"]
+       Skip = ['\n', '\t', ' ']
+       Special = ["(", "\"", ":", ",", "{", "}", ")", "/", "*"]
        S = ""
        if (readyToReturn):
                readyToReturn = 0
@@ -12,242 +14,104 @@ def GetToken(f):
        ToReturn = ""
        c = f.read(1)
        while(True):
-               if (c in Skip):
+               if (c in Special):
                        if (S != ""):
-                               if (c == "{" or c == "}"):
-                                       readyToReturn = 1
-                                       ToReturn = c
-                                       return S
-                               else:
-                                       return S
-                                       
+                               readyToReturn = 1
+                               ToReturn = c
+                               return S
                        else:
-                               if (c == "{" or c == "}"):
-                                       return c
-                               else:
-                                       c = f.read(1)
-                                       continue
+                               return c
+               elif (c in Skip):
+                       if (S != ""):
+                               return S        
+                       else:
+                               c = f.read(1)
+                               continue
                S += c
                c = f.read(1)
                if not c:
                        return "EOF"
-               
-#For both apps 
-outGlobal = open("Extractor/outGlobal.groovy", "w+")
-lockIsSet = 0
-ContactIsSet = 0
-SwitchIsSet = 0
-
-
-def ExtractorFunc(F, outApp, outConstructorApp, Temp):
-       global outGlobal
-       global lockIsSet
-       global ContactIsSet
-       global SwitchIsSet
 
+def ExtractMethods(F, inputMethodsFile, appName):
+       Temp = GetToken(F)
+       inputMethod = ""
        while (Temp != "EOF"):
-               #Extract the global objects for input
+               #Extract the input methods
                if (Temp == "input"):
-                       Object = ""
-                       Type = ""
-                       Temp = GetToken(F) #name or "name"
-                       #input name: "name", type: "type",...
-                       if (Temp == "name"):
-                               Temp = GetToken(F) #"name"
-                               Object = Temp
-                               GetToken(F) #type
-                               Temp = GetToken(F) #"type"
-                               Type = Temp             
-                       #input "name", "type",...
-                       else:
-                               Object = Temp
-                               Temp = GetToken(F) #"type"
-                               Type = Temp
-                       Temp = GetToken(F)
-                       Title = ""
-                       Required = ""
-                       Multiple = ""
-                       while (Temp != "input" and Temp != "}"):
-                               if (Temp == "title"):
+                       inputMethod += Temp
+                       Temp = GetToken(F) #",(,input
+                       #input "","",linkedHashMap
+                       if (Temp == "\""):
+                               while (Temp!="input" and Temp!="}"):
+                                       inputMethod += Temp
                                        Temp = GetToken(F)
-                                       Title = Temp
-                               elif (Temp == "required"):
+                               inputMethod += ",\""+appName+"\""
+                       #input ()
+                       elif (Temp == "("):
+                               while (Temp!=")"):
+                                       inputMethod += Temp
                                        Temp = GetToken(F)
-                                       Required = Temp
-                               elif (Temp == "multiple"):
+                               inputMethod += ",\""+appName+"\")"
+                       #input linkedHashMap
+                       elif (Temp == "input"):
+                               while (Temp!="input" and Temp!="}"):
+                                       inputMethod += Temp
                                        Temp = GetToken(F)
-                                       Multiple = Temp
-                               Temp = GetToken(F)                      
-                       if (Type == "capability.lock"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               if (Multiple != "" and Multiple == "true" and lockIsSet != 1):
-                                       lockIsSet = 1
-                                       g = raw_input("Enter the number of locks to control: (1, 2, or 3)\n") 
-                                       outGlobal.write("//Global Object for class lock!\n")
-                                       outGlobal.write("@Field def lockObject = new Locking(sendEvent, ")
-                                       outGlobal.write("%s)\n" % g)
-                               elif ((Multiple == "" or Multiple == "false") and lockIsSet != 1):
-                                       lockIsSet = 1
-                                       outGlobal.write("//Global Object for class lock!\n")
-                                       outGlobal.write("@Field def lockObject = new Locking(sendEvent, 1)\n")
-                               outApp.write("//Object for class lock!\n")
-                               outApp.write("def %s\n" % Object)
-                               outConstructorApp.write("%s = obj.lockObject\n" % Object)
-                       #elif (Type == "capability.alarm"):
-
-                       #elif (Type == "capability.battery"):
-
-                       #elif (Type == "capability.beacon"):
-
-                       #elif (Type == "capability.carbonMonoxideDetector"):
-
-                       #elif (Type == "capability.colorControl"):
-
-                       elif (Type == "capability.contactSensor"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               if (Multiple != "" and Multiple == "true" and ContactIsSet != 1):
-                                       ContactIsSet = 1
-                                       g = raw_input("Enter the number of contact sensors to control: (1, 2, or 3)\n") 
-                                       outGlobal.write("//Global Object for class contactSensor!\n")
-                                       outGlobal.write("@Field def contactObject = new Contacting(sendEvent, ")
-                                       outGlobal.write("%s)\n" % g)
-                               elif ((Multiple == "" or Multiple == "false") and ContactIsSet != 1):
-                                       ContactIsSet = 1
-                                       outGlobal.write("//Global Object for class contactSensor!\n")
-                                       outGlobal.write("@Field def contactObject = new Contacting(sendEvent, 1)\n")
-                               outApp.write("//Object for class contactSensor!\n")
-                               outApp.write("def %s\n" % Object)
-                               outConstructorApp.write("%s = obj.contactObject\n" % Object)
-                       #elif (Type == "capability.doorControl"):
-
-                       #elif (Type == "capability.energyMeter"):
-
-                       #elif (Type == "capability.illuminanceMeasurement"):
-
-                       #elif (Type == "capability.accelerationSensor"):
-
-                       #elif (Type == "capability.motionSensor"):
-
-                       #elif (Type == "capability.musicPlayer"):
-
-                       #elif (Type == "capability.powerMeter"):
-
-                       #elif (Type == "capability.presenceSensor"):
-
-                       #elif (Type == "capability.relativeHumidityMeasurement"):
-
-                       #elif (Type == "capability.relaySwitch"):
-
-                       #elif (Type == "capability.sleepSensor"):
-
-                       #elif (Type == "capability.smokeDetector"):
-
-                       #elif (Type == "capability.stepSensor"):
-
-                       elif (Type == "capability.switch"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               if (Multiple != "" and Multiple == "true" and SwitchIsSet != 1):
-                                       SwitchIsSet = 1
-                                       g = raw_input("Enter the number of switches to control: (1, 2, or 3)\n") 
-                                       outGlobal.write("//Global Object for class Switch!\n")
-                                       outGlobal.write("@Field def switchObject = new Switching(sendEvent, ")
-                                       outGlobal.write("%s)\n" % g)
-                               elif ((Multiple == "" or Multiple == "false") and SwitchIsSet != 1):
-                                       SwitchIsSet = 1
-                                       outGlobal.write("//Global Object for class Switch!\n")
-                                       outGlobal.write("@Field def switchObject = new Switching(sendEvent, 1)\n")
-                               outApp.write("//Object for class Switch!\n")
-                               outApp.write("def %s\n" % Object)
-                               outConstructorApp.write("%s = obj.switchObject\n" % Object)
-                       #elif (Type == "capability.switchLevel"):
-
-                       #elif (Type == "capability.temperatureMeasurement"):
-               
-                       #elif (Type == "capability.thermostat"):
-                       
-                       #elif (Type == "capability.valve"):
-
-                       #elif (Type == "capability.waterSensor"):
-
-                       #elif (Type == "capability.touchSensor"):
-
-                       #elif (Type == "capability.imageCapture"):
-
-                       #elif (Type == "device.mobilePresence"):
-
-                       #elif (Type == "device.aeonKeyFob"):
-
-                       elif (Type == "mode"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               g = raw_input("Enter the mode: ") 
-                               outApp.write("//Global variable for mode!\n")
-                               outApp.write("def %s = " % Object)
-                               outApp.write("\"%s\"\n" % g)
-                       #elif (Type == "decimal"):
-
-                       #elif (Type == "text"):
-                       
-                       elif (Type == "number"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               g = raw_input("Enter the number: ") 
-                               outApp.write("//Global variable for number!\n")
-                               outApp.write("def %s = " % Object)
-                               outApp.write("%s\n" % g)
-                       #elif (Type == "time"):
-
-                       #elif (Type == "enum"):
-
-                       #elif (Type == "bool"):
-
-                       elif (Type == "phone"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               g = raw_input("Enter the number to send notification to:\n") 
-                               outApp.write("//Global variable for phone number!\n")
-                               outApp.write("def %s = " % Object)
-                               outApp.write("%s\n" % g)
-                       elif (Type == "contact"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               g = raw_input("Enter the name of the recipients:\n") 
-                               outApp.write("//Global variable for recipients!\n")
-                               g = g.split()
-                               outApp.write("def %s = " % Object)
-                               outApp.write("%s\n" % g)
-               #Extract the global object for functions
+                               inputMethod += ",\""+appName+"\""
                elif (Temp == "def"):
                        Temp = GetToken(F)
                        NameofFunc = Temp
                        if (GetToken(F) != "="): #We have a function to create object for
-                               outApp.write("//Global Object for functions in subscribe method!\n")    
-                               outApp.write("def %s = this.&" % NameofFunc)
-                               outApp.write("%s\n" % NameofFunc)
-               if (Temp != "input"):
-                       Temp = GetToken(F)
-       
-       F.close()
-       outApp.close()
-       outConstructorApp.close()
-
-#For app1
-F = open("Extractor/App1.groovy", "r")
-outApp = open("Extractor/extractedObjectsApp1.groovy", "w+")
-outConstructorApp = open("Extractor/extractedObjectsConstructorApp1.groovy", "w+")
-Temp = GetToken(F)
-
-ExtractorFunc(F, outApp, outConstructorApp, Temp)
-
-#For app2
-F = open("Extractor/App2.groovy", "r")
-outApp = open("Extractor/extractedObjectsApp2.groovy", "w+")
-outConstructorApp = open("Extractor/extractedObjectsConstructorApp2.groovy", "w+")
-Temp = GetToken(F)
+                               if (appName == "App1"):
+                                       extractedFunctionsApp1.write("//Global Object for functions in subscribe method!\n")    
+                                       extractedFunctionsApp1.write("def %s = this.&" % NameofFunc)
+                                       extractedFunctionsApp1.write("%s\n" % NameofFunc)
+                               else:
+                                       extractedFunctionsApp2.write("//Global Object for functions in subscribe method!\n")    
+                                       extractedFunctionsApp2.write("def %s = this.&" % NameofFunc)
+                                       extractedFunctionsApp2.write("%s\n" % NameofFunc)
+               if (Temp!="input"):
+                       Temp=GetToken(F)
+               if (inputMethod != ""):
+                       inputMethodsFile.write(inputMethod+"\n")
+                       inputMethod = ""
+               
 
-ExtractorFunc(F, outApp, outConstructorApp, Temp)
 
-outGlobal.close()
+#Extract the methods from App1 and store in inputMethodsFile
+F1 = open("Extractor/App1/App1.groovy", "r")
+inputMethodsFile1 = open("Extractor/App1/inputMethodsFile1.groovy", "w+")
+extractedFunctionsApp1 = open("Extractor/App1/extractedFunctionsApp1.groovy", "w+")
+ExtractMethods(F1, inputMethodsFile1, "App1")
+inputMethodsFile1.close()
+F1.close()
+
+#Extract the methods from App2 and store in inputMethodsFile
+F2 = open("Extractor/App2/App2.groovy", "r")
+inputMethodsFile2 = open("Extractor/App2/inputMethodsFile2.groovy", "w+")
+extractedFunctionsApp2 = open("Extractor/App2/extractedFunctionsApp2.groovy", "w+")
+ExtractMethods(F2, inputMethodsFile2, "App2")
+inputMethodsFile2.close()
+F2.close()
+
+
+#Save the extracted methods and methods functions in a same file
+extractorFile = open("Extractor/extractorFile.groovy", "w+")
+inputMethodsFile1 = open("Extractor/App1/inputMethodsFile1.groovy", "r")
+inputMethodsFile2 = open("Extractor/App2/inputMethodsFile2.groovy", "r")
+inputMethods = open("Extractor/inputMethods.groovy", "r")
+for line in inputMethods:
+       extractorFile.write(line)
+extractorFile.write("\n\n")
+for line in inputMethodsFile1:
+       extractorFile.write(line)
+for line in inputMethodsFile2:
+       extractorFile.write(line)
+extractorFile.close()
+inputMethodsFile1.close()
+inputMethodsFile2.close()
+inputMethods.close()
+
+
+#Run the file to extract the objects
+os.system("groovy Extractor/extractorFile.groovy")