[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 | |
---|
| 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 | listtofill= [temp.lstrip().rstrip()] |
---|
| 128 | |
---|
| 129 | listtofill += temp3+temp4 |
---|
| 130 | return listtofill |
---|
| 131 | except: |
---|
| 132 | raise ValueError, "Could not parse file %s" % file |
---|
| 133 | |
---|
| 134 | elif find_fixed : |
---|
| 135 | if not find_key2: |
---|
| 136 | raise ValueError, "Could not parse file %s" % file |
---|
| 137 | #print "find_key3",find_key3,line,key3 |
---|
| 138 | if find_key3: |
---|
| 139 | #print "At the ned of the file----->" |
---|
| 140 | temp1=[] |
---|
| 141 | temp2=[] |
---|
| 142 | temp3=[] |
---|
| 143 | temp4=[] |
---|
| 144 | temp5=[] |
---|
| 145 | |
---|
| 146 | temp1=split_text(separator=key3, string1=line) |
---|
| 147 | temp2=split_list(separator='//',mylist=temp1) |
---|
| 148 | temp5=split_list(separator="\*",mylist=temp1) |
---|
| 149 | |
---|
| 150 | if len(temp5)>0: |
---|
| 151 | temp3=split_list(separator=';',mylist=temp5) |
---|
| 152 | temp4=split_list(separator=',', mylist=temp5) |
---|
| 153 | elif len(temp2)>0: |
---|
| 154 | temp3=split_list(separator=';',mylist=temp2) |
---|
| 155 | temp4=split_list(separator=',', mylist=temp2) |
---|
| 156 | else: |
---|
| 157 | temp3=split_list(separator=';',mylist=temp1) |
---|
| 158 | temp4=split_list(separator=',', mylist=temp1) |
---|
| 159 | |
---|
| 160 | if len(temp3+ temp4)==0:# [FIXED]= only one param |
---|
| 161 | listtofill+= temp1 |
---|
| 162 | listtofill+=temp3+temp4 # |
---|
| 163 | break |
---|
| 164 | |
---|
| 165 | else: |
---|
| 166 | temp2=split_text(separator='//',string1=line) |
---|
| 167 | temp5=split_text(separator="\*",string1=line) |
---|
| 168 | if len(temp5)>0: |
---|
| 169 | temp3=split_list(separator=';',mylist=temp5) |
---|
| 170 | temp4=split_list(separator=',', mylist=temp5) |
---|
| 171 | elif len(temp2)>0: |
---|
| 172 | temp3=split_list(separator=';',mylist=temp2) |
---|
| 173 | temp4=split_list(separator=',', mylist=temp2) |
---|
| 174 | else: |
---|
| 175 | if look_for_tag( string1=line,begin=";")[0]:# split ";" first |
---|
| 176 | temp3=split_text(separator=';',string1=line) |
---|
| 177 | temp4=split_list(separator=',', mylist=temp3) |
---|
| 178 | else: |
---|
| 179 | temp3=split_text(separator=',',string1=line)# slip "," first |
---|
| 180 | temp4=split_list(separator=';', mylist=temp3) |
---|
| 181 | if len(temp3+ temp4)==0:# [FIXED]= only one param |
---|
| 182 | listtofill= [line.lstrip().rstrip()] |
---|
| 183 | listtofill+=temp3+temp4 # |
---|
| 184 | break |
---|
| 185 | return listtofill |
---|
| 186 | |
---|
| 187 | # main |
---|
| 188 | if __name__ == '__main__': |
---|
| 189 | |
---|
| 190 | # Read file |
---|
| 191 | name= "sphere.h" |
---|
| 192 | f = open("..\c_extensions\core_shell.h",'r') |
---|
| 193 | buf = f.read() |
---|
| 194 | |
---|
| 195 | lines = buf.split('\n') |
---|
| 196 | |
---|
| 197 | ## Catch Fixed parameters |
---|
| 198 | key = "[FIXED]" |
---|
| 199 | #open item in this case Fixed |
---|
| 200 | text='text' |
---|
| 201 | key2="<%s>"%text.lower() |
---|
| 202 | # close an item in this case fixed |
---|
| 203 | text='TexT' |
---|
| 204 | key3="</%s>"%text.lower() |
---|
| 205 | listto=[] |
---|
| 206 | |
---|
| 207 | listto= readhelper(lines, key, key2,key3, file=name) |
---|
| 208 | print "fixed parameters\n ",listto |
---|
| 209 | print |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | key0="[ORIENTATION_PARAMS]" |
---|
| 213 | |
---|
| 214 | listto=[] |
---|
| 215 | listto= readhelper(lines,key0, key2,key3, file=name) |
---|
| 216 | print "orientation parameters\n ",listto |
---|
| 217 | print |
---|
| 218 | f.close() |
---|
| 219 | # End of file |
---|