Merge branch 'master' of wking.cfbe
[be.git] / libbe / command / severity.py
1 # Copyright (C) 2005-2010 Aaron Bentley <abentley@panoramicfeedback.com>
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 file is part of Bugs Everywhere.
8 #
9 # Bugs Everywhere is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by the
11 # Free Software Foundation, either version 2 of the License, or (at your
12 # option) any later version.
13 #
14 # Bugs Everywhere is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Bugs Everywhere.  If not, see <http://www.gnu.org/licenses/>.
21
22 import libbe
23 import libbe.bug
24 import libbe.command
25 import libbe.command.util
26
27
28 class Severity (libbe.command.Command):
29     """Change a bug's severity level
30
31     >>> import sys
32     >>> import libbe.bugdir
33     >>> bd = libbe.bugdir.SimpleBugDir(memory=False)
34     >>> io = libbe.command.StringInputOutput()
35     >>> io.stdout = sys.stdout
36     >>> ui = libbe.command.UserInterface(io=io)
37     >>> ui.storage_callbacks.set_bugdir(bd)
38     >>> cmd = Severity(ui=ui)
39
40     >>> bd.bug_from_uuid('a').severity
41     'minor'
42     >>> ret = ui.run(cmd, args=['wishlist', '/a'])
43     >>> bd.flush_reload()
44     >>> bd.bug_from_uuid('a').severity
45     'wishlist'
46     >>> ret = ui.run(cmd, args=['none', '/a'])
47     Traceback (most recent call last):
48     UserError: Invalid severity level: none
49     >>> ui.cleanup()
50     >>> bd.cleanup()
51     """
52     name = 'severity'
53
54     def __init__(self, *args, **kwargs):
55         libbe.command.Command.__init__(self, *args, **kwargs)
56         self.args.extend([
57                 libbe.command.Argument(
58                     name='severity', metavar='SEVERITY', default=None,
59                     completion_callback=libbe.command.util.complete_severity),
60                 libbe.command.Argument(
61                     name='bug-id', metavar='BUG-ID', default=None,
62                     repeatable=True,
63                     completion_callback=libbe.command.util.complete_bug_id),
64                 ])
65
66     def _run(self, **params):
67         bugdir = self._get_bugdir()
68         for bug_id in params['bug-id']:
69             bug,dummy_comment = \
70                 libbe.command.util.bug_comment_from_user_id(bugdir, bug_id)
71             if bug.severity != params['severity']:
72                 try:
73                     bug.severity = params['severity']
74                 except ValueError, e:
75                     if e.name != 'severity':
76                         raise e
77                     raise libbe.command.UserError(
78                         'Invalid severity level: %s' % e.value)
79         return 0
80
81     def _long_help(self):
82         ret = ["""
83 Show or change a bug's severity level.
84
85 If no severity is specified, the current value is printed.  If a severity level
86 is specified, it will be assigned to the bug.
87
88 Severity levels are:
89 """]
90         try: # See if there are any per-tree severity configurations
91             bd = self._get_bugdir()
92         except NotImplementedError:
93             pass # No tree, just show the defaults
94         longest_severity_len = max([len(s) for s in libbe.bug.severity_values])
95         for severity in libbe.bug.severity_values :
96             description = libbe.bug.severity_description[severity]
97             ret.append('%*s : %s\n' \
98                 % (longest_severity_len, severity, description))
99         return ''.join(ret)