Commit b3ad7cd5 authored by Nicolas Chauvat's avatar Nicolas Chauvat
Browse files

feat: make serialization work with multiple inheritance

It is not clear wether yams supports multiple inheritance or not
but in some cases it seems to work, so fix serialize to be
able to use it.

--HG--
branch : 0.45
parent bca458b240d4
......@@ -26,5 +26,40 @@ class MyTests(testlib.TestCase):
' attr1 = String()',
'\n']))
if __name__ == '__main__':
def test_yams_serialize_multiple_inheritance(self):
# declaration order is important, we want to test ordered_nodes()
# so add entity Ent1 before Base0 and Base1
s = schema.Schema("Unknown")
register_base_types(s)
e1 = EntityType("Ent1")
e1.specialized_type = "Base0, Base1"
s.add_entity_type(e1)
b0 = EntityType("Base0")
s.add_entity_type(b0)
b1 = EntityType("Base1")
s.add_entity_type(b1)
s.add_relation_type(RelationType("attr1"))
s.add_relation_def(RelationDefinition("Ent1", "attr1", "String"))
out = serialize.serialize_to_python(s)
self.assertMultiLineEqual(
out,
"\n".join(
[
"from yams.buildobjs import *",
"",
"class Base1(EntityType):",
" pass",
"",
"class Base0(EntityType):",
" pass",
"",
"class Ent1(Base0, Base1):",
" attr1 = String()",
"\n",
]
),
)
if __name__ == "__main__":
testlib.unittest_main()
......@@ -5,12 +5,22 @@ from logilab.common.graph import ordered_nodes
def serialize_to_python(s):
out = StringIO()
w = out.write
w(u'from yams.buildobjs import *\n\n')
graph = {}
for entity in s.entities():
l = graph.setdefault(entity, [])
if entity._specialized_type is not None:
l.append(entity._specialized_type)
w("from yams.buildobjs import *\n\n")
graph: Dict = {}
for entity in schema.entities():
targets = graph.setdefault(entity, set())
if entity.final:
continue
base = entity._specialized_type
if base is None:
continue
if isinstance(base, str) and ", " in base:
targets |= set(base.split(", "))
else:
targets.add(base)
for e in reversed(ordered_nodes(graph)):
if not e.final:
if e._specialized_type:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment