Debian Codegraph @

src/imageclasses/fun2method_errortype_nm.py

raw symbols SCIP
1 # first 20190808 by [email protected]
2
3 def fun2method(inputfile, outputfile_basename, json_path):
4 """converts MIALib C function declarations into JIPLib C++ methods (outputfile_basename.cc file) and C++ method declarations (outputfile_basename.h file). Convert desctuctive functions that do not support multi-band processing.
5
6 :param inputfile: string for input file containing extern declarations
7 :param outputfile_basename: string for output file basename (i.e. without extension)
8 :returns: True on success, False otherwise
9 :rtype:
10
11 """
12
13 import re
14 import json
15 import sys
16 import os
17 with open(json_path, 'r') as f:
18 old2newDic = json.load(f)
19
20 # for writing a dictionary in json file
21 #j son.dump(old2NewDict, open("text.txt",'w'))
22 # reading dictionary in json file
23 #old2newDic = json.load(open("old2NewNames.json"))
24
25 ifp=open(inputfile, 'r')
26
27 f = open(outputfile_basename+'.cc', 'w')
28 fh = open(outputfile_basename+'.h', 'w')
29
30
31 lines=ifp.readlines()
32
33 for line in lines:
34 print(line)
35
36 name=re.match(r'extern ERROR_TYPE (.*)\((.*)\);',line)
37 args=re.split(',', name.group(2))
38 re.sub('\**', '', re.sub('.* ', '', args[0]))
39
40 arglist = []
41
42 for i in args:
43 atype=re.match(' *(.* \**)', i).group(1)
44 aname=re.sub('\**', '', re.sub('.* ', '', i))
45 arglist.append([atype, aname])
46
47
48 a= { "name" : name.group(1),
49 "arguments" : arglist }
50
51 print(a)
52
53 imDeclare=[]
54 imRasterArray=[]
55
56 GTDeclare=[]
57 GTVars=[]
58
59 GDTypes = ['GDT_Byte', 'GDT_Int16', 'GDT_UInt16', 'GDT_Int32', 'GDT_UInt32', 'GDT_Float32', 'GDT_Float64']
60 MIATypes = ['uc_', 's_', 'us_', 'i32_', 'u32_', 'f_', 'd_']
61 CTypes = ['unsigned char', 'short int', 'unsigned short int', 'int', 'unsigned int', 'float', 'double']
62
63 methodDeclaration='std::shared_ptr<Jim> Jim::'+old2newDic.get(a.get("name"))+'('
64 print(methodDeclaration)
65
66 cSeparator=', '
67 separator = '' # default value in case there are no arguments besides the input image
68
69 if a.get("arguments")[0][0] != 'IMAGE *':
70 print('WARNING: ',line,' does not have IMAGE * as first argument')
71 continue
72 else:
73 imDeclare.append(a.get("arguments")[0][0]+' '+a.get("arguments")[0][1]+'=this->getMIA();')
74 cCall=a.get("arguments")[0][1]
75
76 imCount=1
77 for idx, arg in enumerate(a.get("arguments")[1:len(a.get("arguments"))]):
78 print(methodDeclaration)
79 print(idx)
80 print(idx)
81 print(separator)
82 print(arg)
83 print(arg[0])
84
85 if (arg[0]=='IMAGE *'):
86 methodDeclaration+=separator+'Jim& imRaster_'+arg[1]
87 imRasterArray.append('imRaster_'+arg[1])
88 imDeclare.append(arg[0]+' '+arg[1]+'=imRaster_'+arg[1]+'.getMIA();')
89 elif (arg[0]=='G_TYPE '): # G_TYPE as double in python
90 GTDeclare.append(arg[0]+' '+arg[1]+';')
91 GTVars.append(arg[1])
92 methodDeclaration+=separator+'double '+'d_'+arg[1]
93 else:
94 methodDeclaration+=separator+arg[0]+' '+arg[1]
95
96 cCall+=cSeparator+arg[1]
97 separator=', '
98
99 fh.write(re.sub(r'Jim::','',methodDeclaration+');\n'))
100
101 # methodDeclaration+=separator+'int iband'+cSeparator+'bool destructive)'
102 methodDeclaration+=')'
103
104 f.write(methodDeclaration+'{')
105
106 f.write('\n\t\ttry{')
107 f.write('\n\t\t//make a copy of this')
108 f.write('\n\t\tstd::shared_ptr<Jim> copyImg=this->clone();')
109
110 f.write('\n\t\t'+a.get("arguments")[0][0]+' '+a.get("arguments")[0][1]+' = 0;') # assigned later depending on destructive or not
111 llen=len(imDeclare)
112 for i in range(1,llen): # skip first argument since it depends on whether destructive or not
113 f.write('\n\t\t'+imDeclare[i])
114 for i in GTDeclare:
115 f.write('\n\t\t'+i)
116
117 if len(GTDeclare)>0:
118 f.write('\n\t\tswitch(getDataType()){')
119 for idx, GDType in enumerate(GDTypes):
120 f.write('\n\t\tcase('+GDType+'):')
121 for var_idx, GTVar in enumerate(GTVars):
122 f.write('\n\t\t\t'+GTVar+'.'+MIATypes[idx]+'val=static_cast<'+CTypes[idx]+'>(d_'+GTVars[var_idx]+');')
123 if (GDType=='GDT_Byte'):
124 f.write('\n\t\t\t'+GTVar+'.'+'generic_'+'val=static_cast<'+CTypes[idx]+'>(d_'+GTVars[var_idx]+');')
125 f.write('\n\t\t\tbreak;')
126 f.write('''
127 \t\tdefault:
128 \t\t\tstd::string errorString="Error: data type not supported";
129 \t\t\tthrow(errorString);
130 \t\t\tbreak;
131 \t\t}''')
132 f.write('\n\t\t'+a.get("arguments")[0][1]+'=copyImg->getMIA();')
133
134 f.write('\n\t\tif(::'+a.get("name")+'('+cCall+') == NO_ERROR){')
135 f.write('\n\t\t\tcopyImg->setMIA();')
136 for i in imRasterArray:
137 f.write('\n\t\t\t'+i+'.setMIA();')
138 f.write('\n\t\t}')
139
140 f.write('\n\t\telse{')
141 f.write('\n\t\t\tcopyImg->setMIA();')
142 for i in imRasterArray:
143 f.write('\n\t\t\t'+i+'.setMIA();')
144 f.write('\n\t\t\tstd::string errorString="Error: '+a.get("name")+'() function in MIA failed, returning NULL pointer";')
145 f.write('\n\t\t\tthrow(errorString);')
146 f.write('\n\t\t}')
147
148 f.write('\n\t\treturn(copyImg);')
149 f.write('\n\t}')
150
151 f.write('\n\tcatch(std::string errorString){')
152 f.write('\n\t\tstd::cerr << errorString << std::endl;')
153 f.write('\n\tthrow;')
154 f.write('\n\t}')
155 f.write('\n\tcatch(...){')
156 f.write('\n\t\tthrow;')
157 f.write('\n\t}')
158 f.write('\n}\n')
159 # f.write('''
160 # catch(std::string errorString){
161 # \tstd::cerr << errorString << std::endl;
162 # throw;
163 # }
164 # catch(...){
165 # throw;
166 # }
167 # }\n''')
168
169 ifp.close()
170
171 f.close()
172 fh.close()
173
174 return(True)
175
176
177
178 ####################################################################
179
180 import sys, getopt
181
182 def main(argv):
183 inputfile="miallib_errortype_nm"
184 outputfile="fun2method_errortype_nm"
185 json_path = "old2NewNames.json"
186 try:
187 opts, args = getopt.getopt(argv,"hi:o:j:",["ifile=","ofile=", "json="])
188 except getopt.GetoptError:
189 print('fun2method.py -i <inputfile> -o <outputfilebasename> -j <json_path>')
190 sys.exit(2)
191 for opt, arg in opts:
192 if opt == '-h':
193 print('fun2method.py -i <inputfile> -o <outputfilebasename> -j <json_path>')
194 sys.exit()
195 elif opt in ("-i", "--ifile"):
196 inputfile = arg
197 elif opt in ("-o", "--ofile"):
198 outputfile = arg
199 elif opt in ("-j", "--json"):
200 json_path = arg
201 print('Input file is "', inputfile)
202 print('Output file is "', outputfile)
203 print('JSON file is "', json_path)
204
205 fun2method(inputfile, outputfile, json_path)
206
207 if __name__ == "__main__":
208 main(sys.argv[1:])
209
210
211
212
213 # cat /home/soillpi/workstation/jip/mia//core/c/miallib_*.h | grep '^extern ERROR' > miallib_error_type_nm
214 # cat /home/soillpi/work/jip20170201/mia//core/c/miallib_*.h | grep '^extern ERROR' > miallib_error_type_nm
215 # python fun2method_errortype_nm.py -i miallib_error_type -o fun2method_errortype_nm
216 # to automatically insert content of fun2method in jim.h within placeholder //start insert from fun2method -> //end insert from fun2method
217 # sed -i -ne '/\/\/start insert from fun2method_errortype_nm/ {p; r fun2method_errortype_nm.h' -e ':a; n; /\/\/end insert from fun2method_errortype_nm/ {p; b}; ba}; p' jim.h