becommands/severity and status now handle --complete appropriately.
[be.git] / becommands / severity.py
1 # Copyright (C) 2005 Aaron Bentley and Panometrics, Inc.
2 # <abentley@panoramicfeedback.com>
3 #
4 #    This program is free software; you can redistribute it and/or modify
5 #    it under the terms of the GNU General Public License as published by
6 #    the Free Software Foundation; either version 2 of the License, or
7 #    (at your option) any later version.
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program; if not, write to the Free Software
16 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 """Show or change a bug's severity level"""
18 from libbe import cmdutil, bugdir, bug
19 __desc__ = __doc__
20
21 def execute(args, test=False):
22     """
23     >>> import os
24     >>> bd = bugdir.simple_bug_dir()
25     >>> os.chdir(bd.root)
26     >>> execute(["a"], test=True)
27     minor
28     >>> execute(["a", "wishlist"], test=True)
29     >>> execute(["a"], test=True)
30     wishlist
31     >>> execute(["a", "none"], test=True)
32     Traceback (most recent call last):
33     UserError: Invalid severity level: none
34     """
35     parser = get_parser()
36     options, args = parser.parse_args(args)
37     complete(options, args, parser)
38     if len(args) not in (1,2):
39         raise cmdutil.UsageError
40     bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test)
41     bug = bd.bug_from_shortname(args[0])
42     if len(args) == 1:
43         print bug.severity
44     elif len(args) == 2:
45         try:
46             bug.severity = args[1]
47         except ValueError, e:
48             if e.name != "severity":
49                 raise e
50             raise cmdutil.UserError ("Invalid severity level: %s" % e.value)
51         bd.save()
52
53 def get_parser():
54     parser = cmdutil.CmdOptionParser("be severity BUG-ID [SEVERITY]")
55     return parser
56
57 def help():
58     longhelp=["""
59 Show or change a bug's severity level.
60
61 If no severity is specified, the current value is printed.  If a severity level
62 is specified, it will be assigned to the bug.
63
64 Severity levels are:
65 """]
66     try: # See if there are any per-tree severity configurations
67         bd = bugdir.BugDir(from_disk=True, manipulate_encodings=False)
68     except bugdir.NoBugDir, e:
69         pass # No tree, just show the defaults
70     longest_severity_len = max([len(s) for s in bug.severity_values])
71     for severity in bug.severity_values :
72         description = bug.severity_description[severity]
73         s = "%*s : %s\n" % (longest_severity_len, severity, description)
74         longhelp.append(s)
75     longhelp = ''.join(longhelp)
76     return get_parser().help_str() + longhelp
77
78 def complete(options, args, parser):
79     for option,value in cmdutil.option_value_pairs(options, parser):
80         if value == "--complete":
81             # no argument-options at the moment, so this is future-proofing
82             raise cmdutil.GetCompletions()
83     for pos,value in enumerate(args):
84         if value == "--complete":
85             try: # See if there are any per-tree severity configurations
86                 bd = bugdir.BugDir(from_disk=True,
87                                    manipulate_encodings=False)
88             except bugdir.NoBugDir:
89                 bd = None
90             if pos == 0: # fist positional argument is a bug id 
91                 ids = []
92                 if bd != None:
93                     bd.load_all_bugs()
94                     filter = lambda bg : bg.active==True
95                     bugs = [bg for bg in bd if filter(bg)==True]
96                     ids = [bd.bug_shortname(bg) for bg in bugs]
97                 raise cmdutil.GetCompletions(ids)
98             elif pos == 1: # second positional argument is a severity
99                 raise cmdutil.GetCompletions(bug.severity_values)
100             raise cmdutil.GetCompletions()