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