[25a608f5] | 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 | |
---|
[79ba1fc] | 59 | def readhelper(lines, key, key2, key3 , file): |
---|
[25a608f5] | 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 | find_fixed= True |
---|
| 71 | index = line.index(key) |
---|
| 72 | toks = line[index:].split("=",1 ) |
---|
| 73 | temp = toks[1].lstrip().rstrip() |
---|
[79ba1fc] | 74 | find_key2, find_key3=look_for_tag( string1=temp, |
---|
| 75 | begin=key2, end=key3 ) |
---|
[25a608f5] | 76 | if find_key2 and find_key3: |
---|
| 77 | temp1=[] |
---|
| 78 | temp2=[] |
---|
| 79 | temp3=[] |
---|
| 80 | temp4=[] |
---|
| 81 | temp1=split_text(separator=key2, string1=temp) |
---|
| 82 | temp2=split_list(separator=key3, mylist=temp1) |
---|
| 83 | temp3=split_list(separator=';', mylist=temp2) |
---|
| 84 | temp4=split_list(separator=',', mylist=temp3) |
---|
| 85 | listtofill= temp3 + temp4 |
---|
| 86 | return listtofill |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | elif find_key2 and not find_key3: |
---|
| 90 | ## [key]= key2(<text>) |
---|
| 91 | ## .... |
---|
| 92 | ## key3(<text>) |
---|
| 93 | temp1=[] |
---|
| 94 | temp2=[] |
---|
| 95 | temp3=[] |
---|
| 96 | temp4=[] |
---|
| 97 | ## remove key2= <text> |
---|
| 98 | temp1=split_text(separator=key2, string1=temp) |
---|
| 99 | |
---|
| 100 | ## split ";" first |
---|
| 101 | temp3=split_list(separator=';', mylist=temp1) |
---|
| 102 | temp4=split_list(separator=',', mylist=temp3) |
---|
| 103 | |
---|
[79ba1fc] | 104 | if len(temp3 + temp4)==0: |
---|
[25a608f5] | 105 | # [FIXED]= only one param |
---|
| 106 | listtofill+= temp1 |
---|
| 107 | listtofill += temp3+temp4 |
---|
| 108 | |
---|
| 109 | elif not find_key2 and not find_key3 : |
---|
| 110 | ## [key]= param done looking |
---|
| 111 | temp3=[] |
---|
| 112 | temp4=[] |
---|
| 113 | if look_for_tag( string1=temp,begin=";")[0]: |
---|
| 114 | ## split ";" first |
---|
| 115 | temp3=split_text(separator=';',string1=temp) |
---|
| 116 | temp4=split_list(separator=',', mylist=temp3) |
---|
| 117 | else: |
---|
| 118 | ## slip "," first |
---|
| 119 | temp3=split_text(separator=',',string1=temp) |
---|
| 120 | temp4=split_list(separator=';', mylist=temp3) |
---|
| 121 | if len(temp3+ temp4)==0: |
---|
| 122 | ## [FIXED]= only one param |
---|
[27a0771] | 123 | if temp.lstrip().rstrip()!="": |
---|
| 124 | listtofill= [temp.lstrip().rstrip()] |
---|
[25a608f5] | 125 | |
---|
| 126 | listtofill += temp3+temp4 |
---|
| 127 | return listtofill |
---|
| 128 | except: |
---|
| 129 | raise ValueError, "Could not parse file %s" % file |
---|
| 130 | |
---|
| 131 | elif find_fixed : |
---|
| 132 | if not find_key2: |
---|
| 133 | raise ValueError, "Could not parse file %s" % file |
---|
| 134 | if find_key3: |
---|
| 135 | temp1=[] |
---|
| 136 | temp2=[] |
---|
| 137 | temp3=[] |
---|
| 138 | temp4=[] |
---|
| 139 | temp5=[] |
---|
| 140 | |
---|
| 141 | temp1=split_text(separator=key3, string1=line) |
---|
| 142 | temp2=split_list(separator='//',mylist=temp1) |
---|
| 143 | temp5=split_list(separator="\*",mylist=temp1) |
---|
| 144 | |
---|
| 145 | if len(temp5)>0: |
---|
| 146 | temp3=split_list(separator=';',mylist=temp5) |
---|
| 147 | temp4=split_list(separator=',', mylist=temp5) |
---|
| 148 | elif len(temp2)>0: |
---|
| 149 | temp3=split_list(separator=';',mylist=temp2) |
---|
| 150 | temp4=split_list(separator=',', mylist=temp2) |
---|
| 151 | else: |
---|
| 152 | temp3=split_list(separator=';',mylist=temp1) |
---|
| 153 | temp4=split_list(separator=',', mylist=temp1) |
---|
| 154 | |
---|
[79ba1fc] | 155 | if len(temp3 + temp4)==0:# [FIXED]= only one param |
---|
| 156 | listtofill += temp1 |
---|
[25a608f5] | 157 | listtofill+=temp3+temp4 # |
---|
| 158 | break |
---|
| 159 | |
---|
| 160 | else: |
---|
[79ba1fc] | 161 | temp2=split_text(separator='//', string1=line) |
---|
| 162 | temp5=split_text(separator="\*", string1=line) |
---|
[25a608f5] | 163 | if len(temp5)>0: |
---|
[79ba1fc] | 164 | temp3=split_list(separator=';', mylist=temp5) |
---|
[25a608f5] | 165 | temp4=split_list(separator=',', mylist=temp5) |
---|
| 166 | elif len(temp2)>0: |
---|
[79ba1fc] | 167 | temp3=split_list(separator=';', mylist=temp2) |
---|
[25a608f5] | 168 | temp4=split_list(separator=',', mylist=temp2) |
---|
| 169 | else: |
---|
| 170 | if look_for_tag( string1=line,begin=";")[0]:# split ";" first |
---|
[79ba1fc] | 171 | temp3=split_text(separator=';', string1=line) |
---|
[25a608f5] | 172 | temp4=split_list(separator=',', mylist=temp3) |
---|
| 173 | else: |
---|
[79ba1fc] | 174 | temp3=split_text(separator=',', string1=line)# slip "," first |
---|
[25a608f5] | 175 | temp4=split_list(separator=';', mylist=temp3) |
---|
| 176 | if len(temp3+ temp4)==0:# [FIXED]= only one param |
---|
[27a0771] | 177 | if line.lstrip().rstrip()!="": |
---|
[79ba1fc] | 178 | listtofill=[line.lstrip().rstrip()] |
---|
[25a608f5] | 179 | listtofill+=temp3+temp4 # |
---|
| 180 | break |
---|
| 181 | return listtofill |
---|
| 182 | |
---|
| 183 | # main |
---|
| 184 | if __name__ == '__main__': |
---|
| 185 | |
---|
| 186 | # Read file |
---|
| 187 | name= "sphere.h" |
---|
[101065a] | 188 | f = open("..\include\core_shell.h",'r') |
---|
[25a608f5] | 189 | buf = f.read() |
---|
| 190 | |
---|
| 191 | lines = buf.split('\n') |
---|
| 192 | |
---|
| 193 | ## Catch Fixed parameters |
---|
| 194 | key = "[FIXED]" |
---|
| 195 | #open item in this case Fixed |
---|
| 196 | text='text' |
---|
| 197 | key2="<%s>"%text.lower() |
---|
| 198 | # close an item in this case fixed |
---|
| 199 | text='TexT' |
---|
| 200 | key3="</%s>"%text.lower() |
---|
| 201 | listto=[] |
---|
| 202 | |
---|
| 203 | listto= readhelper(lines, key, key2,key3, file=name) |
---|
| 204 | print "fixed parameters\n ",listto |
---|
| 205 | print |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | key0="[ORIENTATION_PARAMS]" |
---|
| 209 | |
---|
| 210 | listto=[] |
---|
| 211 | listto= readhelper(lines,key0, key2,key3, file=name) |
---|
| 212 | print "orientation parameters\n ",listto |
---|
| 213 | print |
---|
| 214 | f.close() |
---|
| 215 | # End of file |
---|