Commit #7: Events thread-based + new easier Extractor.py + our own Timer class
[smartthings-infrastructure.git] / Extractor / ExtractorScript.py
1 import os
2 readyToReturn = 0
3 ToReturn = ""
4
5 def GetToken(f):
6         global readyToReturn
7         global ToReturn
8         Skip = ['\n', '\t', ' ']
9         Special = ["(", "\"", ":", ",", "{", "}", ")", "/", "*"]
10         S = ""
11         if (readyToReturn):
12                 readyToReturn = 0
13                 return ToReturn
14         ToReturn = ""
15         c = f.read(1)
16         while(True):
17                 if (c in Special):
18                         if (S != ""):
19                                 readyToReturn = 1
20                                 ToReturn = c
21                                 return S
22                         else:
23                                 return c
24                 elif (c in Skip):
25                         if (S != ""):
26                                 return S        
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 def ExtractMethods(F, inputMethodsFile, appName):
36         Temp = GetToken(F)
37         inputMethod = ""
38         while (Temp != "EOF"):
39                 #Extract the input methods
40                 if (Temp == "input"):
41                         inputMethod += Temp
42                         Temp = GetToken(F) #",(,input
43                         #input "","",linkedHashMap
44                         if (Temp == "\""):
45                                 while (Temp!="input" and Temp!="}"):
46                                         inputMethod += Temp
47                                         Temp = GetToken(F)
48                                 inputMethod += ",\""+appName+"\""
49                         #input ()
50                         elif (Temp == "("):
51                                 while (Temp!=")"):
52                                         inputMethod += Temp
53                                         Temp = GetToken(F)
54                                 inputMethod += ",\""+appName+"\")"
55                         #input linkedHashMap
56                         elif (Temp == "input"):
57                                 while (Temp!="input" and Temp!="}"):
58                                         inputMethod += Temp
59                                         Temp = GetToken(F)
60                                 inputMethod += ",\""+appName+"\""
61                 elif (Temp == "def"):
62                         Temp = GetToken(F)
63                         NameofFunc = Temp
64                         if (GetToken(F) != "="): #We have a function to create object for
65                                 if (appName == "App1"):
66                                         extractedFunctionsApp1.write("//Global Object for functions in subscribe method!\n")    
67                                         extractedFunctionsApp1.write("def %s = this.&" % NameofFunc)
68                                         extractedFunctionsApp1.write("%s\n" % NameofFunc)
69                                 else:
70                                         extractedFunctionsApp2.write("//Global Object for functions in subscribe method!\n")    
71                                         extractedFunctionsApp2.write("def %s = this.&" % NameofFunc)
72                                         extractedFunctionsApp2.write("%s\n" % NameofFunc)
73                 if (Temp!="input"):
74                         Temp=GetToken(F)
75                 if (inputMethod != ""):
76                         inputMethodsFile.write(inputMethod+"\n")
77                         inputMethod = ""
78                 
79
80
81 #Extract the methods from App1 and store in inputMethodsFile
82 F1 = open("Extractor/App1/App1.groovy", "r")
83 inputMethodsFile1 = open("Extractor/App1/inputMethodsFile1.groovy", "w+")
84 extractedFunctionsApp1 = open("Extractor/App1/extractedFunctionsApp1.groovy", "w+")
85 ExtractMethods(F1, inputMethodsFile1, "App1")
86 inputMethodsFile1.close()
87 F1.close()
88
89 #Extract the methods from App2 and store in inputMethodsFile
90 F2 = open("Extractor/App2/App2.groovy", "r")
91 inputMethodsFile2 = open("Extractor/App2/inputMethodsFile2.groovy", "w+")
92 extractedFunctionsApp2 = open("Extractor/App2/extractedFunctionsApp2.groovy", "w+")
93 ExtractMethods(F2, inputMethodsFile2, "App2")
94 inputMethodsFile2.close()
95 F2.close()
96
97
98 #Save the extracted methods and methods functions in a same file
99 extractorFile = open("Extractor/extractorFile.groovy", "w+")
100 inputMethodsFile1 = open("Extractor/App1/inputMethodsFile1.groovy", "r")
101 inputMethodsFile2 = open("Extractor/App2/inputMethodsFile2.groovy", "r")
102 inputMethods = open("Extractor/inputMethods.groovy", "r")
103 for line in inputMethods:
104         extractorFile.write(line)
105 extractorFile.write("\n\n")
106 for line in inputMethodsFile1:
107         extractorFile.write(line)
108 for line in inputMethodsFile2:
109         extractorFile.write(line)
110 extractorFile.close()
111 inputMethodsFile1.close()
112 inputMethodsFile2.close()
113 inputMethods.close()
114
115
116 #Run the file to extract the objects
117 os.system("groovy Extractor/extractorFile.groovy")