[af03ddd] | 1 | #!/usr/bin/env python |
---|
| 2 | """ WrapperGenerator class to generate model code automatically. |
---|
| 3 | """ |
---|
| 4 | |
---|
[95986b5] | 5 | import os, sys,re |
---|
[25a608f5] | 6 | import lineparser |
---|
[af03ddd] | 7 | |
---|
| 8 | class WrapperGenerator: |
---|
| 9 | """ Python wrapper generator for C models |
---|
| 10 | |
---|
| 11 | The developer must provide a header file describing |
---|
| 12 | the new model. |
---|
| 13 | |
---|
| 14 | To provide the name of the Python class to be |
---|
| 15 | generated, the .h file must contain the following |
---|
| 16 | string in the comments: |
---|
| 17 | |
---|
| 18 | // [PYTHONCLASS] = my_model |
---|
| 19 | |
---|
| 20 | where my_model must be replaced by the name of the |
---|
| 21 | class that you want to import from sans.models. |
---|
| 22 | (example: [PYTHONCLASS] = MyModel |
---|
| 23 | will create a class MyModel in sans.models.MyModel. |
---|
| 24 | It will also create a class CMyModel in |
---|
| 25 | sans_extension.c_models.) |
---|
| 26 | |
---|
| 27 | Also in comments, each parameter of the params |
---|
| 28 | dictionary must be declared with a default value |
---|
| 29 | in the following way: |
---|
| 30 | |
---|
| 31 | // [DEFAULT]=param_name=default_value |
---|
| 32 | |
---|
| 33 | (example: |
---|
| 34 | // [DEFAULT]=radius=20.0 |
---|
| 35 | ) |
---|
| 36 | |
---|
| 37 | See cylinder.h for an example. |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | A .c file corresponding to the .h file should also |
---|
| 41 | be provided (example: my_model.h, my_model.c). |
---|
| 42 | |
---|
| 43 | The .h file should define two function definitions. For example, |
---|
| 44 | cylinder.h defines the following: |
---|
| 45 | |
---|
[8389beb] | 46 | /// 1D scattering function |
---|
| 47 | double cylinder_analytical_1D(CylinderParameters *pars, double q); |
---|
[af03ddd] | 48 | |
---|
[8389beb] | 49 | /// 2D scattering function |
---|
| 50 | double cylinder_analytical_2D(CylinderParameters *pars, double q, double phi); |
---|
[af03ddd] | 51 | |
---|
| 52 | The .c file implements those functions. |
---|
| 53 | |
---|
| 54 | @author: Mathieu Doucet / UTK |
---|
| 55 | @contact: mathieu.doucet@nist.gov |
---|
| 56 | """ |
---|
| 57 | |
---|
[642a025] | 58 | def __init__(self, filename, output_dir='.', c_wrapper_dir='.'): |
---|
[af03ddd] | 59 | """ Initialization """ |
---|
| 60 | |
---|
| 61 | ## Name of .h file to generate wrapper from |
---|
| 62 | self.file = filename |
---|
| 63 | |
---|
| 64 | # Info read from file |
---|
| 65 | |
---|
| 66 | ## Name of python class to write |
---|
| 67 | self.pythonClass = None |
---|
| 68 | ## Parser in struct section |
---|
| 69 | self.inStruct = False |
---|
[a1d1b90] | 70 | self.foundCPP = False |
---|
[d62f422] | 71 | self.inParDefs = False |
---|
[af03ddd] | 72 | ## Name of struct for the c object |
---|
| 73 | self.structName = None |
---|
| 74 | ## Dictionary of parameters |
---|
| 75 | self.params = {} |
---|
| 76 | ## ModelCalculation module flag |
---|
| 77 | self.modelCalcFlag = False |
---|
| 78 | ## List of default parameters (text) |
---|
| 79 | self.default_list = "" |
---|
| 80 | ## Dictionary of units |
---|
| 81 | self.details = "" |
---|
| 82 | ## List of dispersed parameters |
---|
| 83 | self.disp_params = [] |
---|
[4e2f6ef8] | 84 | #model description |
---|
| 85 | self.description='' |
---|
[836fe6e] | 86 | # paramaters for fittable |
---|
| 87 | self.fixed= [] |
---|
[35aface] | 88 | # paramaters for non-fittable |
---|
| 89 | self.non_fittable= [] |
---|
[25a608f5] | 90 | ## parameters with orientation |
---|
| 91 | self.orientation_params =[] |
---|
[318b5bbb] | 92 | ## parameter with magnetism |
---|
| 93 | self.magentic_params = [] |
---|
[fa6db8b] | 94 | # Model category |
---|
| 95 | self.category = None |
---|
[5da3cc5] | 96 | # Whether model belongs to multifunc |
---|
| 97 | self.is_multifunc = False |
---|
[2d1b700] | 98 | ## output directory for wrappers |
---|
| 99 | self.output_dir = output_dir |
---|
[642a025] | 100 | self.c_wrapper_dir = c_wrapper_dir |
---|
[0a9686d] | 101 | |
---|
[25a608f5] | 102 | |
---|
[af03ddd] | 103 | |
---|
| 104 | def __repr__(self): |
---|
| 105 | """ Simple output for printing """ |
---|
| 106 | |
---|
[25a608f5] | 107 | rep = "\n Python class: %s\n\n" % self.pythonClass |
---|
| 108 | rep += " struc name: %s\n\n" % self.structName |
---|
| 109 | rep += " params: %s\n\n" % self.params |
---|
| 110 | rep += " description: %s\n\n" % self.description |
---|
| 111 | rep += " Fittable parameters: %s\n\n"% self.fixed |
---|
[35aface] | 112 | rep += " Non-Fittable parameters: %s\n\n"% self.non_fittable |
---|
[25a608f5] | 113 | rep += " Orientation parameters: %s\n\n"% self.orientation_params |
---|
[318b5bbb] | 114 | rep += " Magnetic parameters: %s\n\n"% self.magnetic_params |
---|
[af03ddd] | 115 | return rep |
---|
| 116 | |
---|
| 117 | def read(self): |
---|
| 118 | """ Reads in the .h file to catch parameters of the wrapper """ |
---|
| 119 | |
---|
| 120 | # Check if the file is there |
---|
| 121 | if not os.path.isfile(self.file): |
---|
| 122 | raise ValueError, "File %s is not a regular file" % self.file |
---|
| 123 | |
---|
| 124 | # Read file |
---|
| 125 | f = open(self.file,'r') |
---|
| 126 | buf = f.read() |
---|
| 127 | |
---|
| 128 | self.default_list = "List of default parameters:\n" |
---|
| 129 | #lines = string.split(buf,'\n') |
---|
| 130 | lines = buf.split('\n') |
---|
| 131 | self.details = "## Parameter details [units, min, max]\n" |
---|
| 132 | self.details += " self.details = {}\n" |
---|
[25a608f5] | 133 | |
---|
[836fe6e] | 134 | #open item in this case Fixed |
---|
| 135 | text='text' |
---|
| 136 | key2="<%s>"%text.lower() |
---|
| 137 | # close an item in this case fixed |
---|
[da3dae3] | 138 | text='TexT' |
---|
[836fe6e] | 139 | key3="</%s>"%text.lower() |
---|
| 140 | |
---|
[25a608f5] | 141 | ## Catch fixed parameters |
---|
| 142 | key = "[FIXED]" |
---|
| 143 | try: |
---|
[79ba1fc] | 144 | self.fixed= lineparser.readhelper(lines, key, |
---|
| 145 | key2, key3, file=self.file) |
---|
[25a608f5] | 146 | except: |
---|
| 147 | raise |
---|
[35aface] | 148 | ## Catch non-fittable parameters parameters |
---|
| 149 | key = "[NON_FITTABLE_PARAMS]" |
---|
| 150 | try: |
---|
[79ba1fc] | 151 | self.non_fittable= lineparser.readhelper(lines, key, key2, |
---|
| 152 | key3, file=self.file) |
---|
[35aface] | 153 | except: |
---|
| 154 | raise |
---|
| 155 | |
---|
[25a608f5] | 156 | ## Catch parameters with orientation |
---|
| 157 | key = "[ORIENTATION_PARAMS]" |
---|
| 158 | try: |
---|
[79ba1fc] | 159 | self.orientation_params = lineparser.readhelper(lines, key, |
---|
| 160 | key2, key3, file=self.file) |
---|
[25a608f5] | 161 | except: |
---|
| 162 | raise |
---|
[318b5bbb] | 163 | |
---|
| 164 | ## Catch parameters with orientation |
---|
| 165 | key = "[MAGNETIC_PARAMS]" |
---|
| 166 | try: |
---|
| 167 | self.magnetic_params = lineparser.readhelper( lines,key, |
---|
| 168 | key2,key3, file= self.file) |
---|
| 169 | except: |
---|
| 170 | raise |
---|
| 171 | |
---|
[25a608f5] | 172 | ## Catch Description |
---|
[96672c0] | 173 | key = "[DESCRIPTION]" |
---|
[25a608f5] | 174 | |
---|
| 175 | find_description = False |
---|
[96672c0] | 176 | temp="" |
---|
| 177 | for line in lines: |
---|
| 178 | if line.count(key)>0 : |
---|
[9316609] | 179 | |
---|
[96672c0] | 180 | try: |
---|
[25a608f5] | 181 | find_description= True |
---|
[96672c0] | 182 | index = line.index(key) |
---|
| 183 | toks = line[index:].split("=",1 ) |
---|
| 184 | temp=toks[1].lstrip().rstrip() |
---|
| 185 | text='text' |
---|
[9316609] | 186 | key2="<%s>"%text.lower() |
---|
[96672c0] | 187 | if re.match(key2,temp)!=None: |
---|
[25a608f5] | 188 | |
---|
[9316609] | 189 | toks2=temp.split(key2,1) |
---|
| 190 | self.description=toks2[1] |
---|
| 191 | text='text' |
---|
| 192 | key2="</%s>"%text.lower() |
---|
| 193 | if re.search(key2,toks2[1])!=None: |
---|
| 194 | temp=toks2[1].split(key2,1) |
---|
| 195 | self.description=temp[0] |
---|
| 196 | break |
---|
[25a608f5] | 197 | |
---|
[96672c0] | 198 | else: |
---|
| 199 | self.description=temp |
---|
[9316609] | 200 | break |
---|
[96672c0] | 201 | except: |
---|
| 202 | raise ValueError, "Could not parse file %s" % self.file |
---|
[25a608f5] | 203 | elif find_description: |
---|
[9316609] | 204 | text='text' |
---|
| 205 | key2="</%s>"%text.lower() |
---|
| 206 | if re.search(key2,line)!=None: |
---|
| 207 | tok=line.split(key2,1) |
---|
| 208 | temp=tok[0].split("//",1) |
---|
| 209 | self.description+=tok[1].lstrip().rstrip() |
---|
| 210 | break |
---|
| 211 | else: |
---|
| 212 | if re.search("//",line)!=None: |
---|
| 213 | temp=line.split("//",1) |
---|
| 214 | self.description+='\n\t\t'+temp[1].lstrip().rstrip() |
---|
| 215 | |
---|
| 216 | else: |
---|
| 217 | self.description+='\n\t\t'+line.lstrip().rstrip() |
---|
| 218 | |
---|
| 219 | |
---|
[0a9686d] | 220 | |
---|
[af03ddd] | 221 | for line in lines: |
---|
| 222 | |
---|
| 223 | # Catch class name |
---|
| 224 | key = "[PYTHONCLASS]" |
---|
| 225 | if line.count(key)>0: |
---|
| 226 | try: |
---|
| 227 | index = line.index(key) |
---|
| 228 | toks = line[index:].split("=" ) |
---|
| 229 | self.pythonClass = toks[1].lstrip().rstrip() |
---|
[0a9686d] | 230 | |
---|
[af03ddd] | 231 | except: |
---|
| 232 | raise ValueError, "Could not parse file %s" % self.file |
---|
| 233 | |
---|
[fa6db8b] | 234 | key = "[CATEGORY]" |
---|
| 235 | if line.count(key)>0: |
---|
| 236 | try: |
---|
| 237 | index = line.index(key) |
---|
| 238 | toks = line[index:].split("=") |
---|
| 239 | self.category = toks[1].lstrip().rstrip() |
---|
| 240 | |
---|
| 241 | except: |
---|
| 242 | raise ValueError, "Could not parse file %s" % self.file |
---|
| 243 | |
---|
[5da3cc5] | 244 | # is_multifunc |
---|
| 245 | key = "[MULTIPLICITY_INFO]" |
---|
| 246 | if line.count(key) > 0: |
---|
| 247 | self.is_multifunc = True |
---|
| 248 | try: |
---|
| 249 | index = line.index(key) |
---|
| 250 | toks = line[index:].split("=") |
---|
| 251 | self.multiplicity_info = toks[1].lstrip().rstrip() |
---|
| 252 | except: |
---|
| 253 | raise ValueError, "Could not parse file %s" % self.file |
---|
[fa6db8b] | 254 | |
---|
[af03ddd] | 255 | # Catch struct name |
---|
[a1d1b90] | 256 | # C++ class definition |
---|
| 257 | if line.count("class")>0: |
---|
| 258 | # We are entering a class definition |
---|
[d62f422] | 259 | self.inParDefs = True |
---|
[a1d1b90] | 260 | self.foundCPP = True |
---|
| 261 | |
---|
| 262 | # Old-Style C struct definition |
---|
[af03ddd] | 263 | if line.count("typedef struct")>0: |
---|
| 264 | # We are entering a struct block |
---|
[d62f422] | 265 | self.inParDefs = True |
---|
[af03ddd] | 266 | self.inStruct = True |
---|
| 267 | |
---|
[d62f422] | 268 | if self.inParDefs and line.count("}")>0: |
---|
[af03ddd] | 269 | # We are exiting a struct block |
---|
[d62f422] | 270 | self.inParDefs = False |
---|
[af03ddd] | 271 | |
---|
[d62f422] | 272 | if self.inStruct: |
---|
| 273 | self.inStruct = False |
---|
| 274 | # Catch the name of the struct |
---|
| 275 | index = line.index("}") |
---|
| 276 | toks = line[index+1:].split(";") |
---|
| 277 | # Catch pointer definition |
---|
| 278 | toks2 = toks[0].split(',') |
---|
| 279 | self.structName = toks2[0].lstrip().rstrip() |
---|
[96672c0] | 280 | |
---|
[af03ddd] | 281 | # Catch struct content |
---|
| 282 | key = "[DEFAULT]" |
---|
[d62f422] | 283 | if self.inParDefs and line.count(key)>0: |
---|
[af03ddd] | 284 | # Found a new parameter |
---|
| 285 | try: |
---|
| 286 | index = line.index(key) |
---|
| 287 | toks = line[index:].split("=") |
---|
| 288 | toks2 = toks[2].split() |
---|
| 289 | val = float(toks2[0]) |
---|
| 290 | self.params[toks[1]] = val |
---|
| 291 | #self.pythonClass = toks[1].lstrip().rstrip() |
---|
| 292 | units = "" |
---|
| 293 | if len(toks2) >= 2: |
---|
| 294 | units = toks2[1] |
---|
| 295 | self.default_list += " %-15s = %s %s\n" % \ |
---|
| 296 | (toks[1], val, units) |
---|
| 297 | |
---|
| 298 | # Check for min and max |
---|
| 299 | min = "None" |
---|
| 300 | max = "None" |
---|
| 301 | if len(toks2) == 4: |
---|
| 302 | min = toks2[2] |
---|
| 303 | max = toks2[3] |
---|
| 304 | |
---|
| 305 | self.details += " self.details['%s'] = ['%s', %s, %s]\n" % \ |
---|
| 306 | (toks[1].lstrip().rstrip(), units.lstrip().rstrip(), min, max) |
---|
| 307 | except: |
---|
| 308 | raise ValueError, "Could not parse input file %s \n %s" % \ |
---|
| 309 | (self.file, sys.exc_value) |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | # Catch need for numerical calculations |
---|
| 313 | key = "CalcParameters calcPars" |
---|
| 314 | if line.count(key)>0: |
---|
| 315 | self.modelCalcFlag = True |
---|
| 316 | |
---|
| 317 | # Catch list of dispersed parameters |
---|
| 318 | key = "[DISP_PARAMS]" |
---|
| 319 | if line.count(key)>0: |
---|
| 320 | try: |
---|
| 321 | index = line.index(key) |
---|
| 322 | toks = line[index:].split("=") |
---|
| 323 | list_str = toks[1].lstrip().rstrip() |
---|
| 324 | self.disp_params = list_str.split(',') |
---|
| 325 | except: |
---|
| 326 | raise ValueError, "Could not parse file %s" % self.file |
---|
[0a9686d] | 327 | |
---|
[af03ddd] | 328 | def write_c_wrapper(self): |
---|
| 329 | """ Writes the C file to create the python extension class |
---|
| 330 | The file is written in C[PYTHONCLASS].c |
---|
| 331 | """ |
---|
[8389beb] | 332 | file_path = os.path.join(self.c_wrapper_dir, |
---|
| 333 | "C"+self.pythonClass+'.cpp') |
---|
[2d1b700] | 334 | file = open(file_path, 'w') |
---|
[af03ddd] | 335 | |
---|
[8389beb] | 336 | template = open(os.path.join(os.path.dirname(__file__), |
---|
| 337 | "classTemplate.txt"), 'r') |
---|
[af03ddd] | 338 | |
---|
| 339 | tmp_buf = template.read() |
---|
| 340 | #tmp_lines = string.split(tmp_buf,'\n') |
---|
| 341 | tmp_lines = tmp_buf.split('\n') |
---|
| 342 | |
---|
| 343 | for tmp_line in tmp_lines: |
---|
| 344 | |
---|
| 345 | # Catch class name |
---|
| 346 | newline = self.replaceToken(tmp_line, |
---|
| 347 | "[PYTHONCLASS]", 'C'+self.pythonClass) |
---|
[9316609] | 348 | #Catch model description |
---|
[95986b5] | 349 | #newline = self.replaceToken(tmp_line, |
---|
| 350 | # "[DESCRIPTION]", self.description) |
---|
[af03ddd] | 351 | # Catch C model name |
---|
| 352 | newline = self.replaceToken(newline, |
---|
| 353 | "[CMODEL]", self.pythonClass) |
---|
| 354 | |
---|
| 355 | # Catch class name |
---|
| 356 | newline = self.replaceToken(newline, |
---|
| 357 | "[MODELSTRUCT]", self.structName) |
---|
[5da3cc5] | 358 | |
---|
| 359 | # Sort model initialization based on multifunc |
---|
| 360 | if(self.is_multifunc): |
---|
| 361 | line = "int level = 1;\nPyArg_ParseTuple(args,\"i\",&level);\n" |
---|
| 362 | line += "self->model = new " + self.pythonClass + "(level);" |
---|
| 363 | else: |
---|
| 364 | line = "self->model = new " + self.pythonClass + "();" |
---|
| 365 | |
---|
| 366 | newline = self.replaceToken(newline,"[INITIALIZE_MODEL]", |
---|
| 367 | line) |
---|
[af03ddd] | 368 | |
---|
| 369 | # Dictionary initialization |
---|
| 370 | param_str = "// Initialize parameter dictionary\n" |
---|
| 371 | for par in self.params: |
---|
[35aface] | 372 | param_str += " PyDict_SetItemString(self->params,\"%s\",Py_BuildValue(\"d\",%10.12f));\n" % \ |
---|
[0f5bc9f] | 373 | (par, self.params[par]) |
---|
[af03ddd] | 374 | |
---|
[1b758b3] | 375 | if len(self.disp_params)>0: |
---|
| 376 | param_str += " // Initialize dispersion / averaging parameter dict\n" |
---|
| 377 | param_str += " DispersionVisitor* visitor = new DispersionVisitor();\n" |
---|
| 378 | param_str += " PyObject * disp_dict;\n" |
---|
| 379 | for par in self.disp_params: |
---|
| 380 | par = par.strip() |
---|
[5c8ffa5] | 381 | if par == '': |
---|
| 382 | continue |
---|
[1b758b3] | 383 | param_str += " disp_dict = PyDict_New();\n" |
---|
| 384 | param_str += " self->model->%s.dispersion->accept_as_source(visitor, self->model->%s.dispersion, disp_dict);\n" % (par, par) |
---|
| 385 | param_str += " PyDict_SetItemString(self->dispersion, \"%s\", disp_dict);\n" % par |
---|
[af03ddd] | 386 | |
---|
| 387 | # Initialize dispersion object dictionnary |
---|
| 388 | param_str += "\n" |
---|
| 389 | |
---|
| 390 | |
---|
| 391 | newline = self.replaceToken(newline, |
---|
| 392 | "[INITDICTIONARY]", param_str) |
---|
| 393 | |
---|
| 394 | # Read dictionary |
---|
| 395 | param_str = " // Reader parameter dictionary\n" |
---|
| 396 | for par in self.params: |
---|
| 397 | param_str += " self->model->%s = PyFloat_AsDouble( PyDict_GetItemString(self->params, \"%s\") );\n" % \ |
---|
| 398 | (par, par) |
---|
| 399 | |
---|
[1b758b3] | 400 | if len(self.disp_params)>0: |
---|
| 401 | param_str += " // Read in dispersion parameters\n" |
---|
| 402 | param_str += " PyObject* disp_dict;\n" |
---|
| 403 | param_str += " DispersionVisitor* visitor = new DispersionVisitor();\n" |
---|
| 404 | for par in self.disp_params: |
---|
| 405 | par = par.strip() |
---|
[5c8ffa5] | 406 | if par == '': |
---|
| 407 | continue |
---|
[1b758b3] | 408 | param_str += " disp_dict = PyDict_GetItemString(self->dispersion, \"%s\");\n" % par |
---|
| 409 | param_str += " self->model->%s.dispersion->accept_as_destination(visitor, self->model->%s.dispersion, disp_dict);\n" % (par, par) |
---|
[af03ddd] | 410 | |
---|
| 411 | newline = self.replaceToken(newline, "[READDICTIONARY]", param_str) |
---|
| 412 | |
---|
| 413 | # Name of .c file |
---|
| 414 | #toks = string.split(self.file,'.') |
---|
| 415 | basename = os.path.basename(self.file) |
---|
| 416 | toks = basename.split('.') |
---|
| 417 | newline = self.replaceToken(newline, "[C_FILENAME]", toks[0]) |
---|
| 418 | |
---|
| 419 | # Include file |
---|
| 420 | basename = os.path.basename(self.file) |
---|
| 421 | newline = self.replaceToken(newline, |
---|
[a1d1b90] | 422 | "[INCLUDE_FILE]", self.file) |
---|
| 423 | if self.foundCPP: |
---|
| 424 | newline = self.replaceToken(newline, |
---|
[8389beb] | 425 | "[C_INCLUDE_FILE]", "") |
---|
[a1d1b90] | 426 | newline = self.replaceToken(newline, |
---|
[8389beb] | 427 | "[CPP_INCLUDE_FILE]", "#include \"%s\"" % basename) |
---|
[a1d1b90] | 428 | else: |
---|
| 429 | newline = self.replaceToken(newline, |
---|
[8389beb] | 430 | "[C_INCLUDE_FILE]", "#include \"%s\"" % basename) |
---|
[a1d1b90] | 431 | newline = self.replaceToken(newline, |
---|
[8389beb] | 432 | "[CPP_INCLUDE_FILE]", "#include \"models.hh\"") |
---|
[af03ddd] | 433 | |
---|
| 434 | # Numerical calcs dealloc |
---|
| 435 | dealloc_str = "\n" |
---|
| 436 | if self.modelCalcFlag: |
---|
| 437 | dealloc_str = " modelcalculations_dealloc(&(self->model_pars.calcPars));\n" |
---|
| 438 | newline = self.replaceToken(newline, |
---|
| 439 | "[NUMERICAL_DEALLOC]", dealloc_str) |
---|
| 440 | |
---|
| 441 | # Numerical calcs init |
---|
| 442 | init_str = "\n" |
---|
| 443 | if self.modelCalcFlag: |
---|
| 444 | init_str = " modelcalculations_init(&(self->model_pars.calcPars));\n" |
---|
| 445 | newline = self.replaceToken(newline, |
---|
| 446 | "[NUMERICAL_INIT]", init_str) |
---|
| 447 | |
---|
| 448 | # Numerical calcs reset |
---|
| 449 | reset_str = "\n" |
---|
| 450 | if self.modelCalcFlag: |
---|
| 451 | reset_str = "modelcalculations_reset(&(self->model_pars.calcPars));\n" |
---|
| 452 | newline = self.replaceToken(newline, |
---|
| 453 | "[NUMERICAL_RESET]", reset_str) |
---|
| 454 | |
---|
| 455 | # Setting dispsertion weights |
---|
| 456 | set_weights = " // Ugliness necessary to go from python to C\n" |
---|
| 457 | set_weights = " // TODO: refactor this\n" |
---|
| 458 | for par in self.disp_params: |
---|
| 459 | par = par.strip() |
---|
[5c8ffa5] | 460 | if par == '': |
---|
| 461 | continue |
---|
[af03ddd] | 462 | set_weights += " if (!strcmp(par_name, \"%s\")) {\n" % par |
---|
| 463 | set_weights += " self->model->%s.dispersion = dispersion;\n" % par |
---|
| 464 | set_weights += " } else" |
---|
| 465 | newline = self.replaceToken(newline, |
---|
| 466 | "[SET_DISPERSION]", set_weights) |
---|
| 467 | |
---|
| 468 | # Write new line to the wrapper .c file |
---|
| 469 | file.write(newline+'\n') |
---|
| 470 | |
---|
| 471 | |
---|
| 472 | file.close() |
---|
| 473 | |
---|
| 474 | def write_python_wrapper(self): |
---|
| 475 | """ Writes the python file to create the python extension class |
---|
| 476 | The file is written in ../[PYTHONCLASS].py |
---|
| 477 | """ |
---|
[642a025] | 478 | file_path = os.path.join(self.output_dir, self.pythonClass+'.py') |
---|
[2d1b700] | 479 | file = open(file_path, 'w') |
---|
[8389beb] | 480 | template = open(os.path.join(os.path.dirname(__file__), |
---|
| 481 | "modelTemplate.txt"), 'r') |
---|
[af03ddd] | 482 | |
---|
| 483 | tmp_buf = template.read() |
---|
| 484 | tmp_lines = tmp_buf.split('\n') |
---|
| 485 | |
---|
| 486 | for tmp_line in tmp_lines: |
---|
| 487 | |
---|
| 488 | # Catch class name |
---|
| 489 | newline = self.replaceToken(tmp_line, |
---|
[318b5bbb] | 490 | "[CPYTHONCLASS]", |
---|
| 491 | 'C' + self.pythonClass) |
---|
[af03ddd] | 492 | |
---|
| 493 | # Catch class name |
---|
| 494 | newline = self.replaceToken(newline, |
---|
| 495 | "[PYTHONCLASS]", self.pythonClass) |
---|
| 496 | |
---|
| 497 | # Include file |
---|
| 498 | newline = self.replaceToken(newline, |
---|
| 499 | "[INCLUDE_FILE]", self.file) |
---|
| 500 | |
---|
| 501 | # Include file |
---|
| 502 | newline = self.replaceToken(newline, |
---|
| 503 | "[DEFAULT_LIST]", self.default_list) |
---|
[95986b5] | 504 | # model description |
---|
[af03ddd] | 505 | newline = self.replaceToken(newline, |
---|
[95986b5] | 506 | "[DESCRIPTION]", self.description) |
---|
[4e2f6ef8] | 507 | # Parameter details |
---|
| 508 | newline = self.replaceToken(newline, |
---|
[95986b5] | 509 | "[PAR_DETAILS]", self.details) |
---|
[5da3cc5] | 510 | |
---|
| 511 | # Call base constructor |
---|
| 512 | if self.is_multifunc: |
---|
| 513 | newline = self.replaceToken(newline,"[CALL_CPYTHON_INIT]", |
---|
[8389beb] | 514 | 'C' + self.pythonClass + \ |
---|
| 515 | ".__init__(self,multfactor)\n\tself.is_multifunc = True") |
---|
| 516 | newline = self.replaceToken(newline,"[MULTIPLICITY_INFO]", |
---|
| 517 | self.multiplicity_info) |
---|
[5da3cc5] | 518 | else: |
---|
| 519 | newline = self.replaceToken(newline,"[CALL_CPYTHON_INIT]", |
---|
[8389beb] | 520 | 'C' + self.pythonClass + \ |
---|
| 521 | ".__init__(self)\n self.is_multifunc = False") |
---|
| 522 | newline = self.replaceToken(newline, |
---|
[318b5bbb] | 523 | "[MULTIPLICITY_INFO]", "None") |
---|
[5da3cc5] | 524 | |
---|
[95986b5] | 525 | |
---|
[836fe6e] | 526 | # fixed list details |
---|
[79ba1fc] | 527 | fixed_str = str(self.fixed) |
---|
[0ea247e] | 528 | fixed_str = fixed_str.replace(', ', ',\n ') |
---|
[318b5bbb] | 529 | newline = self.replaceToken(newline, "[FIXED]", fixed_str) |
---|
[79ba1fc] | 530 | |
---|
| 531 | # non-fittable list details |
---|
[7343319] | 532 | pars_str = str(self.non_fittable) |
---|
[0ea247e] | 533 | pars_str = pars_str.replace(', ', |
---|
| 534 | ',\n ') |
---|
[836fe6e] | 535 | newline = self.replaceToken(newline, |
---|
[318b5bbb] | 536 | "[NON_FITTABLE_PARAMS]", pars_str) |
---|
[79ba1fc] | 537 | |
---|
[25a608f5] | 538 | ## parameters with orientation |
---|
[79ba1fc] | 539 | oriented_str = str(self.orientation_params) |
---|
[0ea247e] | 540 | formatted_endl = ',\n ' |
---|
[5c46f43] | 541 | oriented_str = oriented_str.replace(', ', formatted_endl) |
---|
[25a608f5] | 542 | newline = self.replaceToken(newline, |
---|
[79ba1fc] | 543 | "[ORIENTATION_PARAMS]", oriented_str) |
---|
[318b5bbb] | 544 | ## parameters with magnetism |
---|
| 545 | newline = self.replaceToken(newline, |
---|
| 546 | "[MAGNETIC_PARAMS]", str(self.magnetic_params)) |
---|
| 547 | |
---|
[fa6db8b] | 548 | if self.category: |
---|
[318b5bbb] | 549 | newline = self.replaceToken(newline, "[CATEGORY]", |
---|
| 550 | '"' + self.category + '"') |
---|
[fa6db8b] | 551 | else: |
---|
[318b5bbb] | 552 | newline = self.replaceToken(newline, "[CATEGORY]", |
---|
[fa6db8b] | 553 | "None") |
---|
| 554 | |
---|
| 555 | |
---|
| 556 | |
---|
[af03ddd] | 557 | # Write new line to the wrapper .c file |
---|
| 558 | file.write(newline+'\n') |
---|
| 559 | |
---|
| 560 | file.close() |
---|
| 561 | |
---|
| 562 | |
---|
| 563 | def replaceToken(self, line, key, value): #pylint: disable-msg=R0201 |
---|
| 564 | """ Replace a token in the template file |
---|
| 565 | @param line: line of text to inspect |
---|
| 566 | @param key: token to look for |
---|
| 567 | @param value: string value to replace the token with |
---|
| 568 | @return: new string value |
---|
| 569 | """ |
---|
| 570 | lenkey = len(key) |
---|
| 571 | newline = line |
---|
[836fe6e] | 572 | |
---|
[af03ddd] | 573 | while newline.count(key)>0: |
---|
| 574 | index = newline.index(key) |
---|
| 575 | newline = newline[:index]+value+newline[index+lenkey:] |
---|
[836fe6e] | 576 | |
---|
[af03ddd] | 577 | return newline |
---|
| 578 | |
---|
[0a9686d] | 579 | def getModelName(self): |
---|
| 580 | return self.pythonClass |
---|
[af03ddd] | 581 | |
---|
[0a9686d] | 582 | |
---|
| 583 | |
---|
[af03ddd] | 584 | # main |
---|
| 585 | if __name__ == '__main__': |
---|
| 586 | if len(sys.argv)>1: |
---|
| 587 | print "Will look for file %s" % sys.argv[1] |
---|
| 588 | app = WrapperGenerator(sys.argv[1]) |
---|
| 589 | else: |
---|
| 590 | app = WrapperGenerator("test.h") |
---|
| 591 | app.read() |
---|
| 592 | app.write_c_wrapper() |
---|
| 593 | app.write_python_wrapper() |
---|
| 594 | print app |
---|
| 595 | |
---|
[0a9686d] | 596 | # End of file |
---|