tristate.py 3.1 KB
Newer Older
1 2
# vim: set fileencoding=utf-8 :
#
Guido Günther's avatar
Guido Günther committed
3
# (C) 2011 Guido Günther <agx@sigxcpu.org>
4 5 6 7 8 9 10 11 12 13 14
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
15 16
#    along with this program; if not, please see
#    <http://www.gnu.org/licenses/>
17

Guido Günther's avatar
Guido Günther committed
18 19 20 21
"""
A switch with three states: on|off|auto
"""

Guido Günther's avatar
Guido Günther committed
22

23
class Tristate(object):
24
    """Tri-state value: on, off or auto """
Guido Günther's avatar
Guido Günther committed
25
    ON = True       # state is on == do it
26 27 28
    OFF = False     # state is off == don't do it
    AUTO = -1       # autodetect == do if possible

29
    # We accept true as alias for on and false as alias for off
Guido Günther's avatar
Guido Günther committed
30
    _VALID_NAMES = ['on', 'off', 'true', 'false', 'auto']
31 32

    def __init__(self, val):
Guido Günther's avatar
Guido Günther committed
33
        if type(val) in [type(t) for t in (True, 1)]:
34 35 36 37 38 39
            if val > 0:
                self._state = self.ON
            elif val < 0:
                self._state = self.AUTO
            else:
                self._state = self.OFF
40
        elif type(val) is str:
Guido Günther's avatar
Guido Günther committed
41
            if val.lower() in ['on', 'true']:
42
                self._state = self.ON
Guido Günther's avatar
Guido Günther committed
43
            elif val.lower() in ['auto']:
44 45 46
                self._state = self.AUTO
            else:
                self._state = self.OFF
47 48
        elif type(val) is Tristate:
            self._state = val.state
49 50 51 52
        else:
            raise TypeError

    def __repr__(self):
Guido Günther's avatar
Guido Günther committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66
        """
        >>> Tristate('on').__repr__()
        'on'
        >>> Tristate(True).__repr__()
        'on'
        >>> Tristate(False).__repr__()
        'off'
        >>> Tristate('auto').__repr__()
        'auto'
        """
        if self._state == self.ON:
            return 'on'
        elif self._state == self.AUTO:
            return 'auto'
67
        else:
Guido Günther's avatar
Guido Günther committed
68
            return 'off'
69

70 71 72 73 74 75 76 77 78 79 80
    def __nonzero__(self):
        """
        >>> Tristate('on').__nonzero__()
        True
        >>> Tristate('auto').__nonzero__()
        True
        >>> Tristate('off').__nonzero__()
        False
        """
        return self._state is not self.OFF

81 82 83 84 85
    @property
    def state(self):
        """Get current state"""
        return self._state

86 87 88 89 90 91 92 93 94
    def is_auto(self):
        return [False, True][self._state == self.AUTO]

    def is_on(self):
        return [False, True][self._state == self.ON]

    def is_off(self):
        return [False, True][self._state == self.OFF]

95 96 97 98 99 100 101 102 103 104 105 106 107
    def do(self, function, *args, **kwargs):
        """
        Run function if tristate is on or auto, only report a failure if
        tristate is on since failing is o.k. for autodetect.
        """
        if self.is_off():
            return True

        success = function(*args, **kwargs)
        if not success:
            return [True, False][self.is_on()]

        return True