flag.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. from __future__ import (nested_scopes, generators, division, absolute_import,
  3. with_statement, print_function, unicode_literals)
  4. from grass.pygrass.functions import docstring_property
  5. from grass.pygrass.modules.interface import read
  6. class Flag(object):
  7. """The Flag object store all information about a flag of module.
  8. It is possible to set flags of command using this object.
  9. >>> flag = Flag(diz=dict(name='a', description='Flag description',
  10. ... default=True))
  11. >>> flag.name
  12. u'a'
  13. >>> flag.special
  14. False
  15. >>> flag.description
  16. u'Flag description'
  17. >>> flag = Flag(diz=dict(name='overwrite'))
  18. >>> flag.name
  19. u'overwrite'
  20. >>> flag.special
  21. True
  22. """
  23. def __init__(self, xflag=None, diz=None):
  24. self.value = False
  25. diz = read.element2dict(xflag) if xflag is not None else diz
  26. self.name = diz['name']
  27. self.special = True if self.name in (
  28. 'verbose', 'overwrite', 'quiet', 'run') else False
  29. self.description = diz.get('description', None)
  30. self.default = diz.get('default', None)
  31. self.guisection = diz.get('guisection', None)
  32. def get_bash(self):
  33. """Return the BASH representation of a flag.
  34. >>> flag = Flag(diz=dict(name='a', description='Flag description',
  35. ... default=True))
  36. >>> flag.get_bash()
  37. u''
  38. >>> flag.value = True
  39. >>> flag.get_bash()
  40. u'-a'
  41. >>> flag = Flag(diz=dict(name='overwrite'))
  42. >>> flag.get_bash()
  43. u''
  44. >>> flag.value = True
  45. >>> flag.get_bash()
  46. u'--o'
  47. """
  48. if self.value:
  49. if self.special:
  50. return '--%s' % self.name[0]
  51. else:
  52. return '-%s' % self.name
  53. else:
  54. return ''
  55. def get_python(self):
  56. """Return the python representation of a flag.
  57. >>> flag = Flag(diz=dict(name='a', description='Flag description',
  58. ... default=True))
  59. >>> flag.get_python()
  60. u''
  61. >>> flag.value = True
  62. >>> flag.get_python()
  63. u'a'
  64. >>> flag = Flag(diz=dict(name='overwrite'))
  65. >>> flag.get_python()
  66. u''
  67. >>> flag.value = True
  68. >>> flag.get_python()
  69. u'overwrite=True'
  70. """
  71. if self.value:
  72. return '%s=True' % self.name if self.special else self.name
  73. return ''
  74. def __str__(self):
  75. """Return the BASH representation of the flag."""
  76. return self.get_bash()
  77. def __repr__(self):
  78. """Return a string with the python representation of the instance."""
  79. return "Flag <%s> (%s)" % (self.name, self.description)
  80. @docstring_property(__doc__)
  81. def __doc__(self):
  82. """Return a documentation string, something like:
  83. {name}: {default}
  84. {description}
  85. >>> flag = Flag(diz=dict(name='a', description='Flag description',
  86. ... default=True))
  87. >>> print(flag.__doc__)
  88. a: True
  89. Flag description
  90. >>> flag = Flag(diz=dict(name='overwrite'))
  91. >>> print(flag.__doc__)
  92. overwrite: None
  93. None
  94. """
  95. return read.DOC['flag'].format(name=self.name,
  96. default=repr(self.default),
  97. description=self.description)