Use tristate option for --color=value
authorGuido Günther <agx@sigxcpu.org>
Sat, 8 Jan 2011 12:36:21 +0000 (13:36 +0100)
committerGuido Günther <agx@sigxcpu.org>
Sat, 8 Jan 2011 14:23:16 +0000 (15:23 +0100)
this allows true and false as alias for on and off.

gbp-clone
gbp-pq
gbp-pull
gbp/config.py
gbp/log.py
gbp/tristate.py
git-buildpackage
git-dch
git-import-dsc
git-import-orig

index a2429fe..e75cb2c 100755 (executable)
--- a/gbp-clone
+++ b/gbp-clone
@@ -45,7 +45,7 @@ def main(argv):
 
     parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
                       help="verbose command execution")
-    parser.add_config_file_option(option_name="color", dest="color")
+    parser.add_config_file_option(option_name="color", dest="color", type='tristate')
 
     (options, args) = parser.parse_args(argv)
     gbp.log.setup(options.color, options.verbose)
diff --git a/gbp-pq b/gbp-pq
index 7c35a99..a262398 100755 (executable)
--- a/gbp-pq
+++ b/gbp-pq
@@ -162,7 +162,8 @@ def main(argv):
     parser.add_boolean_config_file_option(option_name="patch-numbers", dest="patch_numbers")
     parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
                       help="verbose command execution")
-    parser.add_config_file_option(option_name="color", dest="color")
+    parser.add_config_file_option(option_name="color", dest="color", type='tristate')
+
 
     (options, args) = parser.parse_args(argv)
     gbp.log.setup(options.color, options.verbose)
index e7d9815..102168d 100755 (executable)
--- a/gbp-pull
+++ b/gbp-pull
@@ -80,7 +80,7 @@ def main(argv):
     branch_group.add_boolean_config_file_option(option_name="pristine-tar", dest="pristine_tar")
     parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
                       help="verbose command execution")
-    parser.add_config_file_option(option_name="color", dest="color")
+    parser.add_config_file_option(option_name="color", dest="color", type='tristate')
 
 
     (options, args) = parser.parse_args(argv)
index c989afe..e623c3b 100644 (file)
@@ -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):
     """
index 07de1cf..86e4943 100644 (file)
@@ -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
index ae47d03..95391b9 100644 (file)
 #    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
index 42049d3..a7a0cec 100755 (executable)
@@ -247,7 +247,7 @@ def main(argv):
     parser.add_boolean_config_file_option(option_name = "ignore-new", dest="ignore_new")
     parser.add_option("--git-verbose", action="store_true", dest="verbose", default=False,
                       help="verbose command execution")
-    parser.add_config_file_option(option_name="color", dest="color")
+    parser.add_config_file_option(option_name="color", dest="color", type='tristate')
     tag_group.add_option("--git-tag", action="store_true", dest="tag", default=False,
                       help="create a tag after a successful build")
     tag_group.add_option("--git-tag-only", action="store_true", dest="tag_only", default=False,
diff --git a/git-dch b/git-dch
index 69c2792..d2271be 100755 (executable)
--- a/git-dch
+++ b/git-dch
@@ -340,7 +340,7 @@ def main(argv):
                       help="options to pass to git-log, default is '%(git-log)s'")
     parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
                       help="verbose command execution")
-    parser.add_config_file_option(option_name="color", dest="color")
+    parser.add_config_file_option(option_name="color", dest="color", type='tristate')
     range_group.add_option("-s", "--since", dest="since", help="commit to start from (e.g. HEAD^^^, debian/0.4.3)")
     range_group.add_option("-a", "--auto", action="store_true", dest="auto", default=False,
                       help="autocomplete changelog from last snapshot or tag")
index ee7a72b..409e345 100755 (executable)
@@ -170,7 +170,7 @@ def main(argv):
 
     parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
                       help="verbose command execution")
-    parser.add_config_file_option(option_name="color", dest="color")
+    parser.add_config_file_option(option_name="color", dest="color", type='tristate')
     parser.add_option("--download", action="store_true", dest="download", default=False,
                       help="download source package")
     branch_group.add_config_file_option(option_name="debian-branch",
index 10d85b1..9a09605 100755 (executable)
@@ -235,7 +235,7 @@ def main(argv):
                                           dest='interactive')
     parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
                       help="verbose command execution")
-    parser.add_config_file_option(option_name="color", dest="color")
+    parser.add_config_file_option(option_name="color", dest="color", type='tristate')
 
     # Accepted for compatibility
     parser.add_option("--no-dch", dest='no_dch', action="store_true",