use_rules.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python3
  2. """
  3. Build 'use others module data' mechanism for f2py2e.
  4. Unfinished.
  5. Copyright 2000 Pearu Peterson all rights reserved,
  6. Pearu Peterson <pearu@ioc.ee>
  7. Permission to use, modify, and distribute this software is given under the
  8. terms of the NumPy License.
  9. NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  10. $Date: 2000/09/10 12:35:43 $
  11. Pearu Peterson
  12. """
  13. __version__ = "$Revision: 1.3 $"[10:-1]
  14. f2py_version = 'See `f2py -v`'
  15. from .auxfuncs import (
  16. applyrules, dictappend, gentitle, hasnote, outmess
  17. )
  18. usemodule_rules = {
  19. 'body': """
  20. #begintitle#
  21. static char doc_#apiname#[] = \"\\\nVariable wrapper signature:\\n\\
  22. \t #name# = get_#name#()\\n\\
  23. Arguments:\\n\\
  24. #docstr#\";
  25. extern F_MODFUNC(#usemodulename#,#USEMODULENAME#,#realname#,#REALNAME#);
  26. static PyObject *#apiname#(PyObject *capi_self, PyObject *capi_args) {
  27. /*#decl#*/
  28. \tif (!PyArg_ParseTuple(capi_args, \"\")) goto capi_fail;
  29. printf(\"c: %d\\n\",F_MODFUNC(#usemodulename#,#USEMODULENAME#,#realname#,#REALNAME#));
  30. \treturn Py_BuildValue(\"\");
  31. capi_fail:
  32. \treturn NULL;
  33. }
  34. """,
  35. 'method': '\t{\"get_#name#\",#apiname#,METH_VARARGS|METH_KEYWORDS,doc_#apiname#},',
  36. 'need': ['F_MODFUNC']
  37. }
  38. ################
  39. def buildusevars(m, r):
  40. ret = {}
  41. outmess(
  42. '\t\tBuilding use variable hooks for module "%s" (feature only for F90/F95)...\n' % (m['name']))
  43. varsmap = {}
  44. revmap = {}
  45. if 'map' in r:
  46. for k in r['map'].keys():
  47. if r['map'][k] in revmap:
  48. outmess('\t\t\tVariable "%s<=%s" is already mapped by "%s". Skipping.\n' % (
  49. r['map'][k], k, revmap[r['map'][k]]))
  50. else:
  51. revmap[r['map'][k]] = k
  52. if 'only' in r and r['only']:
  53. for v in r['map'].keys():
  54. if r['map'][v] in m['vars']:
  55. if revmap[r['map'][v]] == v:
  56. varsmap[v] = r['map'][v]
  57. else:
  58. outmess('\t\t\tIgnoring map "%s=>%s". See above.\n' %
  59. (v, r['map'][v]))
  60. else:
  61. outmess(
  62. '\t\t\tNo definition for variable "%s=>%s". Skipping.\n' % (v, r['map'][v]))
  63. else:
  64. for v in m['vars'].keys():
  65. if v in revmap:
  66. varsmap[v] = revmap[v]
  67. else:
  68. varsmap[v] = v
  69. for v in varsmap.keys():
  70. ret = dictappend(ret, buildusevar(v, varsmap[v], m['vars'], m['name']))
  71. return ret
  72. def buildusevar(name, realname, vars, usemodulename):
  73. outmess('\t\t\tConstructing wrapper function for variable "%s=>%s"...\n' % (
  74. name, realname))
  75. ret = {}
  76. vrd = {'name': name,
  77. 'realname': realname,
  78. 'REALNAME': realname.upper(),
  79. 'usemodulename': usemodulename,
  80. 'USEMODULENAME': usemodulename.upper(),
  81. 'texname': name.replace('_', '\\_'),
  82. 'begintitle': gentitle('%s=>%s' % (name, realname)),
  83. 'endtitle': gentitle('end of %s=>%s' % (name, realname)),
  84. 'apiname': '#modulename#_use_%s_from_%s' % (realname, usemodulename)
  85. }
  86. nummap = {0: 'Ro', 1: 'Ri', 2: 'Rii', 3: 'Riii', 4: 'Riv',
  87. 5: 'Rv', 6: 'Rvi', 7: 'Rvii', 8: 'Rviii', 9: 'Rix'}
  88. vrd['texnamename'] = name
  89. for i in nummap.keys():
  90. vrd['texnamename'] = vrd['texnamename'].replace(repr(i), nummap[i])
  91. if hasnote(vars[realname]):
  92. vrd['note'] = vars[realname]['note']
  93. rd = dictappend({}, vrd)
  94. print(name, realname, vars[realname])
  95. ret = applyrules(usemodule_rules, rd)
  96. return ret