read.py 2.6 KB

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