Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def from_address(address):
components = []
if not address or not isinstance(address, Compatibility.stringy):
raise Ref.InvalidRefError('Invalid address: %s' % repr(address))
if not (address.startswith('[') or address.startswith('.')):
if Ref._VALID_START.match(address[0]):
components = Ref.split_components('.' + address)
else:
raise Ref.InvalidRefError(address)
else:
components = Ref.split_components(address)
return Ref(components)
def matches(cls, loadable):
return isinstance(loadable, Compatibility.stringy) and os.path.isfile(loadable)
def Choice(*args):
"""Helper function for creating new choice types.
This can be called either as:
Choice(Name, [Type1, Type2, ...])
or:
Choice([Type1, Type2, ...])
In the latter case, the name of the new type will be autogenerated, and will
look like "Choice_Type1_Type2".
"""
if len(args) == 2:
name, alternatives = args
else:
name = "Choice_" + "_".join(a.__name__ for a in args[0])
alternatives = args[0]
assert isinstance(name, Compatibility.stringy)
assert all(issubclass(t, Type) for t in alternatives)
return TypeFactory.new({}, ChoiceFactory.PROVIDES, name,
tuple(t.serialize_type() for t in alternatives))
def checker(cls, obj):
assert isinstance(obj, Float)
if isinstance(obj._value, Compatibility.real + Compatibility.integer):
return TypeCheck.success()
else:
return TypeCheck.failure("%s not a float" % repr(obj._value))
def wrap(value):
if isinstance(value, dict):
return Environment(value)
elif isinstance(value, (Environment, Object)):
return value
else:
if isinstance(value, Compatibility.numeric + Compatibility.stringy):
return str(value)
else:
raise ValueError(
'Environment values must be strings, numbers, Objects or other Environments. '
'Got %s instead.' % type(value))
def isiterable(values):
return isinstance(values, Sequence) and not isinstance(values, Compatibility.stringy)
resolved = False
for namable in namables:
try:
value = namable.find(ref)
resolved = True
break
except Namable.Error:
continue
if resolved:
isplits.append(value)
else:
isplits.append(ref)
unbound.append(ref)
else:
isplits.append(ref)
return (''.join(map(str if Compatibility.PY3 else unicode, isplits)), unbound)
def create(type_dict, *type_parameters):
"""
type_parameters should be:
(name, (alternative1, alternative2, ...))
where name is a string, and the alternatives are all valid serialized
types.
"""
assert len(type_parameters) == 2
name = type_parameters[0]
alternatives = type_parameters[1]
assert isinstance(name, Compatibility.stringy)
assert isinstance(alternatives, (list, tuple))
choice_types = []
for c in alternatives:
choice_types.append(TypeFactory.new(type_dict, *c))
return TypeMetaclass(str(name), (ChoiceContainer,), {'CHOICES': choice_types})
def checker(cls, obj):
assert isinstance(obj, Integer)
if isinstance(obj._value, Compatibility.integer):
return TypeCheck.success()
else:
return TypeCheck.failure("%s not an integer" % repr(obj._value))