11 from __future__
import absolute_import
16 from collections.abc
import MutableSequence, MutableMapping
19 from collections
import MutableSequence, MutableMapping
21 if sys.version_info >= (3, ):
24 _log = logging.getLogger(__name__)
25 is_64bits = sys.maxsize > 2**32
30 Basic property semantics implementation, with no validation/transformation.
32 __handled_types__ = (re.compile(
r'.*'), )
45 h.match(value)
if hasattr(h,
'match')
else h == value
47 raise TypeError(
'C++ type {!r} not supported'.
format(value))
52 Transformation for data when reading the property.
58 Validation/transformation of the data to be stored.
64 Allow overriding the definition of "is set" if we need helper types.
70 Option string version of value.
72 if hasattr(value,
'__opt_value__'):
73 return value.__opt_value__()
80 Used when merging two Configurable instances, by default just ensure
81 the two values do not conflict, but it can be overridden in
82 derived semantics to, for example, append to the two lists.
85 raise ValueError(
'cannot merge values %r and %r' % (a, b))
89 DefaultSemantics = PropertySemantics
93 __handled_types__ = (
'std::string', )
96 if not isinstance(value, basestring):
97 raise ValueError(
'cannot set property {} to {!r}'.
format(
103 __handled_types__ = (
'bool', )
110 __handled_types__ = (
'float',
'double')
113 from numbers
import Number
114 if not isinstance(value, Number):
116 'number expected, got {!r} in assignemnt to {}'.
format(
124 'signed char': (-128, 127),
125 'short': (-32768, 32767),
126 'int': (-2147483648, 2147483647),
127 'long': ((-9223372036854775808, 9223372036854775807)
if is_64bits
else
128 (-2147483648, 2147483647)),
129 'long long': (-9223372036854775808, 9223372036854775807),
130 'unsigned char': (0, 255),
131 'unsigned short': (0, 65535),
132 'unsigned int': (0, 4294967295),
134 18446744073709551615
if is_64bits
else 4294967295),
135 'unsigned long long': (0, 18446744073709551615),
138 __handled_types__ = tuple(INT_RANGES)
141 from numbers
import Number
142 if not isinstance(value, Number):
144 'number expected, got {!r} in assignemnt to {}'.
format(
148 _log.warning(
'converted %s to %d in assignment to %s', value, v,
151 if v < min_value
or v > max_value:
152 raise ValueError(
'value {} outside limits for {!r} {}'.
format(
157 _IDENTIFIER_RE =
r'[a-zA-Z_][a-zA-Z0-9_]*'
158 _NS_IDENT_RE =
r'{ident}(::{ident})*'.
format(ident=_IDENTIFIER_RE)
159 _COMMA_SEPARATION_RE =
r'{exp}(,{exp})*'
163 __handled_types__ = (
'Algorithm',
'Auditor',
164 re.compile(
r'AlgTool(:{})?$'.
format(
165 _COMMA_SEPARATION_RE.format(exp=_NS_IDENT_RE))),
166 re.compile(
r'Service(:{})?$'.
format(
167 _COMMA_SEPARATION_RE.format(exp=_NS_IDENT_RE))))
170 super(ComponentSemantics, self).
__init__(cpp_type, name)
179 from .
import Configurable, Configurables
180 if isinstance(value, Configurable):
182 elif isinstance(value, basestring):
184 if value
in Configurable.instances:
185 value = Configurable.instances[value]
189 t, n = value.split(
'/')
192 value = Configurables.getByType(t).getInstance(n)
195 'cannot assign {!r} to {!r}, requested string or {!r}'.
format(
197 if value.__component_type__ != self.
cpp_type:
199 'wrong type for {!r}: expected {!r}, got {!r}'.
format(
203 if value.__interfaces__:
204 if not self.
interfaces.issubset(value.__interfaces__):
206 'wrong interfaces for {!r}: required {}'.
format(
208 except AttributeError:
213 return self.
store(value)
218 Return an iterator over the list of template arguments in a C++ type
221 >>> t = 'map<string, vector<int, allocator<int> >, allocator<v<i>, a<i>> >'
222 >>> list(extract_template_args(t))
223 ['string', 'vector<int, allocator<int> >', 'allocator<v<i>, a<i>>']
224 >>> list(extract_template_args('int'))
229 for p, c
in enumerate(cpp_type):
231 if template_level == 1:
232 yield cpp_type[arg_start:p].strip()
236 if template_level == 1:
240 if template_level == 0:
241 yield cpp_type[arg_start:p].strip()
256 return len(self.
data)
267 raise RuntimeError(
'cannot remove elements from the default value')
274 return self.
data != other.data
293 return repr(self.
data)
297 __handled_types__ = (re.compile(
r'(std::)?(vector|list)<.*>$'), )
299 def __init__(self, cpp_type, name=None, valueSem=None):
300 super(SequenceSemantics, self).
__init__(cpp_type, name)
306 new_value.extend(value)
311 new_value.default = value
316 Option string version of value.
318 if not isinstance(value, _ListHelper):
320 return value.opt_value()
325 Extend the sequence-semantics with a merge-method to behave like a
326 OrderedSet: Values are unique but the order is maintained.
327 Use 'OrderedSet<T>' as fifth parameter of the Gaudi::Property<T> constructor
328 to invoke this merging method.
330 __handled_types__ = (re.compile(
r"^OrderedSet<.*>$"), )
333 super(OrderedSetSemantics, self).
__init__(cpp_type, name)
343 def __init__(self, key_semantics, value_semantics):
355 return len(self.
data)
368 raise RuntimeError(
'cannot remove elements from the default value')
372 for key
in self.
data:
390 def get(self, key, default=None):
401 for key, value
in otherMap.items():
413 return repr(self.
data)
417 __handled_types__ = (re.compile(
r'(std::)?(unordered_)?map<.*>$'), )
420 super(MappingSemantics, self).
__init__(cpp_type, name)
427 new_value.update(value)
432 new_value.default = value
437 Option string version of value.
439 if not isinstance(value, _DictHelper):
441 return value.opt_value()
445 c
for c
in globals().values()
if isinstance(c, type)
446 and issubclass(c, PropertySemantics)
and c
is not PropertySemantics
451 for semantics
in SEMANTICS:
453 return semantics(cpp_type, name)