/usr/share/pyshared/plasTeX/ConfigManager/Multi.py is in python-plastex 0.9.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | import os
from Generic import *
from plasTeX.ConfigManager import TooManyValues
from String import StringOption
from UserList import UserList
class MultiParser(GenericParser):
def getArgument(self, args, range=None, delim=None, forcedarg=False):
if range is None:
range = self.range[:]
if delim is None:
delim = self.delim
new_args, args = GenericParser.getArgument(self, args, range, delim, forcedarg=forcedarg)
if type(new_args) in [list, tuple]:
return new_args, args
elif new_args is None:
return [], args
return [new_args], args
class MultiOption(MultiParser, GenericOption, UserList):
"""
Multiple configuration option
Multi options are options delimited by a specified character. They
can also be represented by an option specified multiple times.
All other options, when specified more than once, will overwrite
their previous value. Multi options will append values each
time an option is specified.
"""
synopsis = 'val1,val2,...'
def __init__(self, docstring=DEFAULTS['docstring'],
options=DEFAULTS['options'],
default=[],
optional=DEFAULTS['optional'],
values=DEFAULTS['values'],
category=DEFAULTS['category'],
callback=DEFAULTS['callback'],
synopsis=DEFAULTS['synopsis'],
environ=DEFAULTS['environ'],
registry=DEFAULTS['registry'],
delim=',',
range=[1,'*'],
mandatory=None,
name=DEFAULTS['name'],
source=DEFAULTS['source'],
template=StringOption):
"""
Initialize a multi option
This class is initialized with the same options as the
Option class with one addition: delim. The 'delim' argument
specifies what the delimiter is for each item in the list.
If the delimiter is 'None' or whitespace, each item in the
list is assumed to be delimited by whitespace.
"""
self.delim = delim
self.range = range
assert not issubclass(template, MultiOption), \
'MultiOptions can not have a MultiOption as a template'
assert issubclass(template, GenericOption), \
'Templates must be a subclass of GenericOption'
self.template = template(options=options,name=name,values=values)
UserList.__init__(self, [])
GenericOption.initialize(self, locals())
def cast(self, arg):
if arg is None: return []
if type(arg) in [list,tuple]:
return [self.template.cast(x) for x in list(arg)]
delim = self.delim
if not delim:
delim = ' '
return [self.template.cast(v.strip()) for v in str(arg).split(delim)
if v.strip()]
def getValue(self, value=None):
""" Return value for option """
if self.data and self.source & COMMANDLINE:
return self.data
if self.environ and os.environ.has_key(str(self.environ)):
self.source = ENVIRONMENT
self.file = None
return self.cast(os.environ[str(self.environ)])
if self.data:
return self.data
if self.default:
self.source = BUILTIN
self.file = None
return self.default
self.source = CODE
self.file = None
if value is None:
return []
return value
def clearValue(self):
""" Clear the value to be unset """
self.data = []
def __repr__(self):
""" Print command-line representation """
delim = self.delim
if not delim:
delim = ' '
if self.data:
option = self.actual
if not option:
args = self.getPossibleOptions()
if args: option = args[0]
if option:
return str('%s %s' % (option, delim.join(self.data))).strip()
return ''
def __iadd__(self, other):
""" Append a value to the list """
if callable(self.callback):
other = self.callback(self.cast(other))
self.data += self.validate(other)
range = self.validateRange(self.range)
name = self.name
if self.actual: name = self.actual
if len(self.data) > range[1]:
raise TooManyValues, "Expecting at most %s values for option '%s'." % (range[1], name)
return self
def validate(self, arg):
""" Validate the value of the option """
new_values = []
for i in self.cast(arg):
# new_values.append(self.checkValues(i))
new_values.append(self.template.validate(i))
return new_values
def checkValues(self, value):
return self.template.checkValues(value)
def __str__(self):
if self.delim and self.delim.strip():
delim = '%s ' % self.delim
return delim.join([str(x) for x in self.data])
else:
return '\n'.join([str(x) for x in self.data])
return str(self.data)
def acceptsArgument(self):
""" Return a boolean indicating if the option accepts an argument """
range = self.validateRange(self.range)
return not(not(range[1]))
def requiresArgument(self):
""" Return a boolean indicating if the option requires an argument """
range = self.validateRange(self.range)
return not(not(range[0]))
def setValue(self, value):
"""
Set the value of the option to the given value
Once the value is set to the new value and validated, the
specified callback function is called with that value as its
only argument.
"""
if value is None or ((type(value) in [list,tuple]) and not(value)):
self.clearValue()
else:
if callable(self.callback):
value = self.callback(self.cast(value))
self.data = self.validate(value)
class MultiArgument(GenericArgument, MultiOption):
""" Multiple command-line option """
def __init__(self, docstring=DEFAULTS['docstring'],
options=DEFAULTS['options'],
default=[],
optional=DEFAULTS['optional'],
values=DEFAULTS['values'],
category=DEFAULTS['category'],
callback=DEFAULTS['callback'],
synopsis=DEFAULTS['synopsis'],
environ=DEFAULTS['environ'],
registry=DEFAULTS['registry'],
delim=' ',
range=[1,'*'],
mandatory=None,
name=DEFAULTS['name'],
source=DEFAULTS['source'],
template=StringOption):
""" Initialize a multi argument """
self.delim = delim
self.range = range
assert not issubclass(template, MultiArgument), \
'MultiArguments can not have a MultiArguments as a template'
assert not issubclass(template, MultiOption), \
'MultiOptions can not have a MultiOptions as a template'
assert issubclass(template, GenericOption), \
'Templates must be a subclass of GenericOption'
self.template = template(options=options,name=name,values=values)
UserList.__init__(self, [])
GenericOption.initialize(self, locals())
|