flag.py 3.7 KB

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