Skip to content

Commit

Permalink
Merge branch 'develop' for updating to release v1.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
brunato committed Feb 11, 2021
2 parents e5e6782 + 183643c commit 4e29c69
Show file tree
Hide file tree
Showing 36 changed files with 747 additions and 298 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
CHANGELOG
*********

`v1.5.1`_ (2021-02-11)
======================
* Optimize NamespaceView read-only mapping
* Add experimental XML data bindings with a DataBindingConverter
* Add experimental PythonGenerator for static codegen with Jinja2

`v1.5.0`_ (2021-02-05)
======================
* Add DataElement class for creating objects with schema bindings
Expand Down Expand Up @@ -402,3 +408,4 @@ v0.9.6 (2017-05-05)
.. _v1.4.1: https://github.com/brunato/xmlschema/compare/v1.4.0...v1.4.1
.. _v1.4.2: https://github.com/brunato/xmlschema/compare/v1.4.1...v1.4.2
.. _v1.5.0: https://github.com/brunato/xmlschema/compare/v1.4.2...v1.5.0
.. _v1.6.0: https://github.com/brunato/xmlschema/compare/v1.5.0...v1.5.1
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ This library includes the following features:
* An XPath based API for finding schema's elements and attributes
* Support of XSD validation modes *strict*/*lax*/*skip*
* Remote attacks protection by default using an XMLParser that forbids entities
* XML data bindings based on DataElement class (experimental)
* Static code generation with Jinja2 templates (experimental)


Installation
Expand Down
8 changes: 5 additions & 3 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ The base class `XMLSchemaConverter` is used for defining generic converters.
The subclasses implement some of the most used `conventions for converting XML
to JSON data <http://wiki.open311.org/JSON_and_XML_Conversion/>`_.

.. autoclass:: xmlschema.ElementData

.. autoclass:: xmlschema.XMLSchemaConverter

.. autoattribute:: lossy
Expand Down Expand Up @@ -159,14 +161,15 @@ to JSON data <http://wiki.open311.org/JSON_and_XML_Conversion/>`_.

.. autoclass:: xmlschema.ColumnarConverter

.. autoclass:: xmlschema.DataElementConverter

.. _data-objects-api:

Data objects API
================

.. autoclass:: xmlschema.ElementData
.. autoclass:: xmlschema.DataElement
.. autoclass:: xmlschema.DataElementConverter
.. autoclass:: xmlschema.DataBindingConverter


.. _xml-resource-api:
Expand Down Expand Up @@ -405,7 +408,6 @@ Code generators
.. automethod:: render_to_files



.. autoclass:: xmlschema.extras.codegen.PythonGenerator


Expand Down
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
# The short X.Y version.
version = '1.5'
# The full version, including alpha/beta/rc tags.
release = '1.5.0'
release = '1.5.1'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
5 changes: 2 additions & 3 deletions doc/extras.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
Extra features
**************

The subpackage *xmlschema.extras* contThe subpackage *xmlschema.extras*
acts as a container of a set of extra modules or subpackages that can be
useful for specific needs.
The subpackage *xmlschema.extras* acts as a container of a set of extra
modules or subpackages that can be useful for specific needs.

