1 | #!/usr/bin/env python |
---|
2 | """ WrapperGenerator class to generate model code automatically. |
---|
3 | """ |
---|
4 | |
---|
5 | import os, sys,re |
---|
6 | def split_list(separator, mylist, n=0): |
---|
7 | """ |
---|
8 | @return a list of string without white space of separator |
---|
9 | @param separator: the string to remove |
---|
10 | """ |
---|
11 | list=[] |
---|
12 | for item in mylist: |
---|
13 | if re.search( separator,item)!=None: |
---|
14 | if n >0: |
---|
15 | word =re.split(separator,item,int(n)) |
---|
16 | else: |
---|
17 | word =re.split( separator,item) |
---|
18 | for new_item in word: |
---|
19 | if new_item.lstrip().rstrip() !='': |
---|
20 | list.append(new_item.lstrip().rstrip()) |
---|
21 | return list |
---|
22 | def split_text(separator, string1, n=0): |
---|
23 | """ |
---|
24 | @return a list of string without white space of separator |
---|
25 | @param separator: the string to remove |
---|
26 | """ |
---|
27 | list=[] |
---|
28 | if re.search( separator,string1)!=None: |
---|
29 | if n >0: |
---|
30 | word =re.split(separator,string1,int(n)) |
---|
31 | else: |
---|
32 | word =re.split(separator,string1) |
---|
33 | for item in word: |
---|
34 | if item.lstrip().rstrip() !='': |
---|
35 | list.append(item.lstrip().rstrip()) |
---|
36 | return list |
---|
37 | def look_for_tag( string1,begin, end=None ): |
---|
38 | """ |
---|
39 | @note: this method remove the begin and end tags given by the user |
---|
40 | from the string . |
---|
41 | @param begin: the initial tag |
---|
42 | @param end: the final tag |
---|
43 | @param string: the string to check |
---|
44 | @return: begin_flag==True if begin was found, |
---|
45 | end_flag==if end was found else return false, false |
---|
46 | |
---|
47 | """ |
---|
48 | begin_flag= False |
---|
49 | end_flag= False |
---|
50 | if re.search( begin,string1)!=None: |
---|
51 | begin_flag= True |
---|
52 | if end !=None: |
---|
53 | if re.search(end,string1)!=None: |
---|
54 | end_flag= True |
---|
55 | return begin_flag, end_flag |
---|
56 | |
---|
57 | |
---|
58 | |
---|
59 | def readhelper(lines,key, key2,key3 , file): |
---|
60 | |
---|
61 | temp="" |
---|
62 | # flag to found key |
---|
63 | find_fixed= False |
---|
64 | find_key2=False |
---|
65 | find_key3=False |
---|
66 | listtofill=[] |
---|
67 | for line in lines: |
---|
68 | if line.count(key.lstrip().rstrip())>0 :#[FIXED]= ..... |
---|
69 | try: |
---|
70 | #print "found key", key |
---|
71 | find_fixed= True |
---|
72 | index = line.index(key) |
---|
73 | toks = line[index:].split("=",1 ) |
---|
74 | temp = toks[1].lstrip().rstrip() |
---|
75 | find_key2, find_key3=look_for_tag( string1=temp,begin=key2, end=key3 ) |
---|
76 | #print "looking for endpoints",find_key2, find_key3 |
---|
77 | ##[key]=<text>param </text> |
---|
78 | if find_key2 and find_key3: |
---|
79 | |
---|
80 | temp1=[] |
---|
81 | temp2=[] |
---|
82 | temp3=[] |
---|
83 | temp4=[] |
---|
84 | temp1=split_text(separator=key2, string1=temp) |
---|
85 | temp2=split_list(separator=key3, mylist=temp1) |
---|
86 | temp3=split_list(separator=';', mylist=temp2) |
---|
87 | temp4=split_list(separator=',', mylist=temp3) |
---|
88 | listtofill= temp3 + temp4 |
---|
89 | #print "endpoints found",listtofill |
---|
90 | return listtofill |
---|
91 | |
---|
92 | |
---|
93 | elif find_key2 and not find_key3: |
---|
94 | ## [key]= key2(<text>) |
---|
95 | ## .... |
---|
96 | ## key3(<text>) |
---|
97 | temp1=[] |
---|
98 | temp2=[] |
---|
99 | temp3=[] |
---|
100 | temp4=[] |
---|
101 | ## remove key2= <text> |
---|
102 | temp1=split_text(separator=key2, string1=temp) |
---|
103 | |
---|
104 | ## split ";" first |
---|
105 | temp3=split_list(separator=';', mylist=temp1) |
---|
106 | temp4=split_list(separator=',', mylist=temp3) |
---|
107 | |
---|
108 | if len(temp3+ temp4)==0: |
---|
109 | # [FIXED]= only one param |
---|
110 | listtofill+= temp1 |
---|
111 | listtofill += temp3+temp4 |
---|
112 | |
---|
113 | elif not find_key2 and not find_key3 : |
---|
114 | ## [key]= param done looking |
---|
115 | temp3=[] |
---|
116 | temp4=[] |
---|
117 | if look_for_tag( string1=temp,begin=";")[0]: |
---|
118 | ## split ";" first |
---|
119 | temp3=split_text(separator=';',string1=temp) |
---|
120 | temp4=split_list(separator=',', mylist=temp3) |
---|
121 | else: |
---|
122 | ## slip "," first |
---|
123 | temp3=split_text(separator=',',string1=temp) |
---|
124 | temp4=split_list(separator=';', mylist=temp3) |
---|
125 | if len(temp3+ temp4)==0: |
---|
126 | ## [FIXED]= only one param |
---|
127 | if temp.lstrip().rstrip()!="": |
---|
128 | listtofill= [temp.lstrip().rstrip()] |
---|
129 | |
---|
130 | listtofill += temp3+temp4 |
---|
131 | return listtofill |
---|
132 | except: |
---|
133 | raise ValueError, "Could not parse file %s" % file |
---|
134 | |
---|
135 | elif find_fixed : |
---|
136 | if not find_key2: |
---|
137 | raise ValueError, "Could not parse file %s" % file |
---|
138 | #print "find_key3",find_key3,line,key3 |
---|
139 | if find_key3: |
---|
140 | #print "At the ned of the file----->" |
---|
141 | temp1=[] |
---|
142 | temp2=[] |
---|
143 | temp3=[] |
---|
144 | temp4=[] |
---|
145 | temp5=[] |
---|
146 | |
---|
147 | temp1=split_text(separator=key3, string1=line) |
---|
148 | temp2=split_list(separator='//',mylist=temp1) |
---|
149 | temp5=split_list(separator="\*",mylist=temp1) |
---|
150 | |
---|
151 | if len(temp5)>0: |
---|
152 | temp3=split_list(separator=';',mylist=temp5) |
---|
153 | temp4=split_list(separator=',', mylist=temp5) |
---|
154 | elif len(temp2)>0: |
---|
155 | temp3=split_list(separator=';',mylist=temp2) |
---|
156 | temp4=split_list(separator=',', mylist=temp2) |
---|
157 | else: |
---|
158 | temp3=split_list(separator=';',mylist=temp1) |
---|
159 | temp4=split_list(separator=',', mylist=temp1) |
---|
160 | |
---|
161 | if len(temp3+ temp4)==0:# [FIXED]= only one param |
---|
162 | listtofill+= temp1 |
---|
163 | listtofill+=temp3+temp4 # |
---|
164 | break |
---|
165 | |
---|
166 | else: |
---|
167 | temp2=split_text(separator='//',string1=line) |
---|
168 | temp5=split_text(separator="\*",string1=line) |
---|
169 | if len(temp5)>0: |
---|
170 | temp3=split_list(separator=';',mylist=temp5) |
---|
171 | temp4=split_list(separator=',', mylist=temp5) |
---|
172 | elif len(temp2)>0: |
---|
173 | temp3=split_list(separator=';',mylist=temp2) |
---|
174 | temp4=split_list(separator=',', mylist=temp2) |
---|
175 | else: |
---|
176 | if look_for_tag( string1=line,begin=";")[0]:# split ";" first |
---|
177 | temp3=split_text(separator=';',string1=line) |
---|
178 | temp4=split_list(separator=',', mylist=temp3) |
---|
179 | else: |
---|
180 | temp3=split_text(separator=',',string1=line)# slip "," first |
---|
181 | temp4=split_list(separator=';', mylist=temp3) |
---|
182 | if len(temp3+ temp4)==0:# [FIXED]= only one param |
---|
183 | if line.lstrip().rstrip()!="": |
---|
184 | listtofill= [line.lstrip().rstrip()] |
---|
185 | listtofill+=temp3+temp4 # |
---|
186 | break |
---|
187 | return listtofill |
---|
188 | |
---|
189 | # main |
---|
190 | if __name__ == '__main__': |
---|
191 | |
---|
192 | # Read file |
---|
193 | name= "sphere.h" |
---|
194 | f = open("..\c_extensions\core_shell.h",'r') |
---|
195 | buf = f.read() |
---|
196 | |
---|
197 | lines = buf.split('\n') |
---|
198 | |
---|
199 | ## Catch Fixed parameters |
---|
200 | key = "[FIXED]" |
---|
201 | #open item in this case Fixed |
---|
202 | text='text' |
---|
203 | key2="<%s>"%text.lower() |
---|
204 | # close an item in this case fixed |
---|
205 | text='TexT' |
---|
206 | key3="</%s>"%text.lower() |
---|
207 | listto=[] |
---|
208 | |
---|
209 | listto= readhelper(lines, key, key2,key3, file=name) |
---|
210 | print "fixed parameters\n ",listto |
---|
211 | print |
---|
212 | |
---|
213 | |
---|
214 | key0="[ORIENTATION_PARAMS]" |
---|
215 | |
---|
216 | listto=[] |
---|
217 | listto= readhelper(lines,key0, key2,key3, file=name) |
---|
218 | print "orientation parameters\n ",listto |
---|
219 | print |
---|
220 | f.close() |
---|
221 | # End of file |
---|