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_json_save_multiple_roots_roundtrip(tmpdir):
A = Ecore.EClass('A')
A.eStructuralFeatures.append(Ecore.EAttribute('name', Ecore.EString))
pack = Ecore.EPackage('pack', 'packuri', 'pack')
pack.eClassifiers.append(A)
f = tmpdir.mkdir('pyecore-tmp').join('multiple.json')
resource = JsonResource(URI(str(f)))
resource.append(A(name='root1'))
resource.append(A(name='root2'))
resource.save()
global_registry[pack.nsURI] = pack
resource = JsonResource(URI(str(f)))
resource.load()
assert len(resource.contents) == 2
assert resource.contents[0].name == 'root1'
assert resource.contents[1].name == 'root2'
del global_registry[pack.nsURI]
def test_json_option_serialize_default_values(tmpdir):
f = tmpdir.mkdir('pyecore-tmp').join('test.json')
resource = JsonResource(URI(str(f)))
p = Point()
p.x = 0.0
p.z = 0.0
resource.append(p)
resource.save(options={JsonOptions.SERIALIZE_DEFAULT_VALUES: True})
dct = json.load(open(str(f)))
assert dct['x'] == 0.0
assert dct['z'] == 0.0
assert 'y' not in dct
def test_json_resource_createset(tmpdir, lib):
f = tmpdir.mkdir('pyecore-tmp').join('test.json')
resource = JsonResource(URI(str(f)))
# we create some instances
root = lib.MyRoot()
a1 = lib.A()
suba1 = lib.SubA()
root.a_container.extend([a1, suba1])
root.trans = 'transient_value'
# we add the elements to the resource
resource.append(root)
resource.save()
# we read the model
resource = JsonResource(URI(str(f)))
resource.load()
assert resource.contents != []
def test_json_save_multiple_roots_roundtrip(tmpdir):
A = Ecore.EClass('A')
A.eStructuralFeatures.append(Ecore.EAttribute('name', Ecore.EString))
pack = Ecore.EPackage('pack', 'packuri', 'pack')
pack.eClassifiers.append(A)
f = tmpdir.mkdir('pyecore-tmp').join('multiple.json')
resource = JsonResource(URI(str(f)))
resource.append(A(name='root1'))
resource.append(A(name='root2'))
resource.save()
global_registry[pack.nsURI] = pack
resource = JsonResource(URI(str(f)))
resource.load()
assert len(resource.contents) == 2
assert resource.contents[0].name == 'root1'
assert resource.contents[1].name == 'root2'
del global_registry[pack.nsURI]
f = tmpdir.mkdir('pyecore-tmp').join('test.json')
resource = JsonResource(URI(str(f)))
# we create some instances
root = lib.MyRoot()
a1 = lib.A()
suba1 = lib.SubA()
root.a_container.extend([a1, suba1])
root.trans = 'transient_value'
# we add the elements to the resource
resource.append(root)
resource.save()
# we read the model
resource = JsonResource(URI(str(f)))
resource.load()
assert resource.contents != []
assert len(resource.contents[0].eContents) == 2
pass
@Ecore.EMetaclass
class B(A):
pass
@Ecore.EMetaclass
class Root(object):
many_a = Ecore.EReference(eType=A, upper=-1, containment=True)
root = Root()
root.many_a.append(A())
root.many_a.append(B())
f = tmpdir.mkdir('pyecore-tmp').join('nomapping.json')
resource = JsonResource(URI(str(f)))
resource.register_mapper(A, MyMapper())
resource.append(root)
resource.save()
dct = json.load(open(str(f)))
print(dct)
assert dct['many_a'] == []
rset.resource_factory['json'] = lambda uri: JsonResource(uri)
return rset
def test_json_resource_save_static_metamodel(tmpdir):
f = tmpdir.mkdir('pyecore-tmp').join('test.json')
resource = JsonResource(URI(str(f)))
# we add the elements to the resource
resource.append(eClass)
resource.save()
# we read the model
resource = JsonResource(URI(str(f)))
resource.load()
assert resource.contents != []
assert len(resource.contents[0].eContents) == 2
root = resource.contents[0]
assert root.eContents[0].name == 'A'
pass
@Ecore.EMetaclass
class Root(object):
name = Ecore.EAttribute(eType=Ecore.EString)
many_a = Ecore.EReference(eType=A, upper=-1, containment=True)
eclasses = Ecore.EReference(eType=Ecore.EClass, upper=-1, containment=True)
root = Root()
root.many_a.append(A('test1'))
root.many_a.append(B('test2'))
root.eclasses.append(Ecore.EClass('test3'))
f = tmpdir.mkdir('pyecore-tmp').join('custom.json')
resource = JsonResource(URI(str(f)))
resource.register_mapper(A, MyMapper())
resource.register_mapper(Ecore.EClass.eClass, MyMapper())
resource.register_mapper(Root.eClass, MyRootMapper())
resource.append(root)
resource.save()
dct = json.load(open(str(f)))
assert dct['many_a'][0]['name_custom'] == 'test1_custom'
assert dct['many_a'][1]['name_custom'] == 'test2_custom'
assert dct['eclasses'][0]['name_custom'] == 'test3_custom'
from backports.functools_lru_cache import lru_cache
from chainmap import ChainMap
from pyecore.resources.json import JsonResource
from . import eClassifiers, datasources, types, values, variables
class GeppettoResource(JsonResource):
packages = [eClassifiers,
datasources.eClassifiers,
types.eClassifiers,
values.eClassifiers,
variables.eClassifiers]
chain = ChainMap(*packages)
def serialize_eclass(self, eclass):
return eclass.name
@lru_cache()
def resolve_eclass(self, uri):
return self.chain.get(uri)