1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Add an abstraction level to transparently import optik classes from optparse
19 (python >= 2.3) or the optik package.
20
21 It also defines three new types for optik/optparse command line parser :
22
23 * regexp
24 argument of this type will be converted using re.compile
25 * csv
26 argument of this type will be converted using split(',')
27 * yn
28 argument of this type will be true if 'y' or 'yes', false if 'n' or 'no'
29 * named
30 argument of this type are in the form <NAME>=<VALUE> or <NAME>:<VALUE>
31 * password
32 argument of this type wont be converted but this is used by other tools
33 such as interactive prompt for configuration to double check value and
34 use an invisible field
35 * multiple_choice
36 same as default "choice" type but multiple choices allowed
37 * file
38 argument of this type wont be converted but checked that the given file exists
39 * color
40 argument of this type wont be converted but checked its either a
41 named color or a color specified using hexadecimal notation (preceded by a #)
42 * time
43 argument of this type will be converted to a float value in seconds
44 according to time units (ms, s, min, h, d)
45 * bytes
46 argument of this type will be converted to a float value in bytes
47 according to byte units (b, kb, mb, gb, tb)
48 """
49 __docformat__ = "restructuredtext en"
50
51 import re
52 import sys
53 import time
54 from copy import copy
55 from os.path import exists
56
57
58 from optparse import OptionParser as BaseParser, Option as BaseOption, \
59 OptionGroup, OptionContainer, OptionValueError, OptionError, \
60 Values, HelpFormatter, NO_DEFAULT, SUPPRESS_HELP
61
62 try:
63 from mx import DateTime
64 HAS_MX_DATETIME = True
65 except ImportError:
66 HAS_MX_DATETIME = False
67
68 from logilab.common.textutils import splitstrip
69
71 """check a regexp value by trying to compile it
72 return the compiled regexp
73 """
74 if hasattr(value, 'pattern'):
75 return value
76 try:
77 return re.compile(value)
78 except ValueError:
79 raise OptionValueError(
80 "option %s: invalid regexp value: %r" % (opt, value))
81
83 """check a csv value by trying to split it
84 return the list of separated values
85 """
86 if isinstance(value, (list, tuple)):
87 return value
88 try:
89 return splitstrip(value)
90 except ValueError:
91 raise OptionValueError(
92 "option %s: invalid csv value: %r" % (opt, value))
93
95 """check a yn value
96 return true for yes and false for no
97 """
98 if isinstance(value, int):
99 return bool(value)
100 if value in ('y', 'yes'):
101 return True
102 if value in ('n', 'no'):
103 return False
104 msg = "option %s: invalid yn value %r, should be in (y, yes, n, no)"
105 raise OptionValueError(msg % (opt, value))
106
108 """check a named value
109 return a dictionary containing (name, value) associations
110 """
111 if isinstance(value, dict):
112 return value
113 values = []
114 for value in check_csv(option, opt, value):
115 if value.find('=') != -1:
116 values.append(value.split('=', 1))
117 elif value.find(':') != -1:
118 values.append(value.split(':', 1))
119 if values:
120 return dict(values)
121 msg = "option %s: invalid named value %r, should be <NAME>=<VALUE> or \
122 <NAME>:<VALUE>"
123 raise OptionValueError(msg % (opt, value))
124
126 """check a password value (can't be empty)
127 """
128
129 return value
130
132 """check a file value
133 return the filepath
134 """
135 if exists(value):
136 return value
137 msg = "option %s: file %r does not exist"
138 raise OptionValueError(msg % (opt, value))
139
140
142 """check a file value
143 return the filepath
144 """
145 try:
146 return DateTime.strptime(value, "%Y/%m/%d")
147 except DateTime.Error :
148 raise OptionValueError(
149 "expected format of %s is yyyy/mm/dd" % opt)
150
152 """check a color value and returns it
153 /!\ does *not* check color labels (like 'red', 'green'), only
154 checks hexadecimal forms
155 """
156
157 if re.match('[a-z0-9 ]+$', value, re.I):
158 return value
159
160 if re.match('#[a-f0-9]{6}', value, re.I):
161 return value
162
163 msg = "option %s: invalid color : %r, should be either hexadecimal \
164 value or predefined color"
165 raise OptionValueError(msg % (opt, value))
166
172
178
179 import types
180
182 """override optik.Option to add some new option types
183 """
184 TYPES = BaseOption.TYPES + ('regexp', 'csv', 'yn', 'named', 'password',
185 'multiple_choice', 'file', 'color',
186 'time', 'bytes')
187 ATTRS = BaseOption.ATTRS + ['hide', 'level']
188 TYPE_CHECKER = copy(BaseOption.TYPE_CHECKER)
189 TYPE_CHECKER['regexp'] = check_regexp
190 TYPE_CHECKER['csv'] = check_csv
191 TYPE_CHECKER['yn'] = check_yn
192 TYPE_CHECKER['named'] = check_named
193 TYPE_CHECKER['multiple_choice'] = check_csv
194 TYPE_CHECKER['file'] = check_file
195 TYPE_CHECKER['color'] = check_color
196 TYPE_CHECKER['password'] = check_password
197 TYPE_CHECKER['time'] = check_time
198 TYPE_CHECKER['bytes'] = check_bytes
199 if HAS_MX_DATETIME:
200 TYPES += ('date',)
201 TYPE_CHECKER['date'] = check_date
202
204 BaseOption.__init__(self, *opts, **attrs)
205 if hasattr(self, "hide") and self.hide:
206 self.help = SUPPRESS_HELP
207
209 """FIXME: need to override this due to optik misdesign"""
210 if self.type in ("choice", "multiple_choice"):
211 if self.choices is None:
212 raise OptionError(
213 "must supply a list of choices for type 'choice'", self)
214 elif type(self.choices) not in (tuple, list):
215 raise OptionError(
216 "choices must be a list of strings ('%s' supplied)"
217 % str(type(self.choices)).split("'")[1], self)
218 elif self.choices is not None:
219 raise OptionError(
220 "must not supply choices for type %r" % self.type, self)
221 BaseOption.CHECK_METHODS[2] = _check_choice
222
223
224 - def process(self, opt, value, values, parser):
225
226
227 value = self.convert_value(opt, value)
228 if self.type == 'named':
229 existant = getattr(values, self.dest)
230 if existant:
231 existant.update(value)
232 value = existant
233
234
235
236 return self.take_action(
237 self.action, self.dest, opt, value, values, parser)
238
239
241 """override optik.OptionParser to use our Option class
242 """
245
265
266
267 OptionGroup.level = 0
268
270 return [option for option in group.option_list
271 if (getattr(option, 'level', 0) or 0) <= outputlevel
272 and not option.help is SUPPRESS_HELP]
273
280 OptionContainer.format_option_help = format_option_help
281
282
379
380 -def generate_manpage(optparser, pkginfo, section=1, stream=sys.stdout, level=0):
381 """generate a man page from an optik parser"""
382 formatter = ManHelpFormatter()
383 formatter.output_level = level
384 formatter.parser = optparser
385 print(formatter.format_head(optparser, pkginfo, section), file=stream)
386 print(optparser.format_option_help(formatter), file=stream)
387 print(formatter.format_tail(pkginfo), file=stream)
388
389
390 __all__ = ('OptionParser', 'Option', 'OptionGroup', 'OptionValueError',
391 'Values')
392