annotate DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/f2py/auxfuncs.py @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2a2c65a20a8b
children
rev   line source
Chris@87 1 #!/usr/bin/env python
Chris@87 2 """
Chris@87 3
Chris@87 4 Auxiliary functions for f2py2e.
Chris@87 5
Chris@87 6 Copyright 1999,2000 Pearu Peterson all rights reserved,
Chris@87 7 Pearu Peterson <pearu@ioc.ee>
Chris@87 8 Permission to use, modify, and distribute this software is given under the
Chris@87 9 terms of the NumPy (BSD style) LICENSE.
Chris@87 10
Chris@87 11
Chris@87 12 NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
Chris@87 13 $Date: 2005/07/24 19:01:55 $
Chris@87 14 Pearu Peterson
Chris@87 15
Chris@87 16 """
Chris@87 17 from __future__ import division, absolute_import, print_function
Chris@87 18
Chris@87 19 import pprint
Chris@87 20 import sys
Chris@87 21 import types
Chris@87 22 from functools import reduce
Chris@87 23
Chris@87 24 from . import __version__
Chris@87 25 from . import cfuncs
Chris@87 26
Chris@87 27 f2py_version = __version__.version
Chris@87 28
Chris@87 29
Chris@87 30 errmess=sys.stderr.write
Chris@87 31 #outmess=sys.stdout.write
Chris@87 32 show=pprint.pprint
Chris@87 33
Chris@87 34 options={}
Chris@87 35 debugoptions=[]
Chris@87 36 wrapfuncs = 1
Chris@87 37
Chris@87 38
Chris@87 39 def outmess(t):
Chris@87 40 if options.get('verbose', 1):
Chris@87 41 sys.stdout.write(t)
Chris@87 42
Chris@87 43 def debugcapi(var):
Chris@87 44 return 'capi' in debugoptions
Chris@87 45
Chris@87 46 def _isstring(var):
Chris@87 47 return 'typespec' in var and var['typespec']=='character' and (not isexternal(var))
Chris@87 48
Chris@87 49 def isstring(var):
Chris@87 50 return _isstring(var) and not isarray(var)
Chris@87 51
Chris@87 52 def ischaracter(var):
Chris@87 53 return isstring(var) and 'charselector' not in var
Chris@87 54
Chris@87 55 def isstringarray(var):
Chris@87 56 return isarray(var) and _isstring(var)
Chris@87 57
Chris@87 58 def isarrayofstrings(var):
Chris@87 59 # leaving out '*' for now so that
Chris@87 60 # `character*(*) a(m)` and `character a(m,*)`
Chris@87 61 # are treated differently. Luckily `character**` is illegal.
Chris@87 62 return isstringarray(var) and var['dimension'][-1]=='(*)'
Chris@87 63
Chris@87 64 def isarray(var):
Chris@87 65 return 'dimension' in var and (not isexternal(var))
Chris@87 66
Chris@87 67 def isscalar(var):
Chris@87 68 return not (isarray(var) or isstring(var) or isexternal(var))
Chris@87 69
Chris@87 70 def iscomplex(var):
Chris@87 71 return isscalar(var) and var.get('typespec') in ['complex', 'double complex']
Chris@87 72
Chris@87 73 def islogical(var):
Chris@87 74 return isscalar(var) and var.get('typespec')=='logical'
Chris@87 75
Chris@87 76 def isinteger(var):
Chris@87 77 return isscalar(var) and var.get('typespec')=='integer'
Chris@87 78
Chris@87 79 def isreal(var):
Chris@87 80 return isscalar(var) and var.get('typespec')=='real'
Chris@87 81
Chris@87 82 def get_kind(var):
Chris@87 83 try:
Chris@87 84 return var['kindselector']['*']
Chris@87 85 except KeyError:
Chris@87 86 try:
Chris@87 87 return var['kindselector']['kind']
Chris@87 88 except KeyError:
Chris@87 89 pass
Chris@87 90
Chris@87 91 def islong_long(var):
Chris@87 92 if not isscalar(var):
Chris@87 93 return 0
Chris@87 94 if var.get('typespec') not in ['integer', 'logical']:
Chris@87 95 return 0
Chris@87 96 return get_kind(var)=='8'
Chris@87 97
Chris@87 98 def isunsigned_char(var):
Chris@87 99 if not isscalar(var):
Chris@87 100 return 0
Chris@87 101 if var.get('typespec') != 'integer':
Chris@87 102 return 0
Chris@87 103 return get_kind(var)=='-1'
Chris@87 104
Chris@87 105 def isunsigned_short(var):
Chris@87 106 if not isscalar(var):
Chris@87 107 return 0
Chris@87 108 if var.get('typespec') != 'integer':
Chris@87 109 return 0
Chris@87 110 return get_kind(var)=='-2'
Chris@87 111
Chris@87 112 def isunsigned(var):
Chris@87 113 if not isscalar(var):
Chris@87 114 return 0
Chris@87 115 if var.get('typespec') != 'integer':
Chris@87 116 return 0
Chris@87 117 return get_kind(var)=='-4'
Chris@87 118
Chris@87 119 def isunsigned_long_long(var):
Chris@87 120 if not isscalar(var):
Chris@87 121 return 0
Chris@87 122 if var.get('typespec') != 'integer':
Chris@87 123 return 0
Chris@87 124 return get_kind(var)=='-8'
Chris@87 125
Chris@87 126 def isdouble(var):
Chris@87 127 if not isscalar(var):
Chris@87 128 return 0
Chris@87 129 if not var.get('typespec')=='real':
Chris@87 130 return 0
Chris@87 131 return get_kind(var)=='8'
Chris@87 132
Chris@87 133 def islong_double(var):
Chris@87 134 if not isscalar(var):
Chris@87 135 return 0
Chris@87 136 if not var.get('typespec')=='real':
Chris@87 137 return 0
Chris@87 138 return get_kind(var)=='16'
Chris@87 139
Chris@87 140 def islong_complex(var):
Chris@87 141 if not iscomplex(var):
Chris@87 142 return 0
Chris@87 143 return get_kind(var)=='32'
Chris@87 144
Chris@87 145 def iscomplexarray(var):
Chris@87 146 return isarray(var) and var.get('typespec') in ['complex', 'double complex']
Chris@87 147
Chris@87 148 def isint1array(var):
Chris@87 149 return isarray(var) and var.get('typespec')=='integer' \
Chris@87 150 and get_kind(var)=='1'
Chris@87 151
Chris@87 152 def isunsigned_chararray(var):
Chris@87 153 return isarray(var) and var.get('typespec') in ['integer', 'logical']\
Chris@87 154 and get_kind(var)=='-1'
Chris@87 155
Chris@87 156 def isunsigned_shortarray(var):
Chris@87 157 return isarray(var) and var.get('typespec') in ['integer', 'logical']\
Chris@87 158 and get_kind(var)=='-2'
Chris@87 159
Chris@87 160 def isunsignedarray(var):
Chris@87 161 return isarray(var) and var.get('typespec') in ['integer', 'logical']\
Chris@87 162 and get_kind(var)=='-4'
Chris@87 163
Chris@87 164 def isunsigned_long_longarray(var):
Chris@87 165 return isarray(var) and var.get('typespec') in ['integer', 'logical']\
Chris@87 166 and get_kind(var)=='-8'
Chris@87 167
Chris@87 168 def issigned_chararray(var):
Chris@87 169 return isarray(var) and var.get('typespec') in ['integer', 'logical']\
Chris@87 170 and get_kind(var)=='1'
Chris@87 171
Chris@87 172 def issigned_shortarray(var):
Chris@87 173 return isarray(var) and var.get('typespec') in ['integer', 'logical']\
Chris@87 174 and get_kind(var)=='2'
Chris@87 175
Chris@87 176 def issigned_array(var):
Chris@87 177 return isarray(var) and var.get('typespec') in ['integer', 'logical']\
Chris@87 178 and get_kind(var)=='4'
Chris@87 179
Chris@87 180 def issigned_long_longarray(var):
Chris@87 181 return isarray(var) and var.get('typespec') in ['integer', 'logical']\
Chris@87 182 and get_kind(var)=='8'
Chris@87 183
Chris@87 184 def isallocatable(var):
Chris@87 185 return 'attrspec' in var and 'allocatable' in var['attrspec']
Chris@87 186
Chris@87 187 def ismutable(var):
Chris@87 188 return not (not 'dimension' in var or isstring(var))
Chris@87 189
Chris@87 190 def ismoduleroutine(rout):
Chris@87 191 return 'modulename' in rout
Chris@87 192
Chris@87 193 def ismodule(rout):
Chris@87 194 return ('block' in rout and 'module'==rout['block'])
Chris@87 195
Chris@87 196 def isfunction(rout):
Chris@87 197 return ('block' in rout and 'function'==rout['block'])
Chris@87 198
Chris@87 199 #def isfunction_wrap(rout):
Chris@87 200 # return wrapfuncs and (iscomplexfunction(rout) or isstringfunction(rout)) and (not isexternal(rout))
Chris@87 201
Chris@87 202 def isfunction_wrap(rout):
Chris@87 203 if isintent_c(rout):
Chris@87 204 return 0
Chris@87 205 return wrapfuncs and isfunction(rout) and (not isexternal(rout))
Chris@87 206
Chris@87 207 def issubroutine(rout):
Chris@87 208 return ('block' in rout and 'subroutine'==rout['block'])
Chris@87 209
Chris@87 210 def issubroutine_wrap(rout):
Chris@87 211 if isintent_c(rout):
Chris@87 212 return 0
Chris@87 213 return issubroutine(rout) and hasassumedshape(rout)
Chris@87 214
Chris@87 215 def hasassumedshape(rout):
Chris@87 216 if rout.get('hasassumedshape'):
Chris@87 217 return True
Chris@87 218 for a in rout['args']:
Chris@87 219 for d in rout['vars'].get(a, {}).get('dimension', []):
Chris@87 220 if d==':':
Chris@87 221 rout['hasassumedshape'] = True
Chris@87 222 return True
Chris@87 223 return False
Chris@87 224
Chris@87 225 def isroutine(rout):
Chris@87 226 return isfunction(rout) or issubroutine(rout)
Chris@87 227
Chris@87 228 def islogicalfunction(rout):
Chris@87 229 if not isfunction(rout):
Chris@87 230 return 0
Chris@87 231 if 'result' in rout:
Chris@87 232 a=rout['result']
Chris@87 233 else:
Chris@87 234 a=rout['name']
Chris@87 235 if a in rout['vars']:
Chris@87 236 return islogical(rout['vars'][a])
Chris@87 237 return 0
Chris@87 238
Chris@87 239 def islong_longfunction(rout):
Chris@87 240 if not isfunction(rout):
Chris@87 241 return 0
Chris@87 242 if 'result' in rout:
Chris@87 243 a=rout['result']
Chris@87 244 else:
Chris@87 245 a=rout['name']
Chris@87 246 if a in rout['vars']:
Chris@87 247 return islong_long(rout['vars'][a])
Chris@87 248 return 0
Chris@87 249
Chris@87 250 def islong_doublefunction(rout):
Chris@87 251 if not isfunction(rout):
Chris@87 252 return 0
Chris@87 253 if 'result' in rout:
Chris@87 254 a=rout['result']
Chris@87 255 else:
Chris@87 256 a=rout['name']
Chris@87 257 if a in rout['vars']:
Chris@87 258 return islong_double(rout['vars'][a])
Chris@87 259 return 0
Chris@87 260
Chris@87 261 def iscomplexfunction(rout):
Chris@87 262 if not isfunction(rout):
Chris@87 263 return 0
Chris@87 264 if 'result' in rout:
Chris@87 265 a=rout['result']
Chris@87 266 else:
Chris@87 267 a=rout['name']
Chris@87 268 if a in rout['vars']:
Chris@87 269 return iscomplex(rout['vars'][a])
Chris@87 270 return 0
Chris@87 271
Chris@87 272 def iscomplexfunction_warn(rout):
Chris@87 273 if iscomplexfunction(rout):
Chris@87 274 outmess("""\
Chris@87 275 **************************************************************
Chris@87 276 Warning: code with a function returning complex value
Chris@87 277 may not work correctly with your Fortran compiler.
Chris@87 278 Run the following test before using it in your applications:
Chris@87 279 $(f2py install dir)/test-site/{b/runme_scalar,e/runme}
Chris@87 280 When using GNU gcc/g77 compilers, codes should work correctly.
Chris@87 281 **************************************************************\n""")
Chris@87 282 return 1
Chris@87 283 return 0
Chris@87 284
Chris@87 285 def isstringfunction(rout):
Chris@87 286 if not isfunction(rout):
Chris@87 287 return 0
Chris@87 288 if 'result' in rout:
Chris@87 289 a=rout['result']
Chris@87 290 else:
Chris@87 291 a=rout['name']
Chris@87 292 if a in rout['vars']:
Chris@87 293 return isstring(rout['vars'][a])
Chris@87 294 return 0
Chris@87 295
Chris@87 296 def hasexternals(rout):
Chris@87 297 return 'externals' in rout and rout['externals']
Chris@87 298
Chris@87 299 def isthreadsafe(rout):
Chris@87 300 return 'f2pyenhancements' in rout and 'threadsafe' in rout['f2pyenhancements']
Chris@87 301
Chris@87 302 def hasvariables(rout):
Chris@87 303 return 'vars' in rout and rout['vars']
Chris@87 304
Chris@87 305 def isoptional(var):
Chris@87 306 return ('attrspec' in var and 'optional' in var['attrspec'] and 'required' not in var['attrspec']) and isintent_nothide(var)
Chris@87 307
Chris@87 308 def isexternal(var):
Chris@87 309 return ('attrspec' in var and 'external' in var['attrspec'])
Chris@87 310
Chris@87 311 def isrequired(var):
Chris@87 312 return not isoptional(var) and isintent_nothide(var)
Chris@87 313
Chris@87 314 def isintent_in(var):
Chris@87 315 if 'intent' not in var:
Chris@87 316 return 1
Chris@87 317 if 'hide' in var['intent']:
Chris@87 318 return 0
Chris@87 319 if 'inplace' in var['intent']:
Chris@87 320 return 0
Chris@87 321 if 'in' in var['intent']:
Chris@87 322 return 1
Chris@87 323 if 'out' in var['intent']:
Chris@87 324 return 0
Chris@87 325 if 'inout' in var['intent']:
Chris@87 326 return 0
Chris@87 327 if 'outin' in var['intent']:
Chris@87 328 return 0
Chris@87 329 return 1
Chris@87 330
Chris@87 331 def isintent_inout(var):
Chris@87 332 return 'intent' in var and ('inout' in var['intent'] or 'outin' in var['intent']) and 'in' not in var['intent'] and 'hide' not in var['intent'] and 'inplace' not in var['intent']
Chris@87 333
Chris@87 334 def isintent_out(var):
Chris@87 335 return 'out' in var.get('intent', [])
Chris@87 336
Chris@87 337 def isintent_hide(var):
Chris@87 338 return ('intent' in var and ('hide' in var['intent'] or ('out' in var['intent'] and 'in' not in var['intent'] and (not l_or(isintent_inout, isintent_inplace)(var)))))
Chris@87 339
Chris@87 340 def isintent_nothide(var):
Chris@87 341 return not isintent_hide(var)
Chris@87 342
Chris@87 343 def isintent_c(var):
Chris@87 344 return 'c' in var.get('intent', [])
Chris@87 345
Chris@87 346 # def isintent_f(var):
Chris@87 347 # return not isintent_c(var)
Chris@87 348
Chris@87 349 def isintent_cache(var):
Chris@87 350 return 'cache' in var.get('intent', [])
Chris@87 351
Chris@87 352 def isintent_copy(var):
Chris@87 353 return 'copy' in var.get('intent', [])
Chris@87 354
Chris@87 355 def isintent_overwrite(var):
Chris@87 356 return 'overwrite' in var.get('intent', [])
Chris@87 357
Chris@87 358 def isintent_callback(var):
Chris@87 359 return 'callback' in var.get('intent', [])
Chris@87 360
Chris@87 361 def isintent_inplace(var):
Chris@87 362 return 'inplace' in var.get('intent', [])
Chris@87 363
Chris@87 364 def isintent_aux(var):
Chris@87 365 return 'aux' in var.get('intent', [])
Chris@87 366
Chris@87 367 def isintent_aligned4(var):
Chris@87 368 return 'aligned4' in var.get('intent', [])
Chris@87 369 def isintent_aligned8(var):
Chris@87 370 return 'aligned8' in var.get('intent', [])
Chris@87 371 def isintent_aligned16(var):
Chris@87 372 return 'aligned16' in var.get('intent', [])
Chris@87 373
Chris@87 374 isintent_dict = {isintent_in: 'INTENT_IN', isintent_inout: 'INTENT_INOUT',
Chris@87 375 isintent_out: 'INTENT_OUT', isintent_hide: 'INTENT_HIDE',
Chris@87 376 isintent_cache: 'INTENT_CACHE',
Chris@87 377 isintent_c: 'INTENT_C', isoptional: 'OPTIONAL',
Chris@87 378 isintent_inplace: 'INTENT_INPLACE',
Chris@87 379 isintent_aligned4: 'INTENT_ALIGNED4',
Chris@87 380 isintent_aligned8: 'INTENT_ALIGNED8',
Chris@87 381 isintent_aligned16: 'INTENT_ALIGNED16',
Chris@87 382 }
Chris@87 383
Chris@87 384 def isprivate(var):
Chris@87 385 return 'attrspec' in var and 'private' in var['attrspec']
Chris@87 386
Chris@87 387 def hasinitvalue(var):
Chris@87 388 return '=' in var
Chris@87 389
Chris@87 390 def hasinitvalueasstring(var):
Chris@87 391 if not hasinitvalue(var):
Chris@87 392 return 0
Chris@87 393 return var['='][0] in ['"', "'"]
Chris@87 394
Chris@87 395 def hasnote(var):
Chris@87 396 return 'note' in var
Chris@87 397
Chris@87 398 def hasresultnote(rout):
Chris@87 399 if not isfunction(rout):
Chris@87 400 return 0
Chris@87 401 if 'result' in rout:
Chris@87 402 a=rout['result']
Chris@87 403 else:
Chris@87 404 a=rout['name']
Chris@87 405 if a in rout['vars']:
Chris@87 406 return hasnote(rout['vars'][a])
Chris@87 407 return 0
Chris@87 408
Chris@87 409 def hascommon(rout):
Chris@87 410 return 'common' in rout
Chris@87 411
Chris@87 412 def containscommon(rout):
Chris@87 413 if hascommon(rout):
Chris@87 414 return 1
Chris@87 415 if hasbody(rout):
Chris@87 416 for b in rout['body']:
Chris@87 417 if containscommon(b):
Chris@87 418 return 1
Chris@87 419 return 0
Chris@87 420
Chris@87 421 def containsmodule(block):
Chris@87 422 if ismodule(block):
Chris@87 423 return 1
Chris@87 424 if not hasbody(block):
Chris@87 425 return 0
Chris@87 426 for b in block['body']:
Chris@87 427 if containsmodule(b):
Chris@87 428 return 1
Chris@87 429 return 0
Chris@87 430
Chris@87 431 def hasbody(rout):
Chris@87 432 return 'body' in rout
Chris@87 433
Chris@87 434 def hascallstatement(rout):
Chris@87 435 return getcallstatement(rout) is not None
Chris@87 436
Chris@87 437 def istrue(var):
Chris@87 438 return 1
Chris@87 439
Chris@87 440 def isfalse(var):
Chris@87 441 return 0
Chris@87 442
Chris@87 443 class F2PYError(Exception):
Chris@87 444 pass
Chris@87 445
Chris@87 446 class throw_error:
Chris@87 447 def __init__(self, mess):
Chris@87 448 self.mess = mess
Chris@87 449 def __call__(self, var):
Chris@87 450 mess = '\n\n var = %s\n Message: %s\n' % (var, self.mess)
Chris@87 451 raise F2PYError(mess)
Chris@87 452
Chris@87 453 def l_and(*f):
Chris@87 454 l, l2='lambda v', []
Chris@87 455 for i in range(len(f)):
Chris@87 456 l='%s,f%d=f[%d]'%(l, i, i)
Chris@87 457 l2.append('f%d(v)'%(i))
Chris@87 458 return eval('%s:%s'%(l, ' and '.join(l2)))
Chris@87 459
Chris@87 460 def l_or(*f):
Chris@87 461 l, l2='lambda v', []
Chris@87 462 for i in range(len(f)):
Chris@87 463 l='%s,f%d=f[%d]'%(l, i, i)
Chris@87 464 l2.append('f%d(v)'%(i))
Chris@87 465 return eval('%s:%s'%(l, ' or '.join(l2)))
Chris@87 466
Chris@87 467 def l_not(f):
Chris@87 468 return eval('lambda v,f=f:not f(v)')
Chris@87 469
Chris@87 470 def isdummyroutine(rout):
Chris@87 471 try:
Chris@87 472 return rout['f2pyenhancements']['fortranname']==''
Chris@87 473 except KeyError:
Chris@87 474 return 0
Chris@87 475
Chris@87 476 def getfortranname(rout):
Chris@87 477 try:
Chris@87 478 name = rout['f2pyenhancements']['fortranname']
Chris@87 479 if name=='':
Chris@87 480 raise KeyError
Chris@87 481 if not name:
Chris@87 482 errmess('Failed to use fortranname from %s\n'%(rout['f2pyenhancements']))
Chris@87 483 raise KeyError
Chris@87 484 except KeyError:
Chris@87 485 name = rout['name']
Chris@87 486 return name
Chris@87 487
Chris@87 488 def getmultilineblock(rout,blockname,comment=1,counter=0):
Chris@87 489 try:
Chris@87 490 r = rout['f2pyenhancements'].get(blockname)
Chris@87 491 except KeyError:
Chris@87 492 return
Chris@87 493 if not r: return
Chris@87 494 if counter > 0 and isinstance(r, str):
Chris@87 495 return
Chris@87 496 if isinstance(r, list):
Chris@87 497 if counter>=len(r): return
Chris@87 498 r = r[counter]
Chris@87 499 if r[:3]=="'''":
Chris@87 500 if comment:
Chris@87 501 r = '\t/* start ' + blockname + ' multiline ('+repr(counter)+') */\n' + r[3:]
Chris@87 502 else:
Chris@87 503 r = r[3:]
Chris@87 504 if r[-3:]=="'''":
Chris@87 505 if comment:
Chris@87 506 r = r[:-3] + '\n\t/* end multiline ('+repr(counter)+')*/'
Chris@87 507 else:
Chris@87 508 r = r[:-3]
Chris@87 509 else:
Chris@87 510 errmess("%s multiline block should end with `'''`: %s\n" \
Chris@87 511 % (blockname, repr(r)))
Chris@87 512 return r
Chris@87 513
Chris@87 514 def getcallstatement(rout):
Chris@87 515 return getmultilineblock(rout, 'callstatement')
Chris@87 516
Chris@87 517 def getcallprotoargument(rout,cb_map={}):
Chris@87 518 r = getmultilineblock(rout, 'callprotoargument', comment=0)
Chris@87 519 if r: return r
Chris@87 520 if hascallstatement(rout):
Chris@87 521 outmess('warning: callstatement is defined without callprotoargument\n')
Chris@87 522 return
Chris@87 523 from .capi_maps import getctype
Chris@87 524 arg_types, arg_types2 = [], []
Chris@87 525 if l_and(isstringfunction, l_not(isfunction_wrap))(rout):
Chris@87 526 arg_types.extend(['char*', 'size_t'])
Chris@87 527 for n in rout['args']:
Chris@87 528 var = rout['vars'][n]
Chris@87 529 if isintent_callback(var):
Chris@87 530 continue
Chris@87 531 if n in cb_map:
Chris@87 532 ctype = cb_map[n]+'_typedef'
Chris@87 533 else:
Chris@87 534 ctype = getctype(var)
Chris@87 535 if l_and(isintent_c, l_or(isscalar, iscomplex))(var):
Chris@87 536 pass
Chris@87 537 elif isstring(var):
Chris@87 538 pass
Chris@87 539 #ctype = 'void*'
Chris@87 540 else:
Chris@87 541 ctype = ctype+'*'
Chris@87 542 if isstring(var) or isarrayofstrings(var):
Chris@87 543 arg_types2.append('size_t')
Chris@87 544 arg_types.append(ctype)
Chris@87 545
Chris@87 546 proto_args = ','.join(arg_types+arg_types2)
Chris@87 547 if not proto_args:
Chris@87 548 proto_args = 'void'
Chris@87 549 #print proto_args
Chris@87 550 return proto_args
Chris@87 551
Chris@87 552 def getusercode(rout):
Chris@87 553 return getmultilineblock(rout, 'usercode')
Chris@87 554
Chris@87 555 def getusercode1(rout):
Chris@87 556 return getmultilineblock(rout, 'usercode', counter=1)
Chris@87 557
Chris@87 558 def getpymethoddef(rout):
Chris@87 559 return getmultilineblock(rout, 'pymethoddef')
Chris@87 560
Chris@87 561 def getargs(rout):
Chris@87 562 sortargs, args=[], []
Chris@87 563 if 'args' in rout:
Chris@87 564 args=rout['args']
Chris@87 565 if 'sortvars' in rout:
Chris@87 566 for a in rout['sortvars']:
Chris@87 567 if a in args: sortargs.append(a)
Chris@87 568 for a in args:
Chris@87 569 if a not in sortargs:
Chris@87 570 sortargs.append(a)
Chris@87 571 else: sortargs=rout['args']
Chris@87 572 return args, sortargs
Chris@87 573
Chris@87 574 def getargs2(rout):
Chris@87 575 sortargs, args=[], rout.get('args', [])
Chris@87 576 auxvars = [a for a in rout['vars'].keys() if isintent_aux(rout['vars'][a])\
Chris@87 577 and a not in args]
Chris@87 578 args = auxvars + args
Chris@87 579 if 'sortvars' in rout:
Chris@87 580 for a in rout['sortvars']:
Chris@87 581 if a in args: sortargs.append(a)
Chris@87 582 for a in args:
Chris@87 583 if a not in sortargs:
Chris@87 584 sortargs.append(a)
Chris@87 585 else: sortargs=auxvars + rout['args']
Chris@87 586 return args, sortargs
Chris@87 587
Chris@87 588 def getrestdoc(rout):
Chris@87 589 if 'f2pymultilines' not in rout:
Chris@87 590 return None
Chris@87 591 k = None
Chris@87 592 if rout['block']=='python module':
Chris@87 593 k = rout['block'], rout['name']
Chris@87 594 return rout['f2pymultilines'].get(k, None)
Chris@87 595
Chris@87 596 def gentitle(name):
Chris@87 597 l=(80-len(name)-6)//2
Chris@87 598 return '/*%s %s %s*/'%(l*'*', name, l*'*')
Chris@87 599
Chris@87 600 def flatlist(l):
Chris@87 601 if isinstance(l, list):
Chris@87 602 return reduce(lambda x,y,f=flatlist:x+f(y), l, [])
Chris@87 603 return [l]
Chris@87 604
Chris@87 605 def stripcomma(s):
Chris@87 606 if s and s[-1]==',': return s[:-1]
Chris@87 607 return s
Chris@87 608
Chris@87 609 def replace(str,d,defaultsep=''):
Chris@87 610 if isinstance(d, list):
Chris@87 611 return [replace(str, _m, defaultsep) for _m in d]
Chris@87 612 if isinstance(str, list):
Chris@87 613 return [replace(_m, d, defaultsep) for _m in str]
Chris@87 614 for k in 2*list(d.keys()):
Chris@87 615 if k=='separatorsfor':
Chris@87 616 continue
Chris@87 617 if 'separatorsfor' in d and k in d['separatorsfor']:
Chris@87 618 sep=d['separatorsfor'][k]
Chris@87 619 else:
Chris@87 620 sep=defaultsep
Chris@87 621 if isinstance(d[k], list):
Chris@87 622 str=str.replace('#%s#'%(k), sep.join(flatlist(d[k])))
Chris@87 623 else:
Chris@87 624 str=str.replace('#%s#'%(k), d[k])
Chris@87 625 return str
Chris@87 626
Chris@87 627 def dictappend(rd, ar):
Chris@87 628 if isinstance(ar, list):
Chris@87 629 for a in ar:
Chris@87 630 rd=dictappend(rd, a)
Chris@87 631 return rd
Chris@87 632 for k in ar.keys():
Chris@87 633 if k[0]=='_':
Chris@87 634 continue
Chris@87 635 if k in rd:
Chris@87 636 if isinstance(rd[k], str):
Chris@87 637 rd[k]=[rd[k]]
Chris@87 638 if isinstance(rd[k], list):
Chris@87 639 if isinstance(ar[k], list):
Chris@87 640 rd[k]=rd[k]+ar[k]
Chris@87 641 else:
Chris@87 642 rd[k].append(ar[k])
Chris@87 643 elif isinstance(rd[k], dict):
Chris@87 644 if isinstance(ar[k], dict):
Chris@87 645 if k=='separatorsfor':
Chris@87 646 for k1 in ar[k].keys():
Chris@87 647 if k1 not in rd[k]:
Chris@87 648 rd[k][k1]=ar[k][k1]
Chris@87 649 else:
Chris@87 650 rd[k]=dictappend(rd[k], ar[k])
Chris@87 651 else:
Chris@87 652 rd[k]=ar[k]
Chris@87 653 return rd
Chris@87 654
Chris@87 655 def applyrules(rules,d,var={}):
Chris@87 656 ret={}
Chris@87 657 if isinstance(rules, list):
Chris@87 658 for r in rules:
Chris@87 659 rr=applyrules(r, d, var)
Chris@87 660 ret=dictappend(ret, rr)
Chris@87 661 if '_break' in rr:
Chris@87 662 break
Chris@87 663 return ret
Chris@87 664 if '_check' in rules and (not rules['_check'](var)):
Chris@87 665 return ret
Chris@87 666 if 'need' in rules:
Chris@87 667 res = applyrules({'needs':rules['need']}, d, var)
Chris@87 668 if 'needs' in res:
Chris@87 669 cfuncs.append_needs(res['needs'])
Chris@87 670
Chris@87 671 for k in rules.keys():
Chris@87 672 if k=='separatorsfor':
Chris@87 673 ret[k]=rules[k]; continue
Chris@87 674 if isinstance(rules[k], str):
Chris@87 675 ret[k]=replace(rules[k], d)
Chris@87 676 elif isinstance(rules[k], list):
Chris@87 677 ret[k]=[]
Chris@87 678 for i in rules[k]:
Chris@87 679 ar=applyrules({k:i}, d, var)
Chris@87 680 if k in ar:
Chris@87 681 ret[k].append(ar[k])
Chris@87 682 elif k[0]=='_':
Chris@87 683 continue
Chris@87 684 elif isinstance(rules[k], dict):
Chris@87 685 ret[k]=[]
Chris@87 686 for k1 in rules[k].keys():
Chris@87 687 if isinstance(k1, types.FunctionType) and k1(var):
Chris@87 688 if isinstance(rules[k][k1], list):
Chris@87 689 for i in rules[k][k1]:
Chris@87 690 if isinstance(i, dict):
Chris@87 691 res=applyrules({'supertext':i}, d, var)
Chris@87 692 if 'supertext' in res:
Chris@87 693 i=res['supertext']
Chris@87 694 else: i=''
Chris@87 695 ret[k].append(replace(i, d))
Chris@87 696 else:
Chris@87 697 i=rules[k][k1]
Chris@87 698 if isinstance(i, dict):
Chris@87 699 res=applyrules({'supertext':i}, d)
Chris@87 700 if 'supertext' in res:
Chris@87 701 i=res['supertext']
Chris@87 702 else: i=''
Chris@87 703 ret[k].append(replace(i, d))
Chris@87 704 else:
Chris@87 705 errmess('applyrules: ignoring rule %s.\n'%repr(rules[k]))
Chris@87 706 if isinstance(ret[k], list):
Chris@87 707 if len(ret[k])==1:
Chris@87 708 ret[k]=ret[k][0]
Chris@87 709 if ret[k]==[]:
Chris@87 710 del ret[k]
Chris@87 711 return ret