Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test__jsonresource_load_crossref_ecore(rset):
json_file = path.join('tests', 'json', 'data', 'f2.json')
resource = rset.get_resource(json_file)
root = resource.contents[0]
assert isinstance(root, Ecore.EPackage)
json_file = path.join('tests', 'json', 'data', 'f1.json')
resource = rset.get_resource(json_file)
root = resource.contents[0]
assert isinstance(root, Ecore.EPackage)
assert isinstance(root.eClassifiers[0].eStructuralFeatures[0].eType,
Ecore.EProxy)
assert root.eClassifiers[0].eStructuralFeatures[0].eType.name == 'B'
assert isinstance(root.eClassifiers[0].eStructuralFeatures[1].eType,
Ecore.EProxy)
# auto-load kicks in
assert root.eClassifiers[0].eStructuralFeatures[1].eType.name == 'C'
# we load it, we have the same result
json_file = path.join('tests', 'json', 'data', 'relative', 'f2.json')
rset.get_resource(json_file)
assert root.eClassifiers[0].eStructuralFeatures[1].eType.name == 'C'
# Decode the XMI
if '{{{0}}}XMI'.format(self.prefixes.get(XMI)) == xmlroot.tag:
real_roots = xmlroot
else:
real_roots = [xmlroot]
def grouper(iterable):
args = [iter(iterable)] * 2
return zip(*args)
self.schema_locations = {}
schema_tag_list = xmlroot.attrib.get(self.schema_tag, '')
for prefix, path in grouper(schema_tag_list.split()):
if '#' not in path:
path = path + '#'
self.schema_locations[prefix] = EProxy(path, self)
for root in real_roots:
modelroot = self._init_modelroot(root)
for child in root:
self._decode_eobject(child, modelroot)
if self.contents:
self._decode_ereferences()
self._clean_registers()
self.uri.close_stream()
def _decode_node(self, parent_eobj, node):
_, node_tag = self.extract_namespace(node.tag)
feature_container = self._find_feature(parent_eobj.eClass, node_tag)
if not feature_container:
raise ValueError('Feature "{0}" is unknown for {1}, line {2}'
.format(node_tag,
parent_eobj.eClass.name,
node.sourceline,))
if self._is_none_node(node):
parent_eobj.__setattr__(feature_container.name, None)
return (None, None, [], [], False)
if node.get('href'):
ref = node.get('href')
proxy = EProxy(path=ref, resource=self)
return (feature_container, proxy, [], [], False)
if self._type_attribute(node):
prefix, _type = self._type_attribute(node).split(':')
if not prefix:
raise ValueError('Prefix {0} is not registered, line {1}'
.format(prefix, node.tag))
epackage = self.prefix2epackage(prefix)
etype = epackage.getEClassifier(_type)
if not etype:
raise ValueError('Type {0} is unknown in {1}, line{2}'
.format(_type, epackage, node.tag))
else:
etype = feature_container.eType
if isinstance(etype, EProxy):
etype.force_resolve()
ref = node.get('href')
proxy = EProxy(path=ref, resource=self)
return (feature_container, proxy, [], [], False)
if self._type_attribute(node):
prefix, _type = self._type_attribute(node).split(':')
if not prefix:
raise ValueError('Prefix {0} is not registered, line {1}'
.format(prefix, node.tag))
epackage = self.prefix2epackage(prefix)
etype = epackage.getEClassifier(_type)
if not etype:
raise ValueError('Type {0} is unknown in {1}, line{2}'
.format(_type, epackage, node.tag))
else:
etype = feature_container.eType
if isinstance(etype, EProxy):
etype.force_resolve()
# we create the instance
if etype is EClass or etype is EClass.eClass:
name = node.get('name')
eobject = etype(name)
elif (etype is EStringToStringMapEntry
or etype is EStringToStringMapEntry.eClass) \
and feature_container is EAnnotation.details:
annotation_key = node.get('key')
annotation_value = node.get('value')
parent_eobj.details[annotation_key] = annotation_value
if annotation_key == 'documentation':
container = parent_eobj.eContainer()
if hasattr(container, 'python_class'):
container = container.python_class
def _resolve_nonhref(self, path):
uri, fragment = self._is_external(path)
if uri:
cleaned_uri = uri + '#' + fragment
if cleaned_uri in self._resolve_mem:
return self._resolve_mem[cleaned_uri]
proxy = EProxy(path=cleaned_uri, resource=self)
self._resolve_mem[cleaned_uri] = proxy
return proxy
if fragment in self._resolve_mem:
return self._resolve_mem[fragment]
return self.resolve(fragment)
def to_obj(self, d, owning_feature=None, first=False):
is_ref = self.ref_tag in d
if is_ref:
return EProxy(path=d[self.ref_tag], resource=self)
excludes = ['eClass', self.ref_tag, 'uuid']
if 'eClass' in d:
uri_eclass = d['eClass']
eclass = self.resolve_eclass(uri_eclass)
else:
eclass = owning_feature.eType
if not eclass:
raise ValueError('Unknown metaclass for uri "{}"'
.format(uri_eclass))
if eclass in (EClass.eClass, EClass):
inst = eclass(d['name'])
excludes.append('name')
else:
inst = eclass()
if first:
self.use_uuid = 'uuid' in d
def isinstance(obj, _type):
if obj is None:
return True
elif obj.__class__ is _type:
return True
elif _type.__class__ is EDataType and obj.__class__ is _type.eType:
return True
elif isinstance(obj, EProxy) and not obj.resolved:
return not obj.resolved
elif isinstance(obj, _type):
return True
try:
return _type.__isinstance__(obj)
except AttributeError:
return False
def __init__(self, got=None, expected=None, feature=None):
if isinstance(expected, EProxy):
expected.force_resolve()
expected = expected._wrapped
self.got = got
self.expected = expected
self.feature = feature
msg = "Expected type {0}, but got type {1} with value {2} instead "
msg = msg.format(expected, type(got).__name__, got)
if feature:
msg += "for feature {} of {}".format(feature,
feature.eContainingClass)
super().__init__(msg)