From: Raphael Michel Date: Tue, 16 Oct 2018 05:02:44 +0000 (+0200) Subject: Ensure object order on Python 3.5 X-Git-Tag: 1.0.0~15 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7c04c542c08f0ba1875be777ce9e622ceba251d1;p=thirdparty%2Fpython-drafthorse.git Ensure object order on Python 3.5 --- diff --git a/drafthorse/models/elements.py b/drafthorse/models/elements.py index 7c5a2da..f70f234 100644 --- a/drafthorse/models/elements.py +++ b/drafthorse/models/elements.py @@ -1,3 +1,4 @@ +import collections import sys import xml.etree.cElementTree as ET from collections import OrderedDict @@ -12,6 +13,10 @@ from .fields import Field class BaseElementMeta(type): + @classmethod + def __prepare__(self, name, bases): + return collections.OrderedDict() + def __new__(mcls, name, bases, attrs): cls = super(BaseElementMeta, mcls).__new__(mcls, name, bases, attrs) fields = list(cls._fields) if hasattr(cls, '_fields') else [] @@ -60,8 +65,10 @@ class Element(metaclass=BaseElementMeta): return validate_xml(xmlout=xml, schema="ZUGFeRD1p0") def from_etree(self, root): - if hasattr(self, 'Meta') and hasattr(self.Meta, 'namespace') and root.tag != "{%s}%s" % (self.Meta.namespace, self.Meta.tag): - raise TypeError("Invalid XML, found tag {} where {} was expected".format(root.tag, "{%s}%s" % (self.Meta.namespace, self.Meta.tag))) + if hasattr(self, 'Meta') and hasattr(self.Meta, 'namespace') and root.tag != "{%s}%s" % ( + self.Meta.namespace, self.Meta.tag): + raise TypeError("Invalid XML, found tag {} where {} was expected".format(root.tag, "{%s}%s" % ( + self.Meta.namespace, self.Meta.tag))) field_index = {} for field in self._fields: element = getattr(self, field.name) @@ -76,7 +83,6 @@ class Element(metaclass=BaseElementMeta): else: getattr(self, name).from_etree(child) else: - print(root, field_index) raise TypeError("Unknown element {}".format(child.tag)) return self