setup.py: make libbe._version optional.
[be.git] / libbe / command / assign.py
1 # Copyright (C) 2005-2012 Aaron Bentley <abentley@panoramicfeedback.com>
2 #                         Chris Ball <cjb@laptop.org>
3 #                         Gianluca Montecchi <gian@grys.it>
4 #                         Marien Zwart <marien.zwart@gmail.com>
5 #                         Robert Lehmann <mail@robertlehmann.de>
6 #                         Thomas Gerigk <tgerigk@gmx.de>
7 #                         W. Trevor King <wking@tremily.us>
8 #
9 # This file is part of Bugs Everywhere.
10 #
11 # Bugs Everywhere is free software: you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by the Free
13 # Software Foundation, either version 2 of the License, or (at your option) any
14 # later version.
15 #
16 # Bugs Everywhere is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19 # more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Bugs Everywhere.  If not, see <http://www.gnu.org/licenses/>.
23
24 import libbe
25 import libbe.command
26 import libbe.command.util
27
28
29 class Assign (libbe.command.Command):
30     u"""Assign an individual or group to fix a bug
31
32     >>> import sys
33     >>> import libbe.bugdir
34     >>> bd = libbe.bugdir.SimpleBugDir(memory=False)
35     >>> io = libbe.command.StringInputOutput()
36     >>> io.stdout = sys.stdout
37     >>> ui = libbe.command.UserInterface(io=io)
38     >>> ui.storage_callbacks.set_storage(bd.storage)
39     >>> cmd = Assign(ui=ui)
40
41     >>> bd.bug_from_uuid('a').assigned is None
42     True
43     >>> ui._user_id = u'Fran\xe7ois'
44     >>> ret = ui.run(cmd, args=['-', '/a'])
45     >>> bd.flush_reload()
46     >>> bd.bug_from_uuid('a').assigned
47     u'Fran\\xe7ois'
48
49     >>> ret = ui.run(cmd, args=['someone', '/a', '/b'])
50     >>> bd.flush_reload()
51     >>> bd.bug_from_uuid('a').assigned
52     'someone'
53     >>> bd.bug_from_uuid('b').assigned
54     'someone'
55
56     >>> ret = ui.run(cmd, args=['none', '/a'])
57     >>> bd.flush_reload()
58     >>> bd.bug_from_uuid('a').assigned is None
59     True
60     >>> ui.cleanup()
61     >>> bd.cleanup()
62     """
63     name = 'assign'
64
65     def __init__(self, *args, **kwargs):
66         libbe.command.Command.__init__(self, *args, **kwargs)
67         self.args.extend([
68                 libbe.command.Argument(
69                     name='assigned', metavar='ASSIGNED', default=None,
70                     completion_callback=libbe.command.util.complete_assigned),
71                 libbe.command.Argument(
72                     name='bug-id', metavar='BUG-ID', default=None,
73                     repeatable=True,
74                     completion_callback=libbe.command.util.complete_bug_id),
75                 ])
76
77     def _run(self, **params):
78         assigned = parse_assigned(self, params['assigned'])
79         bugdirs = self._get_bugdirs()
80         for bug_id in params['bug-id']:
81             bugdir,bug,comment = (
82                 libbe.command.util.bugdir_bug_comment_from_user_id(
83                     bugdirs, bug_id))
84             if bug.assigned != assigned:
85                 bug.assigned = assigned
86                 if bug.status == 'open':
87                     bug.status = 'assigned'
88         return 0
89
90     def _long_help(self):
91         return """
92 Assign a person to fix a bug.
93
94 Assigneds should be the person's Bugs Everywhere identity, the same
95 string that appears in Creator fields.
96
97 Special assigned strings:
98   "-"      assign the bug to yourself
99   "none"   un-assigns the bug
100 """
101
102 def parse_assigned(command, assigned):
103     """Standard processing for the 'assigned' Argument.
104     """
105     if assigned == 'none':
106         assigned = None
107     elif assigned == '-':
108         assigned = command._get_user_id()
109     return assigned