From 0295c3f04ac457081559064ddf965dc507d04553 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 5 Dec 2009 21:35:56 -0500 Subject: Moved becommands.depend._allowed_values() to cmdutil.select_values() I like this code, and I want to use it for other places, e.g. `be list`. Also renamed depend options --limit-severity and --limit-status to --severity and --status --- becommands/depend.py | 59 ++++++++++------------------------------------------ libbe/cmdutil.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 48 deletions(-) diff --git a/becommands/depend.py b/becommands/depend.py index 6cf42eb..d19845d 100644 --- a/becommands/depend.py +++ b/becommands/depend.py @@ -94,10 +94,11 @@ def execute(args, manipulate_encodings=True, restrict_file_access=False): for blockee,blocker in fixed]) return 0 - allowed_status_values = _allowed_values(options.limit_status, - bug.status_values) - allowed_severity_values = _allowed_values(options.limit_severity, - bug.severity_values) + allowed_status_values = \ + cmdutil.select_values(options.limit_status, bug.status_values) + allowed_severity_values = \ + cmdutil.select_values(options.limit_severity, bug.severity_values) + bugA = cmdutil.bug_from_id(bd, args[0]) if options.tree_depth != None: @@ -148,9 +149,9 @@ def get_parser(): parser.add_option("-s", "--show-status", action="store_true", dest="show_status", default=False, help="Show status of blocking bugs") - parser.add_option("--limit-status", dest="limit_status", metavar="STATUS", + parser.add_option("--status", dest="limit_status", metavar="STATUS", help="Only show bugs matching the STATUS specifier") - parser.add_option("--limit-severity", dest="limit_severity", + parser.add_option("--severity", dest="limit_severity", metavar="SEVERITY", help="Only show bugs matching the SEVERITY specifier") parser.add_option("-t", "--tree-depth", metavar="DEPTH", default=None, @@ -168,13 +169,13 @@ If bug B is not specified, just print a list of bugs blocking (A). To search for bugs blocked by a particular bug, try $ be list --extra-strings BLOCKED-BY: -The --limit-* options allow you to either blacklist or whitelist -values, for example - $ be list --limit-status open,assigned +The --status and --severity options allow you to either blacklist or +whitelist values, for example + $ be list --status open,assigned will only follow and print dependencies with open or assigned status. You select blacklist mode by starting the list with a minus sign, for example - $ be list --limit-severity -target + $ be list --severity -target which will only follow and print dependencies with non-target severity. In repair mode, add the missing direction to any one-way links. @@ -189,44 +190,6 @@ def help(): # internal helper functions -def _allowed_values(limit_string, possible_values, name="unkown"): - """ - >>> _allowed_values(None, ['abc', 'def', 'hij']) - ['abc', 'def', 'hij'] - >>> _allowed_values('-abc,hij', ['abc', 'def', 'hij']) - ['def'] - >>> _allowed_values('abc,hij', ['abc', 'def', 'hij']) - ['abc', 'hij'] - >>> _allowed_values('-xyz,hij', ['abc', 'def', 'hij'], name="value") - Traceback (most recent call last): - ... - UserError: Invalid value xyz - ['abc', 'def', 'hij'] - >>> _allowed_values('xyz,hij', ['abc', 'def', 'hij'], name="value") - Traceback (most recent call last): - ... - UserError: Invalid value xyz - ['abc', 'def', 'hij'] - """ - possible_values = list(possible_values) # don't alter the original - if limit_string == None: - pass - elif limit_string.startswith('-'): - blacklisted_values = set(limit_string[1:].split(',')) - for value in blacklisted_values: - if value not in possible_values: - raise cmdutil.UserError('Invalid %s %s\n %s' - % (name, value, possible_values)) - possible_values.remove(value) - else: - whitelisted_values = limit_string.split(',') - for value in whitelisted_values: - if value not in possible_values: - raise cmdutil.UserError('Invalid %s %s\n %s' - % (name, value, possible_values)) - possible_values = whitelisted_values - return possible_values - def _generate_blocks_string(blocked_bug): return "%s%s" % (BLOCKS_TAG, blocked_bug.uuid) diff --git a/libbe/cmdutil.py b/libbe/cmdutil.py index dcd4ca9..b892bde 100644 --- a/libbe/cmdutil.py +++ b/libbe/cmdutil.py @@ -217,6 +217,59 @@ def underlined(instring): return "%s\n%s" % (instring, "="*len(instring)) +def select_values(string, possible_values, name="unkown"): + """ + This function allows the user to select values from a list of + possible values. The default is to select all the values: + + >>> select_values(None, ['abc', 'def', 'hij']) + ['abc', 'def', 'hij'] + + The user selects values with a comma-separated limit_string. + Prepending a minus sign to such a list denotes blacklist mode: + + >>> select_values('-abc,hij', ['abc', 'def', 'hij']) + ['def'] + + Without the leading -, the selection is in whitelist mode: + + >>> select_values('abc,hij', ['abc', 'def', 'hij']) + ['abc', 'hij'] + + In either case, appropriate errors are raised if on of the + user-values is not in the list of possible values. The name + parameter lets you make the error message more clear: + + >>> select_values('-xyz,hij', ['abc', 'def', 'hij'], name="foobar") + Traceback (most recent call last): + ... + UserError: Invalid foobar xyz + ['abc', 'def', 'hij'] + >>> select_values('xyz,hij', ['abc', 'def', 'hij'], name="foobar") + Traceback (most recent call last): + ... + UserError: Invalid foobar xyz + ['abc', 'def', 'hij'] + """ + possible_values = list(possible_values) # don't alter the original + if string == None: + pass + elif string.startswith('-'): + blacklisted_values = set(string[1:].split(',')) + for value in blacklisted_values: + if value not in possible_values: + raise UserError('Invalid %s %s\n %s' + % (name, value, possible_values)) + possible_values.remove(value) + else: + whitelisted_values = string.split(',') + for value in whitelisted_values: + if value not in possible_values: + raise UserError('Invalid %s %s\n %s' + % (name, value, possible_values)) + possible_values = whitelisted_values + return possible_values + def restrict_file_access(bugdir, path): """ Check that the file at path is inside bugdir.root. This is -- cgit