2 Created on Jun 27, 2011
9 from os.path
import normpath
10 from zipfile
import is_zipfile
14 Base class for the objects used to process the variables.
18 @param env: dictionary with the reference environment to use
26 Return True if this processor can operate on the given variable.
34 @param value: the content of the variable to be processed
35 @return: the processed value
41 return self.
process(variable, value)
45 Base class for processors operating only on lists.
49 Return True if this variable is a list.
51 return isinstance(variable, List)
55 Base class for processors operating only on scalars.
59 Return True if this variable is a scalar.
61 return isinstance(variable, Scalar)
65 Variable processor to expand the reference to environment variables.
68 super(EnvExpander, self).
__init__(env)
69 self.
_exp = re.compile(
r"\$([A-Za-z_][A-Za-z0-9_]*)|\$\(([A-Za-z_][A-Za-z0-9_]*)\)|\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$\{(\.)\}")
72 return (super(EnvExpander, self).
isTarget(variable)
73 and variable.expandVars)
76 m = self._exp.search(value)
79 value = (value[:m.start()]
80 + str(self.
_env[filter(
None, m.groups())[0]])
83 logging.debug(
'KeyError: %s unknown while expanding %s', k, value)
85 return self.
_repl(value)
90 if isinstance(value, str):
91 value = self.
_repl(value)
94 old_values = set(variable.val)
95 value =
map(
lambda v: v
if v
in old_values
else self.
_repl(v), value)
100 Call os.path.normpath for all the entries of the variable.
105 if isinstance(value, str):
106 if '://' not in value:
107 value = normpath(value)
109 value = [normpath(v)
for v
in value
if v]
114 Remove duplicates entries from lists.
125 Remove empty or not existing directories from lists.
128 from os.path
import isdir
129 from os
import listdir
130 return [s
for s
in value
if s.endswith(
'.zip')
or (isdir(s)
and listdir(s))]
134 Use .zip files instead of regular directories in PYTHONPATH when possible.
137 return (super(UsePythonZip, self).
isTarget(variable)
138 and variable.varName ==
'PYTHONPATH')
151 processors = [ EnvExpander, PathNormalizer, DuplicatesRemover,
153 EmptyDirsRemover, UsePythonZip
157 if (
'no-strip-path' in os.environ.get(
'CMTEXTRATAGS',
'')
158 or 'GAUDI_NO_STRIP_PATH' in os.environ
159 or 'LB_NO_STRIP_PATH' in os.environ):
160 processors.remove(EmptyDirsRemover)
162 if 'no-pyzip' in os.environ.get(
'CMTEXTRATAGS',
''):
163 processors.remove(UsePythonZip)
167 Base class for the classes used to manipulate the environment.
174 self.
log = logging.getLogger(
'Variable')
178 Call all the processors defined in the processors list on 'value'.
180 @return: the processed value
182 for p
in [
c(env)
for c
in processors]:
184 value = p(self, value)
189 Class for manipulating with environment lists.
191 It holds its name and values represented by a list.
192 Some operations are done with separator, which is usually colon. For windows use semicolon.
196 super(List, self).
__init__(name, local)
200 '''Returns the name of the List.'''
203 def set(self, value, separator=':
', environment=None):
204 '''Sets the value of the List. Any previous value is overwritten.'''
205 if isinstance(value, str):
206 value = value.split(separator)
209 def unset(self, value, separator=':
', environment=None):# pylint: disable=W0613
210 '''Sets the value of the List to empty. Any previous value is overwritten.'''
213 def value(self, asString=False, separator=':
'):
214 '''Returns values of the List. Either as a list or string with desired separator.'''
216 return separator.join(self.
val)
219 return list(self.
val)
222 self.remove(value, separator, True)
224 def remove(self, value, separator=':
', regexp=False):
225 '''Removes value(s) from List. If value is not found, removal is canceled.'''
227 value = self.
search(value,
True)
229 elif isinstance(value,str):
230 value = value.split(separator)
232 for i
in range(len(value)):
235 self.log.info(
'Value "%s" not found in List: "%s". Removal canceled.', val, self.
varName)
236 while val
in self.
val:
240 def append(self, value, separator=':
', environment=None):
241 '''Adds value(s) at the end of the list.'''
242 if isinstance(value, str):
243 value = value.split(separator)
246 def prepend(self, value, separator=':
', environment=None):
247 '''Adds value(s) at the beginning of the list.
248 resolve references and duplications'''
249 if isinstance(value, str):
250 value = value.split(separator)
254 '''Searches in List's values for a match
256 Use string value or set regExp to True.
257 In the first case search is done only for an exact match for one of List`s value ('^' and '$' added).
260 expr =
'^' + expr +
'$'
273 if value
in self.
val:
274 self.log.info(
'Var: "%s" value: "%s". Addition canceled because of duplicate entry.', self.
varName, value)
276 self.val.insert(key, value)
286 return item
in self.
val
292 return ':'.join(self.
val)
296 '''Class for manipulating with environment scalars.'''
299 super(Scalar, self).
__init__(name, local)
303 '''Returns the name of the scalar.'''
306 def set(self, value, separator=':
', environment=None):# pylint: disable=W0613
307 '''Sets the value of the scalar. Any previous value is overwritten.'''
310 def unset(self, value, separator=':
', environment=None):# pylint: disable=W0613
311 '''Sets the value of the variable to empty. Any previous value is overwritten.'''
314 def value(self, asString=False, separator=':
'):# pylint: disable=W0613
315 '''Returns values of the scalar.'''
319 self.remove(value, separator, True)
321 def remove(self, value, separator=':
', regexp=True):# pylint: disable=W0613
322 '''Removes value(s) from the scalar. If value is not found, removal is canceled.'''
323 value = self.
search(value)
325 self.
val = self.val.replace(val,
'')
327 def append(self, value, separator=':
', environment=None):# pylint: disable=W0613
328 '''Adds value(s) at the end of the scalar.'''
331 def prepend(self, value, separator=':
', environment=None):# pylint: disable=W0613
332 '''Adds value(s) at the beginning of the scalar.'''
336 '''Searches in scalar`s values for a match'''
337 return re.findall(expr, self.
val)
343 '''Class which defines errors for locals operations.'''
349 if self.
code ==
'undefined':
350 return 'Reference to undefined environment element: "'+self.
val +
'".'
351 elif self.
code ==
'ref2var':
352 return 'Reference to list from the middle of string.'
353 elif self.
code ==
'redeclaration':
354 return 'Wrong redeclaration of environment element "'+self.
val+
'".'
def __init__(self, value, code)
def isTarget(self, variable)
def __contains__(self, item)
def __delitem__(self, key)
def isTarget(self, variable)
def isTarget(self, variable)
def isTarget(self, variable)
def __setitem__(self, key, value)
struct GAUDI_API map
Parametrisation class for map-like implementation.
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range form arbitrary container
def __getitem__(self, key)
def process(self, variable, value)
def process(self, variable, value)
def search(self, expr, regExp)
def process(self, value, env)
def process(self, variable, value)
def __call__(self, variable, value)
def isTarget(self, variable)
def process(self, variable, value)
def process(self, variable, value)
def process(self, variable, value)