aboutsummaryrefslogtreecommitdiffhomepage
path: root/gbp
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2011-01-08 13:36:21 +0100
committerGuido Günther <agx@sigxcpu.org>2011-01-08 15:23:16 +0100
commit79ed2e0f088d0b374f5dbed12051770236268238 (patch)
tree96b8cb9e94425115759aaa97f324c2ac0c5c4a2b /gbp
parentfc1d47d222e76e1c89b8c022f7f285bddbc17f96 (diff)
Use tristate option for --color=value
this allows true and false as alias for on and off.
Diffstat (limited to 'gbp')
-rw-r--r--gbp/config.py15
-rw-r--r--gbp/log.py5
-rw-r--r--gbp/tristate.py22
3 files changed, 34 insertions, 8 deletions
diff --git a/gbp/config.py b/gbp/config.py
index c989afea..e623c3b2 100644
--- a/gbp/config.py
+++ b/gbp/config.py
@@ -3,7 +3,7 @@
# (C) 2006,2007,2010 Guido Guenther <agx@sigxcpu.org>
"""handles command line and config file option parsing for the gbp commands"""
-from optparse import OptionParser, OptionGroup, Option
+from optparse import OptionParser, OptionGroup, Option, OptionValueError
from ConfigParser import SafeConfigParser
from copy import copy
import os.path
@@ -11,15 +11,26 @@ try:
from gbp.gbp_version import gbp_version
except ImportError:
gbp_version = "[Unknown version]"
+import gbp.tristate
def expand_path(option, opt, value):
value = os.path.expandvars(value)
return os.path.expanduser(value)
+def check_tristate(option, opt, value):
+ try:
+ val = gbp.tristate.Tristate(value)
+ except TypeError:
+ raise OptionValueError(
+ "option %s: invalid value: %r" % (opt, value))
+ else:
+ return val
+
class GbpOption(Option):
- TYPES = Option.TYPES + ('path',)
+ TYPES = Option.TYPES + ('path', 'tristate')
TYPE_CHECKER = copy(Option.TYPE_CHECKER)
TYPE_CHECKER['path'] = expand_path
+ TYPE_CHECKER['tristate'] = check_tristate
class GbpOptionParser(OptionParser):
"""
diff --git a/gbp/log.py b/gbp/log.py
index 07de1cf0..86e4943c 100644
--- a/gbp/log.py
+++ b/gbp/log.py
@@ -18,6 +18,7 @@
"""Simple colored logging classes"""
import sys
+import gbp.tristate
logger = None
@@ -53,9 +54,9 @@ class Logger(object):
if type(color) == type(True):
self.color = color
else:
- if color.lower() == "on":
+ if color.is_on():
self.color = True
- elif color.lower() == "auto":
+ elif color.is_auto():
if (sys.stderr.isatty() and
sys.stdout.isatty()):
self.color = True
diff --git a/gbp/tristate.py b/gbp/tristate.py
index ae47d03f..95391b90 100644
--- a/gbp/tristate.py
+++ b/gbp/tristate.py
@@ -16,10 +16,11 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
class Tristate(object):
- """Tri-state value: on, off or auto"""
- AUTO = -1
- ON = True
- OFF = False
+ """Tri-state value: on, off or auto """
+ ON = True # state is on == do it
+ OFF = False # state is off == don't do it
+ AUTO = -1 # autodetect == do if possible
+
# We accept true as alias for on and false as alias for off
_VALID_NAMES = [ 'on', 'off', 'true', 'false', 'auto' ]
@@ -63,3 +64,16 @@ class Tristate(object):
def is_off(self):
return [False, True][self._state == self.OFF]
+ 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