11 from __future__
import absolute_import
18 from collections.abc
import MutableMapping, MutableSequence
21 from collections
import MutableMapping, MutableSequence
23 if sys.version_info >= (3,):
26 _log = logging.getLogger(__name__)
27 is_64bits = sys.maxsize > 2 ** 32
32 Basic property semantics implementation, with no validation/transformation.
35 __handled_types__ = (re.compile(
r".*"),)
48 h.match(value)
if hasattr(h,
"match")
else h == value
51 raise TypeError(
"C++ type {!r} not supported".
format(value))
56 Transformation for data when reading the property.
62 Validation/transformation of the data to be stored.
68 Allow overriding the definition of "is set" if we need helper types.
74 Option string version of value.
76 if hasattr(value,
"__opt_value__"):
77 return value.__opt_value__()
84 Used when merging two Configurable instances, by default just ensure
85 the two values do not conflict, but it can be overridden in
86 derived semantics to, for example, append to the two lists.
89 raise ValueError(
"cannot merge values %r and %r" % (a, b))
93 DefaultSemantics = PropertySemantics
97 __handled_types__ = (
"std::string",)
100 if not isinstance(value, basestring):
101 raise ValueError(
"cannot set property {} to {!r}".
format(self.
name, value))
106 __handled_types__ = (
"bool",)
113 __handled_types__ = (
"float",
"double")
116 from numbers
import Number
118 if not isinstance(value, Number):
120 "number expected, got {!r} in assignemnt to {}".
format(value, self.
name)
128 "signed char": (-128, 127),
129 "short": (-32768, 32767),
130 "int": (-2147483648, 2147483647),
132 (-9223372036854775808, 9223372036854775807)
134 else (-2147483648, 2147483647)
136 "long long": (-9223372036854775808, 9223372036854775807),
137 "unsigned char": (0, 255),
138 "unsigned short": (0, 65535),
139 "unsigned int": (0, 4294967295),
140 "unsigned long": (0, 18446744073709551615
if is_64bits
else 4294967295),
141 "unsigned long long": (0, 18446744073709551615),
144 __handled_types__ = tuple(INT_RANGES)
147 from numbers
import Number
149 if not isinstance(value, Number):
151 "number expected, got {!r} in assignemnt to {}".
format(value, self.
name)
155 _log.warning(
"converted %s to %d in assignment to %s", value, v, self.
name)
157 if v < min_value
or v > max_value:
159 "value {} outside limits for {!r} {}".
format(
166 _IDENTIFIER_RE =
r"[a-zA-Z_][a-zA-Z0-9_]*"
167 _NS_IDENT_RE =
r"{ident}(::{ident})*".
format(ident=_IDENTIFIER_RE)
168 _COMMA_SEPARATION_RE =
r"{exp}(,{exp})*"
172 __handled_types__ = (
176 r"AlgTool(:{})?$".
format(_COMMA_SEPARATION_RE.format(exp=_NS_IDENT_RE))
179 r"Service(:{})?$".
format(_COMMA_SEPARATION_RE.format(exp=_NS_IDENT_RE))
184 super(ComponentSemantics, self).
__init__(cpp_type, name)
193 from .
import Configurable, Configurables
195 if isinstance(value, Configurable):
197 elif isinstance(value, basestring):
199 if value
in Configurable.instances:
200 value = Configurable.instances[value]
204 t, n = value.split(
"/")
207 value = Configurables.getByType(t).getInstance(n)
210 "cannot assign {!r} to {!r}, requested string or {!r}".
format(
214 if value.__component_type__ != self.
cpp_type:
216 "wrong type for {!r}: expected {!r}, got {!r}".
format(
222 if value.__interfaces__:
223 if not self.
interfaces.issubset(value.__interfaces__):
225 "wrong interfaces for {!r}: required {}".
format(
229 except AttributeError:
234 return self.
store(value)
239 Return an iterator over the list of template arguments in a C++ type
242 >>> t = 'map<string, vector<int, allocator<int> >, allocator<v<i>, a<i>> >'
243 >>> list(extract_template_args(t))
244 ['string', 'vector<int, allocator<int> >', 'allocator<v<i>, a<i>>']
245 >>> list(extract_template_args('int'))
250 for p, c
in enumerate(cpp_type):
252 if template_level == 1:
253 yield cpp_type[arg_start:p].strip()
257 if template_level == 1:
261 if template_level == 0:
262 yield cpp_type[arg_start:p].strip()
277 return len(self.
data)
288 raise RuntimeError(
"cannot remove elements from the default value")
295 return self.
data != other.data
313 return repr(self.
data)
317 __handled_types__ = (re.compile(
r"(std::)?(vector|list)<.*>$"),)
319 def __init__(self, cpp_type, name=None, valueSem=None):
320 super(SequenceSemantics, self).
__init__(cpp_type, name)
327 new_value.extend(value)
332 new_value.default = value
337 Option string version of value.
339 if not isinstance(value, _ListHelper):
341 return value.opt_value()
346 Extend the sequence-semantics with a merge-method to behave like a
347 OrderedSet: Values are unique but the order is maintained.
348 Use 'OrderedSet<T>' as fifth parameter of the Gaudi::Property<T> constructor
349 to invoke this merging method.
352 __handled_types__ = (re.compile(
r"^OrderedSet<.*>$"),)
355 super(OrderedSetSemantics, self).
__init__(cpp_type, name)
365 def __init__(self, key_semantics, value_semantics):
377 return len(self.
data)
392 raise RuntimeError(
"cannot remove elements from the default value")
396 for key
in self.
data:
413 def get(self, key, default=None):
424 for key, value
in otherMap.items():
434 return repr(self.
data)
438 __handled_types__ = (re.compile(
r"(std::)?(unordered_)?map<.*>$"),)
441 super(MappingSemantics, self).
__init__(cpp_type, name)
448 new_value.update(value)
453 new_value.default = value
458 Option string version of value.
460 if not isinstance(value, _DictHelper):
462 return value.opt_value()
467 for c
in globals().values()
468 if isinstance(c, type)
469 and issubclass(c, PropertySemantics)
470 and c
is not PropertySemantics
475 for semantics
in SEMANTICS:
477 return semantics(cpp_type, name)