read.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. 'file': str,
  40. 'all': lambda x: x,
  41. }
  42. def element2dict(xparameter):
  43. diz = dict(xparameter.items())
  44. for p in xparameter:
  45. if p.tag in GETFROMTAG:
  46. diz[p.tag] = GETFROMTAG[p.tag](p)
  47. else:
  48. print('New tag: %s, ignored' % p.tag)
  49. return diz
  50. # dictionary used to create docstring for the objects
  51. DOC = {
  52. #------------------------------------------------------------
  53. # head
  54. 'head': """{cmd_name}({cmd_params})
  55. Parameters
  56. ----------
  57. """,
  58. #------------------------------------------------------------
  59. # param
  60. 'param': """{name}: {default}{required}{multi}{ptype}
  61. {description}{values}{keydescvalues}""",
  62. #------------------------------------------------------------
  63. # flag_head
  64. 'flag_head': """
  65. Flags
  66. ------
  67. """,
  68. #------------------------------------------------------------
  69. # flag
  70. 'flag': """{name}: {default}
  71. {description}""",
  72. #------------------------------------------------------------
  73. # foot
  74. 'foot': """
  75. Special Parameters
  76. ------------------
  77. The Module class have some optional parameters which are distinct using a final
  78. underscore.
  79. run_: True, optional
  80. If True execute the module.
  81. finish_: True, optional
  82. If True wait untill the end of the module execution, and store the module
  83. outputs into stdout, stderr attributes of the class.
  84. stdin_: PIPE, optional
  85. Set the standard input.
  86. env_: dictionary, optional
  87. Set the evironment variables.
  88. """}