[code gen] Generate new structures that will ease check for unhandled cardinalities
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 01 Sep 2017 11:03:28 +0200
changeset 2671 fcf239b4e613
parent 2670 a671735e9217
child 2679 39d1d971e058
[code gen] Generate new structures that will ease check for unhandled cardinalities Those structures will be used as bases for hooks necessary to check for unhandled cardinalities, that will be introduced by the following change set. We have to watch composite relation that may lead to several children, and for the entity types that are possible as children. Related to #17098404
cubicweb_seda/entities/generated.py
cubicweb_seda/xsd2entities.py
--- a/cubicweb_seda/entities/generated.py	Fri Sep 01 10:59:40 2017 +0200
+++ b/cubicweb_seda/entities/generated.py	Fri Sep 01 11:03:28 2017 +0200
@@ -915,3 +915,51 @@
     ]
   ]
 }
+CHECK_CARD_ETYPES = [
+  'SEDAAddressee',
+  'SEDAArchiveUnit',
+  'SEDABinaryDataObject',
+  'SEDACustodialHistoryItem',
+  'SEDADataObjectReference',
+  'SEDAEvent',
+  'SEDAIsPartOf',
+  'SEDAIsVersionOf',
+  'SEDAJuridictional',
+  'SEDAKeyword',
+  'SEDAPhysicalDataObject',
+  'SEDARecipient',
+  'SEDARefNonRuleId',
+  'SEDAReferences',
+  'SEDARelatedTransferReference',
+  'SEDARelationship',
+  'SEDAReplaces',
+  'SEDARequires',
+  'SEDASpatial',
+  'SEDATag',
+  'SEDATemporal',
+  'SEDAWriter'
+]
+CHECK_CHILDREN_CARD_RTYPES = [
+  'seda_addressee_from',
+  'seda_archive_unit',
+  'seda_binary_data_object',
+  'seda_custodial_history_item',
+  'seda_data_object_reference',
+  'seda_event',
+  'seda_is_part_of',
+  'seda_is_version_of',
+  'seda_juridictional',
+  'seda_keyword',
+  'seda_physical_data_object',
+  'seda_recipient_from',
+  'seda_ref_non_rule_id_from',
+  'seda_references',
+  'seda_related_transfer_reference',
+  'seda_relationship',
+  'seda_replaces',
+  'seda_requires',
+  'seda_spatial',
+  'seda_tag',
+  'seda_temporal',
+  'seda_writer_from'
+]
--- a/cubicweb_seda/xsd2entities.py	Fri Sep 01 10:59:40 2017 +0200
+++ b/cubicweb_seda/xsd2entities.py	Fri Sep 01 11:03:28 2017 +0200
@@ -44,15 +44,21 @@
 
 
 ''')
-        self.altetype2rtype = {}  # registry of rtype to children elements for all choices
+        self.altetype2rtype = {}  # rtype to children elements for all choices
+        self.check_card_etypes = set()  # entity types whose cardinality must be checked
+        self.check_card_rtypes = set()  # relation types whose children cardinality must be checked
         for mapping_element in mapping.ordered:
             self._callback('register_alt_rtype_for', mapping_element)
             eclass_code = self._callback('entity_class_for', mapping_element)
             if eclass_code:
                 stream.write(eclass_code + '\n')
-        stream.write(u'CHOICE_RTYPE = ')  # Write the registry as a constant
-        stream.write(json.dumps(self.altetype2rtype, sort_keys=True, indent=2,
-                                separators=(',', ': ')).replace('"', "'") + '\n')
+        # Write pre-computed data structures as constants
+        for name, data in [('CHOICE_RTYPE', self.altetype2rtype),
+                           ('CHECK_CARD_ETYPES', sorted(self.check_card_etypes)),
+                           ('CHECK_CHILDREN_CARD_RTYPES', sorted(self.check_card_rtypes))]:
+            stream.write(u'{} = '.format(name))
+            stream.write(json.dumps(data, sort_keys=True, indent=2,
+                                    separators=(',', ': ')).replace('"', "'") + '\n')
 
     def entity_class_for_e_type_mapping(self, mapping):
         attributes = ordered_attributes(mapping)
@@ -76,6 +82,10 @@
             if altetype.startswith('SEDAAlt'):
                 self.altetype2rtype.setdefault(altetype, []).append((mapping.rtype, composite))
 
+        if mapping.card[1] in '*+':
+            self.check_card_etypes.add(mapping.subjtype)
+            self.check_card_rtypes.add(mapping.rtype)
+
 
 if __name__ == '__main__':
     EntitiesGenerator.main()