read.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Apr 2 18:30:34 2013
  4. @author: pietro
  5. """
  6. from __future__ import print_function
  7. def read_keydesc(par):
  8. name = par.text.strip()
  9. items = [e.text.strip() for e in par.findall('item')]
  10. #import ipdb; ipdb.set_trace()
  11. return name, tuple(items) if len(items) > 1 else None
  12. #
  13. # this dictionary is used to extract the value of interest from the xml
  14. # the lambda experssion is used to define small simple functions,
  15. # is equivalent to: ::
  16. #
  17. # def f(p):
  18. # return p.text.strip()
  19. #
  20. # and then we call f(p)
  21. #
  22. GETFROMTAG = {
  23. 'description': lambda p: p.text.strip(),
  24. 'keydesc': read_keydesc,
  25. 'gisprompt': lambda p: dict(p.items()),
  26. 'default': lambda p: p.text.strip(),
  27. 'values': lambda p: [e.text.strip() for e in p.findall('value/name')],
  28. 'value': lambda p: None,
  29. 'guisection': lambda p: p.text.strip(),
  30. 'label': lambda p: p.text.strip(),
  31. 'suppress_required': lambda p: None,
  32. 'keywords': lambda p: p.text.strip(),
  33. }
  34. GETTYPE = {
  35. 'string': str,
  36. 'integer': int,
  37. 'float': float,
  38. 'double': float,
  39. 'all': lambda x: x,
  40. }
  41. def element2dict(xparameter):
  42. diz = dict(xparameter.items())
  43. for p in xparameter:
  44. if p.tag in GETFROMTAG:
  45. diz[p.tag] = GETFROMTAG[p.tag](p)
  46. else:
  47. print('New tag: %s, ignored' % p.tag)
  48. return diz
  49. # dictionary used to create docstring for the objects
  50. DOC = {
  51. #------------------------------------------------------------
  52. # head
  53. 'head': """{cmd_name}({cmd_params})
  54. Parameters
  55. ----------
  56. """,
  57. #------------------------------------------------------------
  58. # param
  59. 'param': """{name}: {default}{required}{multi}{ptype}
  60. {description}{values}{keydescvalues}""",
  61. #------------------------------------------------------------
  62. # flag_head
  63. 'flag_head': """
  64. Flags
  65. ------
  66. """,
  67. #------------------------------------------------------------
  68. # flag
  69. 'flag': """{name}: {default}
  70. {description}""",
  71. #------------------------------------------------------------
  72. # foot
  73. 'foot': """
  74. Special Parameters
  75. ------------------
  76. The Module class have some optional parameters which are distinct using a final
  77. underscore.
  78. run_: True, optional
  79. If True execute the module.
  80. finish_: True, optional
  81. If True wait untill the end of the module execution, and store the module
  82. outputs into stdout, stderr attributes of the class.
  83. stdin_: PIPE,
  84. Set the standard input
  85. """}