These codes are not imported during normal library usage and may require
additional dependencies to be installed. This choice should be facilitate
Expand Down
4 changes: 2 additions & 2 deletions publiccode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ publiccodeYmlVersion: '0.2'
name: xmlschema
url: 'https://github.com/sissaschool/xmlschema'
landingURL: 'https://github.com/sissaschool/xmlschema'
releaseDate: '2021-02-05'
softwareVersion: v1.5.0
releaseDate: '2021-02-11'
softwareVersion: v1.5.1
developmentStatus: stable
platforms:
- linux
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name='xmlschema',
version='1.5.0',
version='1.5.1',
packages=find_packages(include=['xmlschema', 'xmlschema.*']),
include_package_data=True,
entry_points={
Expand Down
27 changes: 27 additions & 0 deletions tests/test_cases/examples/collection/collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Copyright (c), 2016-2021, SISSA (International School for Advanced Studies).
# All rights reserved.
# This file is distributed under the terms of the MIT License.
# See the file 'LICENSE' in the root directory of the present
# distribution, or http://opensource.org/licenses/MIT.
#
# Auto-generated code: don't edit this file
#
"""
Sample of XML data bindings for schema collection.xsd
"""
from xmlschema import XMLSchema, DataElement
from xmlschema.dataobjects import DataBindingMeta

__NAMESPACE__ = "http://example.com/ns/collection"

schema = XMLSchema("collection.xsd")


class CollectionBinding(DataElement, metaclass=DataBindingMeta):
xsd_element = schema.elements['collection']


class PersonBinding(DataElement, metaclass=DataBindingMeta):
xsd_element = schema.elements['person']

61 changes: 40 additions & 21 deletions tests/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
"""Tests concerning XSD based code generators. Requires jinja2 optional dependency."""

import unittest
import os
import datetime
import ast
import platform
import importlib.util
from pathlib import Path
from textwrap import dedent
from xml.etree import ElementTree

from xmlschema import XMLSchema10, XMLSchema11
from xmlschema.names import XSD_ANY_TYPE, XSD_STRING, XSD_FLOAT

Expand All @@ -28,7 +32,7 @@
PythonGenerator = None
DemoGenerator = None
else:
from xmlschema.extras.codegen import filter_method, AbstractGenerator
from xmlschema.extras.codegen import filter_method, AbstractGenerator, PythonGenerator

class DemoGenerator(AbstractGenerator):
formal_language = 'Demo'
Expand Down Expand Up @@ -68,26 +72,6 @@ def not_an_instance_filter(self):
return


class PythonGenerator(AbstractGenerator):
"""
Python code sample generator for XSD schemas.
"""
formal_language = 'Python'

searchpaths = ['templates/python/']

builtin_types = {
'string': 'str',
'boolean': 'bool',
'float': 'float',
'double': 'float',
'integer': 'int',
'unsignedByte': 'int',
'nonNegativeInteger': 'int',
'positiveInteger': 'int',
}


def casepath(relative_path):
return str(Path(__file__).absolute().parent.joinpath('test_cases', relative_path))

Expand Down Expand Up @@ -347,13 +331,48 @@ def test_language_type_filter(self):
self.generator.render('python_type_filter_test.jinja'), ['str']
)

def test_list_templates(self):
template_dir = Path(__file__).parent.joinpath('templates')
language = self.generator_class.formal_language.lower()

templates = {'sample.py.jinja', 'bindings.py.jinja'}
templates.update(x.name for x in template_dir.glob('filters/*'.format(language)))
self.assertSetEqual(set(self.generator.list_templates()), templates)

def test_sample_module(self):
generator = PythonGenerator(self.col_xsd_file)

python_module = generator.render('sample.py.jinja')[0]
ast_module = ast.parse(python_module)
self.assertIsInstance(ast_module, ast.Module)

def test_bindings_module(self):
generator = PythonGenerator(self.col_xsd_file)

python_module = generator.render('bindings.py.jinja')[0]

ast_module = ast.parse(python_module)
self.assertIsInstance(ast_module, ast.Module)

collection_dir = Path(__file__).parent.joinpath('test_cases/examples/collection')
cwd = os.getcwd()
try:
os.chdir(str(collection_dir))
with open('collection.py', 'w') as fp:
fp.write(python_module)

spec = importlib.util.spec_from_file_location('collection', 'collection.py')
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
except Exception:
pass
else:
col_data = module.CollectionBinding.fromsource('collection.xml')
col_root = ElementTree.XML(col_data.tostring())
self.assertEqual(col_root.tag, '{http://example.com/ns/collection}collection')
finally:
os.chdir(cwd)


if __name__ == '__main__':
import platform
Expand Down
9 changes: 4 additions & 5 deletions tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
# @author Davide Brunato <brunato@sissa.it>
#
import unittest
import os
import xml.etree.ElementTree as ElementTree
from pathlib import Path

try:
import lxml.etree as lxml_etree
Expand All @@ -24,13 +24,12 @@

from xmlschema.converters import XMLSchemaConverter, UnorderedConverter, \
ParkerConverter, BadgerFishConverter, AbderaConverter, JsonMLConverter, \
ColumnarConverter, DataElementConverter
ColumnarConverter
from xmlschema.dataobjects import DataElementConverter


class TestConverters(unittest.TestCase):

TEST_CASES_DIR = os.path.join(os.path.dirname(__file__), 'test_cases')

@classmethod
def setUpClass(cls):
cls.col_xsd_filename = cls.casepath('examples/collection/collection.xsd')
Expand All @@ -42,7 +41,7 @@ def setUpClass(cls):

@classmethod
def casepath(cls, relative_path):
return os.path.join(cls.TEST_CASES_DIR, relative_path)
return str(Path(__file__).parent.joinpath('test_cases', relative_path))

def test_element_class_argument(self):
converter = XMLSchemaConverter()
Expand Down
Loading

0 comments on commit 4e29c69

Please sign in to comment.