flag.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.modules.interface.docstring 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. 'a'
  13. >>> flag.special
  14. False
  15. >>> flag.description
  16. 'Flag description'
  17. >>> flag = Flag(diz=dict(name='overwrite'))
  18. >>> flag.name
  19. '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. self.suppress_required = True if 'suppress_required' in diz else False
  33. def get_bash(self):
  34. """Return the BASH representation of a flag.
  35. >>> flag = Flag(diz=dict(name='a', description='Flag description',
  36. ... default=True))
  37. >>> flag.get_bash()
  38. ''
  39. >>> flag.value = True
  40. >>> flag.get_bash()
  41. '-a'
  42. >>> flag = Flag(diz=dict(name='overwrite'))
  43. >>> flag.get_bash()
  44. ''
  45. >>> flag.value = True
  46. >>> flag.get_bash()
  47. '--o'
  48. """
  49. if self.value:
  50. if self.special:
  51. return '--%s' % self.name[0]
  52. else:
  53. return '-%s' % self.name
  54. else:
  55. return ''
  56. def get_python(self):
  57. """Return the python representation of a flag.
  58. >>> flag = Flag(diz=dict(name='a', description='Flag description',
  59. ... default=True))
  60. >>> flag.get_python()
  61. ''
  62. >>> flag.value = True
  63. >>> flag.get_python()
  64. 'a'
  65. >>> flag = Flag(diz=dict(name='overwrite'))
  66. >>> flag.get_python()
  67. ''
  68. >>> flag.value = True
  69. >>> flag.get_python()
  70. 'overwrite=True'
  71. """
  72. if self.value:
  73. return '%s=True' % self.name if self.special else self.name
  74. return ''
  75. def __str__(self):
  76. """Return the BASH representation of the flag."""
  77. return self.get_bash()
  78. def __repr__(self):
  79. """Return a string with the python representation of the instance."""
  80. return "Flag <%s> (%s)" % (self.name, self.description)
  81. def __bool__(self):
  82. """Return a boolean value"""
  83. return self.value
  84. def __nonzero__(self):
  85. return self.__bool__()
  86. @docstring_property(__doc__)
  87. def __doc__(self):
  88. """Return a documentation string, something like:
  89. {name}: {default}, suppress required {supress}
  90. {description}
  91. >>> flag = Flag(diz=dict(name='a', description='Flag description',
  92. ... default=True))
  93. >>> print(flag.__doc__)
  94. a: True
  95. Flag description
  96. >>> flag = Flag(diz=dict(name='overwrite'))
  97. >>> print(flag.__doc__)
  98. overwrite: None
  99. None
  100. """
  101. return read.DOC['flag'].format(name=self.name,
  102. default=repr(self.default),
  103. description=self.description,
  104. supress=('suppress required'
  105. if self.suppress_required
  106. else ''))