Merged with head branch
[be.git] / becommands / set.py
1 # Copyright (C) 2005-2009 Aaron Bentley and Panometrics, Inc.
2 #                         Chris Ball <cjb@laptop.org>
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 """Change tree settings"""
21 import textwrap
22 from libbe import cmdutil, bugdir, rcs, settings_object
23 __desc__ = __doc__
24
25 def _value_string(bd, setting):
26     val = bd.settings.get(setting, settings_object.EMPTY)
27     if val == settings_object.EMPTY:
28         default = getattr(bd, bd._setting_name_to_attr_name(setting))
29         if default not in [None, settings_object.EMPTY]:
30             val = "None (%s)" % default
31         else:
32             val = None
33     return str(val)
34
35 def execute(args, test=False):
36     """
37     >>> import os
38     >>> bd = bugdir.simple_bug_dir()
39     >>> os.chdir(bd.root)
40     >>> execute(["target"], test=True)
41     None
42     >>> execute(["target", "tomorrow"], test=True)
43     >>> execute(["target"], test=True)
44     tomorrow
45     >>> execute(["target", "none"], test=True)
46     >>> execute(["target"], test=True)
47     None
48     """
49     parser = get_parser()
50     options, args = parser.parse_args(args)
51     complete(options, args, parser)
52     if len(args) > 2:
53         raise cmdutil.UsageError, "Too many arguments"
54     bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test)
55     if len(args) == 0:
56         keys = bd.settings_properties
57         keys.sort()
58         for key in keys:
59             print "%16s: %s" % (key, _value_string(bd, key))
60     elif len(args) == 1:
61         print _value_string(bd, args[0])
62     else:
63         if args[1] == "none":
64             setattr(bd, args[0], settings_object.EMPTY)
65         else:
66             if args[0] not in bd.settings_properties:
67                 msg = "Invalid setting %s\n" % args[0]
68                 msg += 'Allowed settings:\n  '
69                 msg += '\n  '.join(bd.settings_properties)
70                 raise cmdutil.UserError(msg)
71             old_setting = bd.settings.get(args[0])
72             setattr(bd, args[0], args[1])
73
74 def get_parser():
75     parser = cmdutil.CmdOptionParser("be set [NAME] [VALUE]")
76     return parser
77
78 def get_bugdir_settings():
79     settings = []
80     for s in bugdir.BugDir.settings_properties:
81         settings.append(s)
82     settings.sort()
83     documented_settings = []
84     for s in settings:
85         set = getattr(bugdir.BugDir, s)
86         dstr = set.__doc__.strip()
87         # per-setting comment adjustments
88         if s == "rcs_name":
89             lines = dstr.split('\n')
90             while lines[0].startswith("This property defaults to") == False:
91                 lines.pop(0)
92             assert len(lines) != None, \
93                 "Unexpected rcs_name docstring:\n  '%s'" % dstr
94             lines.insert(
95                 0, "The name of the revision control system to use.\n")
96             dstr = '\n'.join(lines)
97         doc = textwrap.wrap(dstr, width=70, initial_indent='  ',
98                             subsequent_indent='  ')
99         documented_settings.append("%s\n%s" % (s, '\n'.join(doc)))
100     return documented_settings
101
102 longhelp="""
103 Show or change per-tree settings. 
104
105 If name and value are supplied, the name is set to a new value.
106 If no value is specified, the current value is printed.
107 If no arguments are provided, all names and values are listed. 
108
109 To unset a setting, set it to "none".
110
111 Allowed settings are:
112
113 %s""" % ('\n'.join(get_bugdir_settings()),)
114
115 def help():
116     return get_parser().help_str() + longhelp
117
118 def complete(options, args, parser):
119     for option, value in cmdutil.option_value_pairs(options, parser):
120         if value == "--complete":
121             # no argument-options at the moment, so this is future-proofing
122             raise cmdutil.GetCompletions()
123     for pos,value in enumerate(args):
124         if value == "--complete":
125             if pos == 0: # first positional argument is a setting name
126                 props = bugdir.BugDir.settings_properties
127                 raise cmdutil.GetCompletions(props)
128             raise cmdutil.GetCompletions() # no positional arguments for list