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