Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhayes committed Nov 3, 2016
2 parents cef4236 + dd9aad0 commit 9faf66a
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -91,3 +91,6 @@ ENV/
# direnv
.envrc
.direnv

# pycharm
.idea
10 changes: 10 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,13 @@
# Release 0.2.0 - Thursday 3 November 23:49:32 AEDT 2016

- Allow user to set XML encoding (#6)
- Added encoding parameter to to_xml
- Changed default encoding to UTF-8.
- Added XMLModel attribute xml_encoding with default 'UTF-8'.
- Added docs detailing how to set the encoding.
- Ensure deeply nested lists are traversed even if the parent is OK. (#5)
- Test schematics serialized_name BaseType feature.

# Release 0.1.2 - Wednesday 2 November 23:13:48 AEDT 2016

- Support lists with a single item in XML being converted to raw_data with a list not dict. (#2)
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -57,7 +57,7 @@ XML now contains;

.. code-block:: xml
<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml version='1.0' encoding='UTF-8'?>
<person>
<name>John</name>
</person>
Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.1.2
0.2.0
33 changes: 31 additions & 2 deletions docs/usage.rst
Expand Up @@ -19,7 +19,7 @@ XML now contains;

.. code-block:: xml
<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml version='1.0' encoding='UTF-8'?>
<person>
<name>John</name>
</person>
Expand Down Expand Up @@ -55,7 +55,36 @@ XML now contains;

.. code-block:: xml
<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml version='1.0' encoding='UTF-8'?>
<cat>
<kind>cat</kind>
</cat>
Encoding
--------

By default the encoding returned :py:meth:`.XMLModel.to_xml` is `UTF-8` however
this can be changed either by setting the `xml_encoding` attribute on the model
or by setting the `encoding` kwarg when calling :py:meth:`.XMLModel.to_xml`.

.. code-block:: python
from schematics_xml import XMLModel
class Animal(XMLModel):
xml_encoding = 'UTF-8'
kind = StringType()
garfield = Animal(dict(kind='cat'))
xml = garfield.to_xml()
XML now contains;

.. code-block:: xml
<?xml version='1.0' encoding='UTF-8'?>
<cat>
<animal>cat</animal>
</cat>
2 changes: 1 addition & 1 deletion schematics_xml/__init__.py
Expand Up @@ -10,7 +10,7 @@
'VersionInfo', ('major', 'minor', 'micro', 'releaselevel', 'serial'),
)

VERSION = VersionInfo(0, 1, 2, '', '')
VERSION = VersionInfo(0, 2, 0, '', '')
__version__ = '{0.major}.{0.minor}.{0.micro}{0.releaselevel}'.format(VERSION)
__author__ = 'Alex Hayes'
__contact__ = 'alex@alution.com'
Expand Down
27 changes: 20 additions & 7 deletions schematics_xml/models.py
Expand Up @@ -23,17 +23,24 @@ class XMLModel(Model):
A model that can convert it's fields to and from XML.
"""
@property
def xml_root(self):
def xml_root(self) -> str:
"""
Override this attribute to set the XML root returned by :py:meth:`.XMLModel.to_xml`.
"""
return type(self).__name__.lower()

def to_xml(self, role: str=None, app_data: dict=None, **kwargs) -> str:
#: Override this attribute to set the encoding specified in the XML returned by :py:meth:`.XMLModel.to_xml`.
xml_encoding = 'UTF-8'

def to_xml(self, role: str=None, app_data: dict=None, encoding: str=None, **kwargs) -> str:
"""
Return a string of XML that represents this model.
Currently all arguments are passed through to schematics.Model.to_primitive.
:param role: schematics Model to_primitive role parameter.
:param app_data: schematics Model to_primitive app_data parameter.
:param encoding: xml encoding attribute string.
:param kwargs: schematics Model to_primitive kwargs parameter.
"""
primitive = self.to_primitive(role=role, app_data=app_data, **kwargs)
Expand All @@ -42,7 +49,7 @@ def to_xml(self, role: str=None, app_data: dict=None, **kwargs) -> str:
root,
pretty_print=True,
xml_declaration=True,
encoding='ISO-8859-1'
encoding=encoding or self.xml_encoding
)

def primitive_to_xml(self, primitive: dict, parent: 'lxml.etree._Element'=None):
Expand Down Expand Up @@ -184,10 +191,16 @@ def ensure_lists_in_model(raw_data: dict, model_cls: XMLModel):

def ensure_lists_in_value(value: 'typing.Any', field: BaseType):

if isinstance(field, ListType) and not isinstance(value, list):
value = [
ensure_lists_in_value(value, field.field)
]
if isinstance(field, ListType):
if not isinstance(value, list):
value = [
ensure_lists_in_value(value, field.field)
]
elif field_has_type(ListType, field.field):
value = [
ensure_lists_in_value(_value, field.field)
for _value in value
]

elif field_has_type(ListType, field):
if isinstance(field, DictType):
Expand Down

0 comments on commit 9faf66a

Please sign in to comment.