auxfuncs.py 22 KB

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