Commit ec824f44 authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[code gen] Generate basic entity classes

Those configure the ORM (pre-retrieved attribute and sort order) and provide a
'value_attr' attribute, to be used in the UI  to determine if the entity has
some business attribute or only some cardinality.
parent 6c783edca7e0
This diff is collapsed.
......@@ -2,6 +2,7 @@
all:
python xsd2yams.py > schema/seda2.py
python xsd2uicfg.py | grep -v 'unsupported merge' | grep -v 'extending cards' > views/uicfg.py
python xsd2entities.py | grep -v 'unsupported merge' | grep -v 'extending cards' > entities/generated.py
lcsv:
python xsd2concepts.py migration/data
......@@ -48,7 +48,7 @@ format = pylint
ignore = W503
max-line-length = 100
max-complexity = 12
exclude = doc/conf.py,views/uicfg.py,schema/seda2.py,gends/*,migration/*,test/data/*,setup.py,.tox/*
exclude = doc/conf.py,entities/generated.py,views/uicfg.py,schema/seda2.py,gends/*,migration/*,test/data/*,setup.py,.tox/*
[pytest]
python_files = *test_*.py
# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Generate CubicWeb's entities from XSD file"""
from xsd2yams import CodeGenerator
from xsd2uicfg import ordered_attributes
TEMPLATE = '''class {etype}(AnyEntity):
__regid__ = '{etype}'
fetch_attrs, cw_fetch_order = fetch_config({attributes})
value_attr = {value_attribute}
'''
class EntitiesGenerator(CodeGenerator):
def _generate(self, mapping, stream):
stream.write('from cubicweb.entities import AnyEntity, fetch_config\n\n')
for mapping_element in mapping.ordered:
eclass_code = self._callback('entity_class_for', mapping_element)
if eclass_code:
stream.write(eclass_code + '\n')
def entity_class_for_e_type_mapping(self, mapping):
attributes = ordered_attributes(mapping)
if mapping.etype == 'SEDAArchiveTransfer':
attributes = ordered_attributes(mapping)
value_attribute = None
elif mapping.attributes:
attributes = ordered_attributes(mapping)
value_attribute = next(iter(mapping.attributes))
else:
attributes = []
if len(mapping.cards) > 1:
attributes.append('user_cardinality')
value_attribute = None
return TEMPLATE.format(etype=mapping.etype,
attributes=attributes,
value_attribute=repr(value_attribute))
if __name__ == '__main__':
EntitiesGenerator.main()
......@@ -132,18 +132,7 @@ class UICFGGenerator(CodeGenerator):
yield mapping.rtype, role, section
def order_for_e_type_mapping(self, mapping):
attributes = list(mapping.attributes)
if mapping.etype == 'SEDAArchiveTransfer':
attributes.append('title')
attributes.append('user_annotation')
elif attributes:
if len(mapping.cards) > 1:
if attributes[0] == 'id':
attributes.append('user_cardinality')
else:
attributes.insert(0, 'user_cardinality')
if mapping.etype in HAS_ANNOTATIONS:
attributes.append('user_annotation')
attributes = ordered_attributes(mapping)
if len(attributes) > 1:
yield mapping.etype, attributes
......@@ -156,5 +145,22 @@ class Code(unicode):
return str(self)
def ordered_attributes(mapping):
"""Given an ETypeMapping, return a list of its attributes sorted by desired order of appearance
"""
attributes = list(mapping.attributes)
if mapping.etype == 'SEDAArchiveTransfer':
attributes.append('title')
attributes.append('user_annotation')
elif attributes:
if len(mapping.cards) > 1:
if attributes[0] == 'id':
attributes.append('user_cardinality')
else:
attributes.insert(0, 'user_cardinality')
if mapping.etype in HAS_ANNOTATIONS:
attributes.append('user_annotation')
return attributes
if __name__ == '__main__':
UICFGGenerator.main()
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