Debian Codegraph @

src/imageclasses/fun2method_errortype_nd.py

raw symbols SCIP
1 # first 20161111 by [email protected]
2 # 20170216 Pieter Kempeneers: adapted for functions that do not modify the input image
3
4 def fun2method(inputfile, outputfile_basename, json_path):
5 """converts MIALib C function declarations into JIPLib C++ methods (outputfile_basename.cc file) and C++ method declarations (outputfile_basename.h file). Currently only convert desctuctive functions, i.e., ERROR_TYPE functions, with IMAGE * as first argument (IMAGE ** not yet taken into account).
6
7 :param inputfile: string for input file containing extern declarations
8 :param outputfile_basename: string for output file basename (i.e. without extension)
9 :returns: True on success, False otherwise
10 :rtype:
11
12 """
13
14 import re
15 import json
16 import sys
17 import os
18 with open(json_path, 'r') as f:
19 old2newDic = json.load(f)
20
21 #old2newDic = json.load(open("old2NewNames.json"))
22
23 ifp=open(inputfile, 'r')
24
25 f = open(outputfile_basename+'.cc', 'w')
26 fh = open(outputfile_basename+'.h', 'w')
27
28
29 lines=ifp.readlines()
30
31 for line in lines:
32 print(line)
33
34 name=re.match(r'extern ERROR_TYPE (.*)\((.*)\);',line)
35
36 args=re.split(',', name.group(2))
37 re.sub('\**', '', re.sub('.* ', '', args[0]))
38
39 arglist = []
40
41 for i in args:
42 atype=re.match(' *(.* \**)', i).group(1)
43 aname=re.sub('\**', '', re.sub('.* ', '', i))
44 arglist.append([atype, aname])
45
46 print(name.group(1))
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='CPLErr 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(iband);')
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(iband);')
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+separator+'int iband=0);\n'))
100
101 methodDeclaration+=separator+'int iband)'
102
103 f.write(methodDeclaration+'{')
104 f.write('\n\ttry{')
105 f.write('\n\t\tif(nrOfBand()<=iband){')
106 f.write('\n\t\t\tstd::string errorString=\"Error: band number exceeds number of bands in input image\";')
107 f.write('\n\t\t\tthrow(errorString);')
108 f.write('\n\t\t}')
109 for i in imRasterArray:
110 f.write('\n\t\tif('+i+'.nrOfBand()<=iband){')
111 f.write('\n\t\t\tstd::string errorString=\"Error: band number exceeds number of bands in input image\";')
112 f.write('\n\t\t\tthrow(errorString);')
113 f.write('\n\t\t}')
114 f.write('\n\t\t'+a.get("arguments")[0][0]+' '+a.get("arguments")[0][1]+' = 0;') # assigned later depending on destructive or not
115 llen=len(imDeclare)
116 for i in range(1,llen): # skip first argument since it depends on whether destructive or not
117 f.write('\n\t\t'+imDeclare[i])
118 for i in GTDeclare:
119 f.write('\n\t\t'+i)
120
121 if len(GTDeclare)>0:
122 f.write('\n\t\tswitch(getDataType()){')
123 for idx, GDType in enumerate(GDTypes):
124 f.write('\n\t\tcase('+GDType+'):')
125 for var_idx, GTVar in enumerate(GTVars):
126 f.write('\n\t\t\t'+GTVar+'.'+MIATypes[idx]+'val=static_cast<'+CTypes[idx]+'>(d_'+GTVars[var_idx]+');')
127 if (GDType=='GDT_Byte'):
128 f.write('\n\t\t\t'+GTVar+'.'+'generic_'+'val=static_cast<'+CTypes[idx]+'>(d_'+GTVars[var_idx]+');')
129 f.write('\n\t\t\tbreak;')
130 f.write('''
131 \t\tdefault:
132 \t\t\tstd::string errorString="Error: data type not supported";
133 \t\t\tthrow(errorString);
134 \t\t\tbreak;
135 \t\t}''')
136
137 f.write('\n\t\t'+a.get("arguments")[0][1]+'=this->getMIA(iband);')
138 f.write('\n\t\tif(::'+a.get("name")+'('+cCall+') == NO_ERROR){')
139 f.write('\n\t\t\tthis->setMIA(iband);')
140 for i in imRasterArray:
141 f.write('\n\t\t\t'+i+'.setMIA(iband);')
142 f.write('\n\t\t\treturn(CE_None);')
143 f.write('\n\t\t}')
144
145 f.write('\n\t\telse{')
146 f.write('\n\t\t\tthis->setMIA(iband);')
147 for i in imRasterArray:
148 f.write('\n\t\t\t'+i+'.setMIA(iband);')
149 f.write('\n\t\t\tstd::string errorString="Error: '+a.get("name")+'() function in MIA failed";')
150 f.write('\n\t\t\tthrow(errorString);')
151 f.write('\n\t\t}')
152
153 f.write('''
154 }
155 catch(std::string errorString){
156 \tstd::cerr << errorString << std::endl;
157 throw;
158 }
159 catch(...){
160 throw;
161 }
162 return(CE_None);
163 }\n''')
164
165 ifp.close()
166
167 f.close()
168 fh.close()
169
170 return(True)
171
172
173
174 ####################################################################
175
176 import sys, getopt
177
178 def main(argv):
179 inputfile="miallib_errortype_nd"
180 outputfile="fun2method_errortype_nd"
181 json_path = "old2NewNames.json"
182 try:
183 opts, args = getopt.getopt(argv,"hi:o:j:",["ifile=","ofile=", "json="])
184 except getopt.GetoptError:
185 print('fun2method.py -i <inputfile> -o <outputfilebasename> -j <json_path>')
186 sys.exit(2)
187 for opt, arg in opts:
188 if opt == '-h':
189 print('fun2method.py -i <inputfile> -o <outputfilebasename> -j <json_path>')
190 sys.exit()
191 elif opt in ("-i", "--ifile"):
192 inputfile = arg
193 elif opt in ("-o", "--ofile"):
194 outputfile = arg
195 elif opt in ("-j", "--json"):
196 json_path = arg
197 print('Input file is "', inputfile)
198 print('Output file is "', outputfile)
199 print('JSON file is "', json_path)
200
201 fun2method(inputfile, outputfile, json_path)
202
203 if __name__ == "__main__":
204 main(sys.argv[1:])