From 8407f091392c91dd5492c30ed82ac4ca5b03779d Mon Sep 17 00:00:00 2001 From: Davide Brunato Date: Tue, 15 Oct 2019 21:40:40 +0200 Subject: [PATCH] Add LOCATION_HINTS dict to namespaces module - Use LOCATION_HINTS for schema's fallback locations --- doc/conf.py | 2 +- publiccode.yml | 4 +- setup.py | 2 +- xmlschema/__init__.py | 2 +- xmlschema/namespaces.py | 24 ++++++++++++ .../tests/validators/test_schema_class.py | 5 ++- xmlschema/validators/schema.py | 39 +++++++------------ 7 files changed, 45 insertions(+), 33 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index c0eed6c..90a672d 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -62,7 +62,7 @@ author = 'Davide Brunato' # The short X.Y version. version = '1.0' # The full version, including alpha/beta/rc tags. -release = '1.0.15' +release = '1.0.16' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/publiccode.yml b/publiccode.yml index 6ecb1e0..4770e55 100644 --- a/publiccode.yml +++ b/publiccode.yml @@ -6,8 +6,8 @@ publiccodeYmlVersion: '0.2' name: xmlschema url: 'https://github.com/sissaschool/xmlschema' landingURL: 'https://github.com/sissaschool/xmlschema' -releaseDate: '2019-10-13' -softwareVersion: v1.0.15 +releaseDate: '2019-10-XX' +softwareVersion: v1.0.16 developmentStatus: stable platforms: - linux diff --git a/setup.py b/setup.py index 603a73e..2e5472c 100755 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ class InstallCommand(install): setup( name='xmlschema', - version='1.0.15', + version='1.0.16', install_requires=['elementpath~=1.3.0'], packages=['xmlschema'], include_package_data=True, diff --git a/xmlschema/__init__.py b/xmlschema/__init__.py index e9adc67..d800a17 100644 --- a/xmlschema/__init__.py +++ b/xmlschema/__init__.py @@ -30,7 +30,7 @@ from .validators import ( XsdGlobals, XMLSchemaBase, XMLSchema, XMLSchema10, XMLSchema11 ) -__version__ = '1.0.15' +__version__ = '1.0.16' __author__ = "Davide Brunato" __contact__ = "brunato@sissa.it" __copyright__ = "Copyright 2016-2019, SISSA" diff --git a/xmlschema/namespaces.py b/xmlschema/namespaces.py index beff6c6..44cd453 100644 --- a/xmlschema/namespaces.py +++ b/xmlschema/namespaces.py @@ -12,10 +12,13 @@ This module contains namespace definitions for W3C core standards and namespace related classes. """ from __future__ import unicode_literals +import os import re from .compat import MutableMapping, Mapping +### +# Namespace URIs XSD_NAMESPACE = 'http://www.w3.org/2001/XMLSchema' "URI of the XML Schema Definition namespace (xs|xsd)" @@ -42,6 +45,27 @@ VC_NAMESPACE = 'http://www.w3.org/2007/XMLSchema-versioning' "URI of the XML Schema Versioning namespace (vc)" +### +# Schema location hints + +SCHEMAS_DIR = os.path.join(os.path.dirname(__file__), 'validators/schemas/') + +LOCATION_HINTS = { + # Locally saved schemas + HFP_NAMESPACE: os.path.join(SCHEMAS_DIR, 'XMLSchema-hasFacetAndProperty_minimal.xsd'), + VC_NAMESPACE: os.path.join(SCHEMAS_DIR, 'XMLSchema-versioning_minimal.xsd'), + XLINK_NAMESPACE: os.path.join(SCHEMAS_DIR, 'xlink.xsd'), + XHTML_NAMESPACE: os.path.join(SCHEMAS_DIR, 'xhtml1-strict.xsd'), + + # Remote locations: contributors can propose additional official locations + # for other namespaces for extending this list. + XSLT_NAMESPACE: os.path.join(SCHEMAS_DIR, 'http://www.w3.org/2007/schema-for-xslt20.xsd'), +} + + +### +# Helper functions and classes + NAMESPACE_PATTERN = re.compile(r'{([^}]*)}') diff --git a/xmlschema/tests/validators/test_schema_class.py b/xmlschema/tests/validators/test_schema_class.py index fb94fe1..45be457 100644 --- a/xmlschema/tests/validators/test_schema_class.py +++ b/xmlschema/tests/validators/test_schema_class.py @@ -13,9 +13,11 @@ from __future__ import print_function, unicode_literals import unittest import platform import warnings +import os from xmlschema import XMLSchemaParseError, XMLSchemaIncludeWarning, XMLSchemaImportWarning from xmlschema.etree import etree_element +from xmlschema.namespaces import SCHEMAS_DIR from xmlschema.qnames import XSD_ELEMENT, XSI_TYPE from xmlschema.tests import SKIP_REMOTE_TESTS, XsdValidatorTestCase from xmlschema.validators import XMLSchema11 @@ -113,8 +115,7 @@ class TestXMLSchema10(XsdValidatorTestCase): """, XMLSchemaParseError) def test_base_schemas(self): - from xmlschema.validators.schema import XML_SCHEMA_FILE - self.schema_class(XML_SCHEMA_FILE) + self.schema_class(os.path.join(SCHEMAS_DIR, 'xml_minimal.xsd')) def test_root_elements(self): # Test issue #107 fix diff --git a/xmlschema/validators/schema.py b/xmlschema/validators/schema.py index 321809f..1277d26 100644 --- a/xmlschema/validators/schema.py +++ b/xmlschema/validators/schema.py @@ -32,8 +32,8 @@ from ..qnames import VC_MIN_VERSION, VC_MAX_VERSION, VC_TYPE_AVAILABLE, \ XSD_ALL, XSD_ANY, XSD_ANY_ATTRIBUTE, XSD_INCLUDE, XSD_IMPORT, XSD_REDEFINE, \ XSD_OVERRIDE, XSD_DEFAULT_OPEN_CONTENT from ..helpers import get_xsd_derivation_attribute, get_xsd_form_attribute -from ..namespaces import XSD_NAMESPACE, XML_NAMESPACE, XSI_NAMESPACE, XHTML_NAMESPACE, \ - XLINK_NAMESPACE, VC_NAMESPACE, NamespaceResourcesMap, NamespaceView +from ..namespaces import XSD_NAMESPACE, XML_NAMESPACE, XSI_NAMESPACE, VC_NAMESPACE, \ + SCHEMAS_DIR, LOCATION_HINTS, NamespaceResourcesMap, NamespaceView from ..etree import etree_element, etree_tostring, prune_etree, ParseError from ..resources import is_remote_url, url_path_is_file, fetch_resource, XMLResource from ..converters import XMLSchemaConverter @@ -75,14 +75,6 @@ ANY_ELEMENT = etree_element( 'maxOccurs': 'unbounded' }) -# XSD schemas of W3C standards -SCHEMAS_DIR = os.path.join(os.path.dirname(__file__), 'schemas/') -XML_SCHEMA_FILE = os.path.join(SCHEMAS_DIR, 'xml_minimal.xsd') -XSI_SCHEMA_FILE = os.path.join(SCHEMAS_DIR, 'XMLSchema-instance_minimal.xsd') -XLINK_SCHEMA_FILE = os.path.join(SCHEMAS_DIR, 'xlink.xsd') -XHTML_SCHEMA_FILE = os.path.join(SCHEMAS_DIR, 'xhtml1-strict.xsd') -VC_SCHEMA_FILE = os.path.join(SCHEMAS_DIR, 'XMLSchema-versioning_minimal.xsd') - class XMLSchemaMeta(ABCMeta): @@ -1024,14 +1016,15 @@ class XMLSchemaBase(XsdValidator, ValidationMixin, ElementPathMixin): warnings.warn(self.warnings[-1], XMLSchemaImportWarning, stacklevel=3) self.imports[namespace] = None - def import_schema(self, namespace, location, base_url=None, force=False): + def import_schema(self, namespace, location, base_url=None, force=False, build=False): """ Imports a schema for an external namespace, from a specific URL. :param namespace: is the URI of the external namespace. :param location: is the URL of the schema. :param base_url: is an optional base URL for fetching the schema resource. - :param force: is set to `True` imports the schema also if the namespace is already imported. + :param force: if set to `True` imports the schema also if the namespace is already imported. + :param build: defines when to build the imported schema, the default is to not build. :return: the imported :class:`XMLSchema` instance. """ if not force: @@ -1058,7 +1051,7 @@ class XMLSchemaBase(XsdValidator, ValidationMixin, ElementPathMixin): base_url=self.base_url, defuse=self.defuse, timeout=self.timeout, - build=False, + build=build, ) if schema.target_namespace != namespace: raise XMLSchemaValueError('imported schema %r has an unmatched namespace %r' % (location, namespace)) @@ -1487,13 +1480,10 @@ class XMLSchema10(XMLSchemaBase): } meta_schema = os.path.join(SCHEMAS_DIR, 'XSD_1.0/XMLSchema.xsd') BASE_SCHEMAS = { - XML_NAMESPACE: XML_SCHEMA_FILE, - XSI_NAMESPACE: XSI_SCHEMA_FILE, - } - FALLBACK_LOCATIONS = { - XLINK_NAMESPACE: XLINK_SCHEMA_FILE, - XHTML_NAMESPACE: XHTML_SCHEMA_FILE, + XML_NAMESPACE: os.path.join(SCHEMAS_DIR, 'xml_minimal.xsd'), + XSI_NAMESPACE: os.path.join(SCHEMAS_DIR, 'XMLSchema-instance_minimal.xsd'), } + FALLBACK_LOCATIONS = LOCATION_HINTS # ++++ UNDER DEVELOPMENT, DO NOT USE!!! ++++ @@ -1550,15 +1540,12 @@ class XMLSchema11(XMLSchemaBase): } meta_schema = os.path.join(SCHEMAS_DIR, 'XSD_1.1/XMLSchema.xsd') BASE_SCHEMAS = { + XML_NAMESPACE: os.path.join(SCHEMAS_DIR, 'xml_minimal.xsd'), + XSI_NAMESPACE: os.path.join(SCHEMAS_DIR, 'XMLSchema-instance_minimal.xsd'), XSD_NAMESPACE: os.path.join(SCHEMAS_DIR, 'XSD_1.1/xsd11-extra.xsd'), - XML_NAMESPACE: XML_SCHEMA_FILE, - XSI_NAMESPACE: XSI_SCHEMA_FILE, - VC_NAMESPACE: VC_SCHEMA_FILE, - } - FALLBACK_LOCATIONS = { - XLINK_NAMESPACE: XLINK_SCHEMA_FILE, - XHTML_NAMESPACE: XHTML_SCHEMA_FILE, + VC_NAMESPACE: os.path.join(SCHEMAS_DIR, 'XMLSchema-versioning_minimal.xsd'), } + FALLBACK_LOCATIONS = LOCATION_HINTS def _parse_inclusions(self): super(XMLSchema11, self)._parse_inclusions()