Broke `be comment --xml` out and extended into `be import-xml`.
[be.git] / becommands / severity.py
1 # Copyright (C) 2005-2009 Aaron Bentley and Panometrics, Inc.
2 #                         Gianluca Montecchi <gian@grys.it>
3 #                         Marien Zwart <marienz@gentoo.org>
4 #                         Thomas Gerigk <tgerigk@gmx.de>
5 #                         W. Trevor King <wking@drexel.edu>
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 along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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, manipulate_encodings=True):
25     """
26     >>> import os
27     >>> bd = bugdir.SimpleBugDir()
28     >>> os.chdir(bd.root)
29     >>> execute(["a"], manipulate_encodings=False)
30     minor
31     >>> execute(["a", "wishlist"], manipulate_encodings=False)
32     >>> execute(["a"], manipulate_encodings=False)
33     wishlist
34     >>> execute(["a", "none"], manipulate_encodings=False)
35     Traceback (most recent call last):
36     UserError: Invalid severity level: none
37     >>> bd.cleanup()
38     """
39     parser = get_parser()
40     options, args = parser.parse_args(args)
41     complete(options, args, parser)
42     if len(args) not in (1,2):
43         raise cmdutil.UsageError
44     bd = bugdir.BugDir(from_disk=True,
45                        manipulate_encodings=manipulate_encodings)
46     bug = cmdutil.bug_from_id(bd, args[0])
47     if len(args) == 1:
48         print bug.severity
49     elif len(args) == 2:
50         try:
51             bug.severity = args[1]
52         except ValueError, e:
53             if e.name != "severity":
54                 raise e
55             raise cmdutil.UserError ("Invalid severity level: %s" % e.value)
56
57 def get_parser():
58     parser = cmdutil.CmdOptionParser("be severity BUG-ID [SEVERITY]")
59     return parser
60
61 def help():
62     longhelp=["""
63 Show or change a bug's severity level.
64
65 If no severity is specified, the current value is printed.  If a severity level
66 is specified, it will be assigned to the bug.
67
68 Severity levels are:
69 """]
70     try: # See if there are any per-tree severity configurations
71         bd = bugdir.BugDir(from_disk=True, manipulate_encodings=False)
72     except bugdir.NoBugDir, e:
73         pass # No tree, just show the defaults
74     longest_severity_len = max([len(s) for s in bug.severity_values])
75     for severity in bug.severity_values :
76         description = bug.severity_description[severity]
77         s = "%*s : %s\n" % (longest_severity_len, severity, description)
78         longhelp.append(s)
79     longhelp = ''.join(longhelp)
80     return get_parser().help_str() + longhelp
81
82 def complete(options, args, parser):
83     for option,value in cmdutil.option_value_pairs(options, parser):
84         if value == "--complete":
85             # no argument-options at the moment, so this is future-proofing
86             raise cmdutil.GetCompletions()
87     for pos,value in enumerate(args):
88         if value == "--complete":
89             try: # See if there are any per-tree severity configurations
90                 bd = bugdir.BugDir(from_disk=True,
91                                    manipulate_encodings=False)
92             except bugdir.NoBugDir:
93                 bd = None
94             if pos == 0: # fist positional argument is a bug id 
95                 ids = []
96                 if bd != None:
97                     bd.load_all_bugs()
98                     filter = lambda bg : bg.active==True
99                     bugs = [bg for bg in bd if filter(bg)==True]
100                     ids = [bd.bug_shortname(bg) for bg in bugs]
101                 raise cmdutil.GetCompletions(ids)
102             elif pos == 1: # second positional argument is a severity
103                 raise cmdutil.GetCompletions(bug.severity_values)
104             raise cmdutil.GetCompletions()