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 from __future__ import print_function
50
51 __docformat__ = "restructuredtext en"
52
53 import re
54 import sys
55 import time
56 from copy import copy
57 from os.path import exists
58
59 from six import integer_types
60
61
62 from optparse import OptionParser as BaseParser, Option as BaseOption, \
63 OptionGroup, OptionContainer, OptionValueError, OptionError, \
64 Values, HelpFormatter, NO_DEFAULT, SUPPRESS_HELP
65
66 try:
67 from mx import DateTime
68 HAS_MX_DATETIME = True
69 except ImportError:
70 HAS_MX_DATETIME = False
71
72 from logilab.common.textutils import splitstrip, TIME_UNITS, BYTE_UNITS, \
73 apply_units
74
75
77 """check a regexp value by trying to compile it
78 return the compiled regexp
79 """
80 if hasattr(value, 'pattern'):
81 return value
82 try:
83 return re.compile(value)
84 except ValueError:
85 raise OptionValueError(
86 "option %s: invalid regexp value: %r" % (opt, value))
87
89 """check a csv value by trying to split it
90 return the list of separated values
91 """
92 if isinstance(value, (list, tuple)):
93 return value
94 try:
95 return splitstrip(value)
96 except ValueError:
97 raise OptionValueError(
98 "option %s: invalid csv value: %r" % (opt, value))
99
101 """check a yn value
102 return true for yes and false for no
103 """
104 if isinstance(value, int):
105 return bool(value)
106 if value in ('y', 'yes'):
107 return True
108 if value in ('n', 'no'):
109 return False
110 msg = "option %s: invalid yn value %r, should be in (y, yes, n, no)"
111 raise OptionValueError(msg % (opt, value))
112
114 """check a named value
115 return a dictionary containing (name, value) associations
116 """
117 if isinstance(value, dict):
118 return value
119 values = []
120 for value in check_csv(option, opt, value):
121 if value.find('=') != -1:
122 values.append(value.split('=', 1))
123 elif value.find(':') != -1:
124 values.append(value.split(':', 1))
125 if values:
126 return dict(values)
127 msg = "option %s: invalid named value %r, should be <NAME>=<VALUE> or \
128 <NAME>:<VALUE>"
129 raise OptionValueError(msg % (opt, value))
130
132 """check a password value (can't be empty)
133 """
134
135 return value
136
138 """check a file value
139 return the filepath
140 """
141 if exists(value):
142 return value
143 msg = "option %s: file %r does not exist"
144 raise OptionValueError(msg % (opt, value))
145
146
148 """check a file value
149 return the filepath
150 """
151 try:
152 return DateTime.strptime(value, "%Y/%m/%d")
153 except DateTime.Error :
154 raise OptionValueError(
155 "expected format of %s is yyyy/mm/dd" % opt)
156
158 """check a color value and returns it
159 /!\ does *not* check color labels (like 'red', 'green'), only
160 checks hexadecimal forms
161 """
162
163 if re.match('[a-z0-9 ]+$', value, re.I):
164 return value
165
166 if re.match('#[a-f0-9]{6}', value, re.I):
167 return value
168
169 msg = "option %s: invalid color : %r, should be either hexadecimal \
170 value or predefined color"
171 raise OptionValueError(msg % (opt, value))
172
174 if isinstance(value, integer_types + (float,)):
175 return value
176 return apply_units(value, TIME_UNITS)
177
182
183
185 """override optik.Option to add some new option types
186 """
187 TYPES = BaseOption.TYPES + ('regexp', 'csv', 'yn', 'named', 'password',
188 'multiple_choice', 'file', 'color',
189 'time', 'bytes')
190 ATTRS = BaseOption.ATTRS + ['hide', 'level']
191 TYPE_CHECKER = copy(BaseOption.TYPE_CHECKER)
192 TYPE_CHECKER['regexp'] = check_regexp
193 TYPE_CHECKER['csv'] = check_csv
194 TYPE_CHECKER['yn'] = check_yn
195 TYPE_CHECKER['named'] = check_named
196 TYPE_CHECKER['multiple_choice'] = check_csv
197 TYPE_CHECKER['file'] = check_file
198 TYPE_CHECKER['color'] = check_color
199 TYPE_CHECKER['password'] = check_password
200 TYPE_CHECKER['time'] = check_time
201 TYPE_CHECKER['bytes'] = check_bytes
202 if HAS_MX_DATETIME:
203 TYPES += ('date',)
204 TYPE_CHECKER['date'] = check_date
205
207 BaseOption.__init__(self, *opts, **attrs)
208 if hasattr(self, "hide") and self.hide:
209 self.help = SUPPRESS_HELP
210
212 """FIXME: need to override this due to optik misdesign"""
213 if self.type in ("choice", "multiple_choice"):
214 if self.choices is None:
215 raise OptionError(
216 "must supply a list of choices for type 'choice'", self)
217 elif not isinstance(self.choices, (tuple, list)):
218 raise OptionError(
219 "choices must be a list of strings ('%s' supplied)"
220 % str(type(self.choices)).split("'")[1], self)
221 elif self.choices is not None:
222 raise OptionError(
223 "must not supply choices for type %r" % self.type, self)
224 BaseOption.CHECK_METHODS[2] = _check_choice
225
226
227 - def process(self, opt, value, values, parser):
228
229
230 value = self.convert_value(opt, value)
231 if self.type == 'named':
232 existant = getattr(values, self.dest)
233 if existant:
234 existant.update(value)
235 value = existant
236
237
238
239 return self.take_action(
240 self.action, self.dest, opt, value, values, parser)
241
242
244 """override optik.OptionParser to use our Option class
245 """
248
268
269
270 OptionGroup.level = 0
271
273 return [option for option in group.option_list
274 if (getattr(option, 'level', 0) or 0) <= outputlevel
275 and not option.help is SUPPRESS_HELP]
276
283 OptionContainer.format_option_help = format_option_help
284
285
382
383 -def generate_manpage(optparser, pkginfo, section=1, stream=sys.stdout, level=0):
384 """generate a man page from an optik parser"""
385 formatter = ManHelpFormatter()
386 formatter.output_level = level
387 formatter.parser = optparser
388 print(formatter.format_head(optparser, pkginfo, section), file=stream)
389 print(optparser.format_option_help(formatter), file=stream)
390 print(formatter.format_tail(pkginfo), file=stream)
391
392
393 __all__ = ('OptionParser', 'Option', 'OptionGroup', 'OptionValueError',
394 'Values')
395