File: flag.py

package info (click to toggle)
grass 7.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 135,976 kB
  • ctags: 44,148
  • sloc: ansic: 410,300; python: 166,939; cpp: 34,819; sh: 9,358; makefile: 6,618; xml: 3,551; sql: 769; lex: 519; yacc: 450; asm: 387; perl: 282; sed: 17; objc: 7
file content (123 lines) | stat: -rw-r--r-- 3,981 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
from __future__ import (nested_scopes, generators, division, absolute_import,
                        with_statement, print_function, unicode_literals)
from grass.pygrass.modules.interface.docstring import docstring_property
from grass.pygrass.modules.interface import read


class Flag(object):
    """The Flag object store all information about a flag of module.

    It is possible to set flags of command using this object.

    >>> flag = Flag(diz=dict(name='a', description='Flag description',
    ...                      default=True))
    >>> flag.name
    u'a'
    >>> flag.special
    False
    >>> flag.description
    u'Flag description'
    >>> flag = Flag(diz=dict(name='overwrite'))
    >>> flag.name
    u'overwrite'
    >>> flag.special
    True
    """
    def __init__(self, xflag=None, diz=None):
        self.value = False
        diz = read.element2dict(xflag) if xflag is not None else diz
        self.name = diz['name']
        self.special = True if self.name in (
            'verbose', 'overwrite', 'quiet', 'run') else False
        self.description = diz.get('description', None)
        self.default = diz.get('default', None)
        self.guisection = diz.get('guisection', None)
        self.suppress_required = True if 'suppress_required' in diz else False

    def get_bash(self):
        """Return the BASH representation of a flag.

        >>> flag = Flag(diz=dict(name='a', description='Flag description',
        ...                      default=True))
        >>> flag.get_bash()
        u''
        >>> flag.value = True
        >>> flag.get_bash()
        u'-a'
        >>> flag = Flag(diz=dict(name='overwrite'))
        >>> flag.get_bash()
        u''
        >>> flag.value = True
        >>> flag.get_bash()
        u'--o'
        """
        if self.value:
            if self.special:
                return '--%s' % self.name[0]
            else:
                return '-%s' % self.name
        else:
            return ''

    def get_python(self):
        """Return the python representation of a flag.

        >>> flag = Flag(diz=dict(name='a', description='Flag description',
        ...                      default=True))
        >>> flag.get_python()
        u''
        >>> flag.value = True
        >>> flag.get_python()
        u'a'
        >>> flag = Flag(diz=dict(name='overwrite'))
        >>> flag.get_python()
        u''
        >>> flag.value = True
        >>> flag.get_python()
        u'overwrite=True'
        """
        if self.value:
            return '%s=True' % self.name if self.special else self.name
        return ''

    def __str__(self):
        """Return the BASH representation of the flag."""
        return self.get_bash()

    def __repr__(self):
        """Return a string with the python representation of the instance."""
        return "Flag <%s> (%s)" % (self.name, self.description)

    def __bool__(self):
        """Return a boolean value"""
        return self.value

    def __nonzero__(self):
        return self.__bool__()

    @docstring_property(__doc__)
    def __doc__(self):
        """Return a documentation string, something like:

        {name}: {default}, suppress required {supress}
            {description}

        >>>  flag = Flag(diz=dict(name='a', description='Flag description',
        ...                      default=True))
        >>> print(flag.__doc__)
        a: True
            Flag description

        >>> flag = Flag(diz=dict(name='overwrite'))
        >>> print(flag.__doc__)
        overwrite: None
            None

        """
        return read.DOC['flag'].format(name=self.name,
                                       default=repr(self.default),
                                       description=self.description,
                                       supress=('suppress required'
                                                if self.suppress_required
                                                else ''))