flag.py 3.7 KB

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