Basic export of simplified profile to SEDA 0.2 XSD
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 07 Oct 2016 13:22:26 +0200
changeset 1623 23f825b308db
parent 1622 eeb9a2a9b20c
child 1624 3b80079a761d
Basic export of simplified profile to SEDA 0.2 XSD Related to #15045341
entities/profile_generation.py
test/data/seda_02_export.xsd
test/test_profile_generation.py
--- a/entities/profile_generation.py	Fri Oct 07 13:21:35 2016 +0200
+++ b/entities/profile_generation.py	Fri Oct 07 13:22:26 2016 +0200
@@ -833,7 +833,7 @@
         appraisal_rule_entity = self.archive_unit_appraisal_rule(archive_unit)
         if appraisal_rule_entity:
             self.xsd_appraisal_rule(archive_node, appraisal_rule_entity)
-        # XXX not optional in seda < 2
+        # XXX not optional in seda 1
         access_rule_entity = self.archive_unit_access_rule(archive_unit)
         self.xsd_access_rule(archive_node, access_rule_entity)
         content_entity = self.archive_unit_content(archive_unit)
@@ -1070,6 +1070,93 @@
         return entity.cwuri
 
 
+class SEDA02XSDExport(SEDA1XSDExport):
+    """Adapter to build an XSD representation of a SEDA profile, using SEDA 0.2 specification"""
+    __regid__ = 'SEDA-0.2.xsd'
+
+    namespaces = SEDA1XSDExport.namespaces.copy()
+    namespaces[None] = 'fr:gouv:ae:archive:draft:standard_echange_v0.2'
+    namespaces['qdt'] = 'fr:gouv:ae:archive:draft:standard_echange_v0.2:QualifiedDataType:1'
+    namespaces['udt'] = 'urn:un:unece:uncefact:data:standard:UnqualifiedDataType:6'
+
+    root_attributes = SEDA1XSDExport.root_attributes.copy()
+    root_attributes['targetNamespace'] = 'fr:gouv:ae:archive:draft:standard_echange_v0.2'
+    root_attributes['version'] = '1.1'
+
+    def xsd_archive(self, parent, archive_unit):
+        """Append XSD elements for an archive to the given parent node."""
+        archive_node = self.xsd_element(parent, 'Contains',
+                                        cardinality=archive_unit.user_cardinality,
+                                        documentation=archive_unit.user_annotation,
+                                        xsd_attributes=[XSDAttr('Id', 'xsd:ID')])
+        # hard-coded description's language
+        self.xsd_element(archive_node, 'DescriptionLanguage', 'qdt:CodeLanguageType',
+                         fixed_value='fr',
+                         xsd_attributes=[LIST_VERSION_ID_2009])
+        name_entity = self.archive_unit_name(archive_unit)
+        self.xsd_element(archive_node, 'Name', 'udt:TextType',
+                         fixed_value=name_entity.title,
+                         documentation=name_entity.user_annotation,
+                         xsd_attributes=[XSDAttr('languageID', 'xsd:language')])
+        # in SEDA 0.2, description level is on the archive element, not on its content description
+        content_entity = self.archive_unit_content(archive_unit)
+        self.xsd_description_level(archive_node, content_entity.description_level_concept)
+        appraisal_rule_entity = self.archive_unit_appraisal_rule(archive_unit)
+        if appraisal_rule_entity:
+            self.xsd_appraisal_rule(archive_node, appraisal_rule_entity)
+        # in SEDA 0.2, access restriction is not mandatory
+        access_rule_entity = self.archive_unit_access_rule(archive_unit)
+        if access_rule_entity:
+            self.xsd_access_rule(archive_node, access_rule_entity)
+        content_entity = self.archive_unit_content(archive_unit)
+        self.xsd_content_description(archive_node, content_entity)
+        self.xsd_children(archive_node, archive_unit)
+
+    def xsd_content_description(self, parent, content):
+        """Append XSD elements for a description content to the given parent node"""
+        if not (content.start_date or content.end_date or content.description or content.keywords):
+            return
+        cd_node = self.xsd_element(parent, 'ContentDescription',
+                                   # XXX should always be 1
+                                   # cardinality=content_description.user_cardinality,
+                                   documentation=content.user_annotation,
+                                   xsd_attributes=[XSDAttr('Id', 'xsd:ID')])
+        for seda2_name, seda1_name in (('start', 'oldest'), ('end', 'latest')):
+            date_entity = getattr(content, '%s_date' % seda2_name)
+            if date_entity:
+                self.xsd_element(cd_node, '%sDate' % seda1_name.capitalize(), 'udt:DateType',
+                                 cardinality=date_entity.user_cardinality,
+                                 documentation=date_entity.user_annotation)
+        if content.description:
+            self.xsd_description(cd_node, content.description)
+        # in SEDA 0.2, content description has a description language as well
+        self.xsd_element(cd_node, 'DescriptionLanguage', 'qdt:CodeLanguageType',
+                         fixed_value='fr',
+                         xsd_attributes=[LIST_VERSION_ID_2009])
+        for keyword in content.keywords:
+            self.xsd_keyword(cd_node, keyword)
+        # in SEDA 0.2, there may be some access restriction on the content description but it's on
+        # the archive in our model
+
+    def xsd_archive_object(self, parent, archive_unit):
+        """Append XSD elements for the archive object to the given parent node."""
+        ao_node = super(SEDA02XSDExport, self).xsd_archive_object(parent, archive_unit)
+        # in SEDA 0.2, description level is on the archive object element, not on its content
+        # description
+        content_entity = self.archive_unit_content(archive_unit)
+        self.xsd_description_level(ao_node, content_entity.description_level_concept)
+
+    # in SEDA 0.2, ArchiveObject tag name is 'Contains' (as for Archive)
+    archive_object_tag_name = 'Contains'
+    # in SEDA 0.2, AccessRestrictionRule tag name is 'AccessRestriction'
+    access_restriction_tag_name = 'AccessRestriction'
+    # in SEDA 0.2, keyword tag name is 'ContentDescriptive', not 'Keyword' and keyword content type
+    # is TextType and there is no 'role' attribute
+    kw_tag_name = 'ContentDescriptive'
+    kw_content_tag_type = 'udt:TextType'
+    kw_content_tag_attributes = [XSDAttr('languageID', 'xsd:language')]
+
+
 def xsd_element_cardinality(occ, card_entity):
     """Return XSD element cardinality for the given pyxst Occurence. Cardinality may be overriden by
     the data model's user_cardinality value.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/data/seda_02_export.xsd	Fri Oct 07 13:22:26 2016 +0200
@@ -0,0 +1,288 @@
+<?xml version='1.0' encoding='utf-8' standalone='no'?>
+<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="fr:gouv:ae:archive:draft:standard_echange_v0.2" version="1.1" xmlns:clm60133="urn:un:unece:uncefact:codelist:standard:6:0133:40106" xmlns:clmDAFFileTypeCode="urn:un:unece:uncefact:codelist:draft:DAF:fileTypeCode:2009-08-18" xmlns:clmIANACharacterSetCode="urn:un:unece:uncefact:codelist:standard:IANA:CharacterSetCode:2007-05-14" xmlns:clmIANAMIMEMediaType="urn:un:unece:uncefact:codelist:standard:IANA:MIMEMediaType:2008-11-12" xmlns:qdt="fr:gouv:ae:archive:draft:standard_echange_v0.2:QualifiedDataType:1" xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:6" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="fr:gouv:ae:archive:draft:standard_echange_v0.2">
+  <xsd:element name="ArchiveTransfer">
+    <xsd:annotation>
+      <xsd:documentation>my profile title &amp;&amp;</xsd:documentation>
+    </xsd:annotation>
+    <xsd:complexType>
+      <xsd:sequence>
+        <xsd:element fixed="my profile description &amp;&amp;" name="Comment">
+          <xsd:complexType>
+            <xsd:simpleContent>
+              <xsd:extension base="udt:TextType">
+                <xsd:attribute name="languageID" type="xsd:language" use="prohibited"/>
+              </xsd:extension>
+            </xsd:simpleContent>
+          </xsd:complexType>
+        </xsd:element>
+        <xsd:element name="Date" type="udt:DateTimeType"/>
+        <xsd:element name="TransferIdentifier">
+          <xsd:complexType>
+            <xsd:simpleContent>
+              <xsd:extension base="qdt:ArchivesIDType">
+                <xsd:attribute name="schemeID" type="xsd:token" use="prohibited"/>
+                <xsd:attribute name="schemeName" type="xsd:string" use="prohibited"/>
+                <xsd:attribute name="schemeAgencyName" type="xsd:string" use="prohibited"/>
+                <xsd:attribute name="schemeVersionID" type="xsd:token" use="prohibited"/>
+                <xsd:attribute name="schemeDataURI" type="xsd:anyURI" use="prohibited"/>
+                <xsd:attribute name="schemeURI" type="xsd:anyURI" use="prohibited"/>
+              </xsd:extension>
+            </xsd:simpleContent>
+          </xsd:complexType>
+        </xsd:element>
+        <xsd:element maxOccurs="unbounded" name="Contains">
+          <xsd:complexType>
+            <xsd:sequence>
+              <xsd:element fixed="fr" name="DescriptionLanguage">
+                <xsd:complexType>
+                  <xsd:simpleContent>
+                    <xsd:extension base="qdt:CodeLanguageType">
+                      <xsd:attribute fixed="edition 2009" name="listVersionID" type="xsd:token" use="required"/>
+                    </xsd:extension>
+                  </xsd:simpleContent>
+                </xsd:complexType>
+              </xsd:element>
+              <xsd:element name="Name">
+                <xsd:complexType>
+                  <xsd:simpleContent>
+                    <xsd:extension base="udt:TextType">
+                      <xsd:attribute name="languageID" type="xsd:language" use="prohibited"/>
+                    </xsd:extension>
+                  </xsd:simpleContent>
+                </xsd:complexType>
+              </xsd:element>
+              <xsd:element fixed="file" name="DescriptionLevel">
+                <xsd:complexType>
+                  <xsd:simpleContent>
+                    <xsd:extension base="qdt:CodeDescriptionLevelType">
+                      <xsd:attribute fixed="edition 2009" name="listVersionID" type="xsd:token" use="required"/>
+                    </xsd:extension>
+                  </xsd:simpleContent>
+                </xsd:complexType>
+              </xsd:element>
+              <xsd:element minOccurs="0" name="Appraisal">
+                <xsd:annotation>
+                  <xsd:documentation>detruire le document</xsd:documentation>
+                </xsd:annotation>
+                <xsd:complexType>
+                  <xsd:sequence>
+                    <xsd:element fixed="detruire" name="Code">
+                      <xsd:complexType>
+                        <xsd:simpleContent>
+                          <xsd:extension base="qdt:CodeAppraisalType">
+                            <xsd:attribute fixed="edition 2009" name="listVersionID" type="xsd:token" use="required"/>
+                          </xsd:extension>
+                        </xsd:simpleContent>
+                      </xsd:complexType>
+                    </xsd:element>
+                    <xsd:element fixed="P10Y" name="Duration" type="qdt:ArchivesDurationType">
+                      <xsd:annotation>
+                        <xsd:documentation>C'est dans 10ans je m'en irai</xsd:documentation>
+                      </xsd:annotation>
+                    </xsd:element>
+                    <xsd:element name="StartDate" type="udt:DateType"/>
+                  </xsd:sequence>
+                  <xsd:attribute name="Id" type="xsd:ID" use="prohibited"/>
+                </xsd:complexType>
+              </xsd:element>
+              <xsd:element name="AccessRestriction">
+                <xsd:complexType>
+                  <xsd:sequence>
+                    <xsd:element name="Code">
+                      <xsd:complexType>
+                        <xsd:simpleContent>
+                          <xsd:extension base="qdt:CodeAccessRestrictionType">
+                            <xsd:attribute fixed="edition 2009" name="listVersionID" type="xsd:token" use="required"/>
+                          </xsd:extension>
+                        </xsd:simpleContent>
+                      </xsd:complexType>
+                    </xsd:element>
+                    <xsd:element name="StartDate" type="udt:DateType"/>
+                  </xsd:sequence>
+                  <xsd:attribute name="Id" type="xsd:ID" use="prohibited"/>
+                </xsd:complexType>
+              </xsd:element>
+              <xsd:element name="ContentDescription">
+                <xsd:complexType>
+                  <xsd:sequence>
+                    <xsd:element minOccurs="0" name="OldestDate" type="udt:DateType"/>
+                    <xsd:element minOccurs="0" name="LatestDate" type="udt:DateType"/>
+                    <xsd:element minOccurs="0" name="Description">
+                      <xsd:complexType>
+                        <xsd:simpleContent>
+                          <xsd:extension base="udt:TextType">
+                            <xsd:attribute name="languageID" type="xsd:language" use="prohibited"/>
+                          </xsd:extension>
+                        </xsd:simpleContent>
+                      </xsd:complexType>
+                    </xsd:element>
+                    <xsd:element fixed="fr" name="DescriptionLanguage">
+                      <xsd:complexType>
+                        <xsd:simpleContent>
+                          <xsd:extension base="qdt:CodeLanguageType">
+                            <xsd:attribute fixed="edition 2009" name="listVersionID" type="xsd:token" use="required"/>
+                          </xsd:extension>
+                        </xsd:simpleContent>
+                      </xsd:complexType>
+                    </xsd:element>
+                    <xsd:element maxOccurs="unbounded" minOccurs="0" name="ContentDescriptive">
+                      <xsd:complexType>
+                        <xsd:sequence>
+                          <xsd:element fixed="file" name="KeywordContent">
+                            <xsd:complexType>
+                              <xsd:simpleContent>
+                                <xsd:extension base="udt:TextType">
+                                  <xsd:attribute name="languageID" type="xsd:language" use="prohibited"/>
+                                </xsd:extension>
+                              </xsd:simpleContent>
+                            </xsd:complexType>
+                          </xsd:element>
+                          <xsd:element fixed="%(concept-uri)s" name="KeywordReference">
+                            <xsd:complexType>
+                              <xsd:simpleContent>
+                                <xsd:extension base="qdt:ArchivesIDType">
+                                  <xsd:attribute name="schemeID" type="xsd:token" use="prohibited"/>
+                                  <xsd:attribute fixed="seda_description_level/None vocabulary" name="schemeName" type="xsd:string" use="required"/>
+                                  <xsd:attribute name="schemeAgencyName" type="xsd:string" use="prohibited"/>
+                                  <xsd:attribute name="schemeVersionID" type="xsd:token" use="prohibited"/>
+                                  <xsd:attribute name="schemeDataURI" type="xsd:anyURI" use="prohibited"/>
+                                  <xsd:attribute fixed="%(scheme-uri)s" name="schemeURI" type="xsd:anyURI" use="required"/>
+                                </xsd:extension>
+                              </xsd:simpleContent>
+                            </xsd:complexType>
+                          </xsd:element>
+                        </xsd:sequence>
+                        <xsd:attribute name="Id" type="xsd:ID" use="prohibited"/>
+                      </xsd:complexType>
+                    </xsd:element>
+                  </xsd:sequence>
+                  <xsd:attribute name="Id" type="xsd:ID" use="prohibited"/>
+                </xsd:complexType>
+              </xsd:element>
+              <xsd:element maxOccurs="unbounded" name="Contains">
+                <xsd:complexType>
+                  <xsd:sequence>
+                    <xsd:element name="Name">
+                      <xsd:complexType>
+                        <xsd:simpleContent>
+                          <xsd:extension base="udt:TextType">
+                            <xsd:attribute name="languageID" type="xsd:language" use="prohibited"/>
+                          </xsd:extension>
+                        </xsd:simpleContent>
+                      </xsd:complexType>
+                    </xsd:element>
+                    <xsd:element minOccurs="0" name="Appraisal">
+                      <xsd:complexType>
+                        <xsd:sequence>
+                          <xsd:element name="Code">
+                            <xsd:complexType>
+                              <xsd:simpleContent>
+                                <xsd:extension base="qdt:CodeAppraisalType">
+                                  <xsd:attribute fixed="edition 2009" name="listVersionID" type="xsd:token" use="required"/>
+                                </xsd:extension>
+                              </xsd:simpleContent>
+                            </xsd:complexType>
+                          </xsd:element>
+                          <xsd:element name="Duration" type="qdt:ArchivesDurationType"/>
+                          <xsd:element name="StartDate" type="udt:DateType"/>
+                        </xsd:sequence>
+                        <xsd:attribute name="Id" type="xsd:ID" use="prohibited"/>
+                      </xsd:complexType>
+                    </xsd:element>
+                    <xsd:element name="AccessRestriction">
+                      <xsd:annotation>
+                        <xsd:documentation>restrict</xsd:documentation>
+                      </xsd:annotation>
+                      <xsd:complexType>
+                        <xsd:sequence>
+                          <xsd:element fixed="AR038" name="Code">
+                            <xsd:complexType>
+                              <xsd:simpleContent>
+                                <xsd:extension base="qdt:CodeAccessRestrictionType">
+                                  <xsd:attribute fixed="edition 2009" name="listVersionID" type="xsd:token" use="required"/>
+                                </xsd:extension>
+                              </xsd:simpleContent>
+                            </xsd:complexType>
+                          </xsd:element>
+                          <xsd:element name="StartDate" type="udt:DateType"/>
+                        </xsd:sequence>
+                        <xsd:attribute name="Id" type="xsd:ID" use="prohibited"/>
+                      </xsd:complexType>
+                    </xsd:element>
+                    <xsd:element name="DescriptionLevel">
+                      <xsd:complexType>
+                        <xsd:simpleContent>
+                          <xsd:extension base="qdt:CodeDescriptionLevelType">
+                            <xsd:attribute fixed="edition 2009" name="listVersionID" type="xsd:token" use="required"/>
+                          </xsd:extension>
+                        </xsd:simpleContent>
+                      </xsd:complexType>
+                    </xsd:element>
+                  </xsd:sequence>
+                  <xsd:attribute name="Id" type="xsd:ID" use="prohibited"/>
+                </xsd:complexType>
+              </xsd:element>
+              <xsd:element maxOccurs="unbounded" name="Contains">
+                <xsd:complexType>
+                  <xsd:sequence>
+                    <xsd:element name="Name">
+                      <xsd:complexType>
+                        <xsd:simpleContent>
+                          <xsd:extension base="udt:TextType">
+                            <xsd:attribute name="languageID" type="xsd:language" use="prohibited"/>
+                          </xsd:extension>
+                        </xsd:simpleContent>
+                      </xsd:complexType>
+                    </xsd:element>
+                    <xsd:element name="DescriptionLevel">
+                      <xsd:complexType>
+                        <xsd:simpleContent>
+                          <xsd:extension base="qdt:CodeDescriptionLevelType">
+                            <xsd:attribute fixed="edition 2009" name="listVersionID" type="xsd:token" use="required"/>
+                          </xsd:extension>
+                        </xsd:simpleContent>
+                      </xsd:complexType>
+                    </xsd:element>
+                  </xsd:sequence>
+                  <xsd:attribute name="Id" type="xsd:ID" use="prohibited"/>
+                </xsd:complexType>
+              </xsd:element>
+              <xsd:element maxOccurs="unbounded" minOccurs="0" name="Document">
+                <xsd:complexType>
+                  <xsd:sequence>
+                    <xsd:element name="Attachment">
+                      <xsd:complexType>
+                        <xsd:simpleContent>
+                          <xsd:extension base="qdt:ArchivesBinaryObjectType">
+                            <xsd:attribute fixed="fmt/123" name="format" type="clmDAFFileTypeCode:FileTypeCodeType" use="required"/>
+                            <xsd:attribute fixed="6" name="encodingCode" type="clm60133:CharacterSetEncodingCodeContentType" use="required"/>
+                            <xsd:attribute name="characterSetCode" type="clmIANACharacterSetCode:CharacterSetCodeContentType" use="prohibited"/>
+                            <xsd:attribute name="mimeCode" type="clmIANAMIMEMediaType:MIMEMediaTypeContentType" use="prohibited"/>
+                            <xsd:attribute name="uri" type="xsd:anyURI" use="prohibited"/>
+                            <xsd:attribute name="filename" type="xsd:string" use="prohibited"/>
+                          </xsd:extension>
+                        </xsd:simpleContent>
+                      </xsd:complexType>
+                    </xsd:element>
+                    <xsd:element fixed="CDO" name="Type">
+                      <xsd:complexType>
+                        <xsd:simpleContent>
+                          <xsd:extension base="qdt:CodeDocumentType">
+                            <xsd:attribute fixed="edition 2009" name="listVersionID" type="xsd:token" use="required"/>
+                          </xsd:extension>
+                        </xsd:simpleContent>
+                      </xsd:complexType>
+                    </xsd:element>
+                  </xsd:sequence>
+                  <xsd:attribute name="Id" type="xsd:ID" use="prohibited"/>
+                </xsd:complexType>
+              </xsd:element>
+            </xsd:sequence>
+            <xsd:attribute name="Id" type="xsd:ID" use="prohibited"/>
+          </xsd:complexType>
+        </xsd:element>
+      </xsd:sequence>
+      <xsd:attribute name="Id" type="xsd:ID" use="prohibited"/>
+    </xsd:complexType>
+  </xsd:element>
+</xsd:schema>
--- a/test/test_profile_generation.py	Fri Oct 07 13:21:35 2016 +0200
+++ b/test/test_profile_generation.py	Fri Oct 07 13:22:26 2016 +0200
@@ -780,6 +780,9 @@
     def test_seda_1_0(self):
         self._test_profile_xsd('SEDA-1.0.xsd', 'seda_1_export.xsd')
 
+    def test_seda_0_2(self):
+        self._test_profile_xsd('SEDA-0.2.xsd', 'seda_02_export.xsd')
+
     def _test_profile_xsd(self, adapter_id, expected_file):
         with self.admin_access.client_cnx() as cnx:
             transfer = cnx.entity_from_eid(self.transfer_eid)