maxml 1.0.3__tar.gz → 1.0.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: maxml
3
- Version: 1.0.3
3
+ Version: 1.0.4
4
4
  Summary: A streamlined pure Python XML serializer.
5
5
  Author: Daniel Sissman
6
6
  License-Expression: MIT
@@ -115,10 +115,10 @@ The `Element` class constructor `Element(...)` takes the following arguments:
115
115
 
116
116
  The `Element` class provides the following methods:
117
117
 
118
- * `register_namespace(prefix: str, uri: str)` The `register_namespace()` method
119
- supports registering namespaces globally for the module or per instance depending
120
- on whether the method is called on the class directly or whether it is called on a
121
- specific instance of the class.
118
+ * `register_namespace(prefix: str, uri: str, promoted: bool = False)`
119
+ The `register_namespace()` method supports registering namespaces globally for the
120
+ module or per instance depending on whether the method is called on the class
121
+ directly or whether it is called on a specific instance of the class.
122
122
 
123
123
  If a namespace is registered globally for the module, the registered namespaces
124
124
  become available for use by any instance of the class created within the program
@@ -132,6 +132,12 @@ The `Element` class provides the following methods:
132
132
 
133
133
  Each namespace consists of a prefix which can be used to prefix element names
134
134
  and the URI associated with that namespace prefix.
135
+
136
+ Optionally, a namespace can be marked as promoted during registration, which will
137
+ result in the namespace being serialized into the XML before any attributes on the
138
+ element. Namespaces that are not marked as promoted will appear after attributes.
139
+ Namespace promotion can be enabled for a given namespace during registration by
140
+ passing the optional `promoted` keyword argument with the value of `True`.
135
141
 
136
142
  For example, the 'rdf' prefix is associated with the following canonical URI:
137
143
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
@@ -84,10 +84,10 @@ The `Element` class constructor `Element(...)` takes the following arguments:
84
84
 
85
85
  The `Element` class provides the following methods:
86
86
 
87
- * `register_namespace(prefix: str, uri: str)` The `register_namespace()` method
88
- supports registering namespaces globally for the module or per instance depending
89
- on whether the method is called on the class directly or whether it is called on a
90
- specific instance of the class.
87
+ * `register_namespace(prefix: str, uri: str, promoted: bool = False)`
88
+ The `register_namespace()` method supports registering namespaces globally for the
89
+ module or per instance depending on whether the method is called on the class
90
+ directly or whether it is called on a specific instance of the class.
91
91
 
92
92
  If a namespace is registered globally for the module, the registered namespaces
93
93
  become available for use by any instance of the class created within the program
@@ -101,6 +101,12 @@ The `Element` class provides the following methods:
101
101
 
102
102
  Each namespace consists of a prefix which can be used to prefix element names
103
103
  and the URI associated with that namespace prefix.
104
+
105
+ Optionally, a namespace can be marked as promoted during registration, which will
106
+ result in the namespace being serialized into the XML before any attributes on the
107
+ element. Namespaces that are not marked as promoted will appear after attributes.
108
+ Namespace promotion can be enabled for a given namespace during registration by
109
+ passing the optional `promoted` keyword argument with the value of `True`.
104
110
 
105
111
  For example, the 'rdf' prefix is associated with the following canonical URI:
106
112
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
@@ -28,7 +28,7 @@ class Element(object):
28
28
  _mixed: bool = False
29
29
 
30
30
  @hybridmethod
31
- def register_namespace(self, prefix: str, uri: str):
31
+ def register_namespace(self, prefix: str, uri: str, promoted: bool = False):
32
32
  """Supports registering namespaces globally for the module or per instance
33
33
  depending on whether the method is called on the class directly or whether it is
34
34
  called on a specific instance of the class.
@@ -74,6 +74,9 @@ class Element(object):
74
74
  if not isinstance(uri, str):
75
75
  raise TypeError("The 'uri' argument must have a string value!")
76
76
 
77
+ if not isinstance(promoted, bool):
78
+ raise TypeError("The 'promoted' argument must have a boolean value!")
79
+
77
80
  for namespace in self._namespaces:
78
81
  if namespace.prefix == prefix:
79
82
  if namespace.uri == uri:
@@ -88,7 +91,7 @@ class Element(object):
88
91
  % (prefix, uri, namespace.uri)
89
92
  )
90
93
  else:
91
- if namespace := Namespace(prefix=prefix, uri=uri):
94
+ if namespace := Namespace(prefix=prefix, uri=uri, promoted=promoted):
92
95
  self._namespaces.add(namespace)
93
96
 
94
97
  def __init__(
@@ -186,7 +189,7 @@ class Element(object):
186
189
  if namespace.uri == uri:
187
190
  break
188
191
  else:
189
- namespace = Namespace(prefix=prefix, uri=uri)
192
+ namespace = Namespace(prefix=prefix, uri=uri, promoted=False)
190
193
 
191
194
  self.__class__._namespaces.add(namespace)
192
195
 
@@ -662,7 +665,7 @@ class Element(object):
662
665
  # Add any promoted namespaces (those which should proceed any attributes)
663
666
  count = len(element.namespaced)
664
667
  for index, namespace in enumerate(element.namespaced, start=1):
665
- if not namespace.promoted:
668
+ if namespace.promoted is False:
666
669
  continue
667
670
 
668
671
  if pretty and count > 1 and (newline or (index > 1 and index <= count)):
@@ -691,7 +694,7 @@ class Element(object):
691
694
  newline = True
692
695
 
693
696
  for index, namespace in enumerate(element.namespaced, start=1):
694
- if namespace.promoted:
697
+ if namespace.promoted is True:
695
698
  continue
696
699
 
697
700
  if pretty and count > 1 and (newline or (index > 1 and index <= count)):
@@ -14,7 +14,7 @@ class Namespace(object):
14
14
  _uri: str = None
15
15
  _promoted: bool = False
16
16
 
17
- def __init__(self, prefix: str, uri: str):
17
+ def __init__(self, prefix: str, uri: str, promoted: bool = False):
18
18
  """Initialize the Namespace class"""
19
19
 
20
20
  if not isinstance(prefix, str):
@@ -27,6 +27,11 @@ class Namespace(object):
27
27
 
28
28
  self._uri = uri
29
29
 
30
+ if not isinstance(promoted, bool):
31
+ raise TypeError("The 'promoted' argument must have a boolean value!")
32
+
33
+ self._promoted = promoted
34
+
30
35
  def __str__(self) -> str:
31
36
  """Return a string representation of the class for debugging purposes."""
32
37
 
@@ -0,0 +1 @@
1
+ 1.0.4
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: maxml
3
- Version: 1.0.3
3
+ Version: 1.0.4
4
4
  Summary: A streamlined pure Python XML serializer.
5
5
  Author: Daniel Sissman
6
6
  License-Expression: MIT
@@ -115,10 +115,10 @@ The `Element` class constructor `Element(...)` takes the following arguments:
115
115
 
116
116
  The `Element` class provides the following methods:
117
117
 
118
- * `register_namespace(prefix: str, uri: str)` The `register_namespace()` method
119
- supports registering namespaces globally for the module or per instance depending
120
- on whether the method is called on the class directly or whether it is called on a
121
- specific instance of the class.
118
+ * `register_namespace(prefix: str, uri: str, promoted: bool = False)`
119
+ The `register_namespace()` method supports registering namespaces globally for the
120
+ module or per instance depending on whether the method is called on the class
121
+ directly or whether it is called on a specific instance of the class.
122
122
 
123
123
  If a namespace is registered globally for the module, the registered namespaces
124
124
  become available for use by any instance of the class created within the program
@@ -132,6 +132,12 @@ The `Element` class provides the following methods:
132
132
 
133
133
  Each namespace consists of a prefix which can be used to prefix element names
134
134
  and the URI associated with that namespace prefix.
135
+
136
+ Optionally, a namespace can be marked as promoted during registration, which will
137
+ result in the namespace being serialized into the XML before any attributes on the
138
+ element. Namespaces that are not marked as promoted will appear after attributes.
139
+ Namespace promotion can be enabled for a given namespace during registration by
140
+ passing the optional `promoted` keyword argument with the value of `True`.
135
141
 
136
142
  For example, the 'rdf' prefix is associated with the following canonical URI:
137
143
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
@@ -461,3 +461,49 @@ def test_maxml_special_tostring(data: callable):
461
461
  assert isinstance(compare, str)
462
462
 
463
463
  assert string == compare
464
+
465
+
466
+ def test_maxml_namespace_promotion_non_promoted_namespace(data: callable):
467
+ """Check promotion of registered namespaces works as expected"""
468
+
469
+ maxml.Element.register_namespace(
470
+ prefix="my1", uri="http://namespace.example.org/my1", promoted=False
471
+ )
472
+
473
+ element = maxml.Element(name="my1:test")
474
+
475
+ # Ensure that the element object's type is as expected
476
+ assert isinstance(element, maxml.Element)
477
+
478
+ element.set("my1:attribute", "1234")
479
+
480
+ string: str = element.tostring(pretty=True)
481
+
482
+ assert isinstance(string, str)
483
+
484
+ compare: str = data("examples/example04namespace-unpromoted.xml")
485
+
486
+ assert string == compare
487
+
488
+
489
+ def test_maxml_namespace_promotion_promoted_namespace(data: callable):
490
+ """Check promotion of registered namespaces works as expected"""
491
+
492
+ maxml.Element.register_namespace(
493
+ prefix="my2", uri="http://namespace.example.org/my2", promoted=True
494
+ )
495
+
496
+ element = maxml.Element(name="my2:test")
497
+
498
+ # Ensure that the element object's type is as expected
499
+ assert isinstance(element, maxml.Element)
500
+
501
+ element.set("my2:attribute", "1234")
502
+
503
+ string: str = element.tostring(pretty=True)
504
+
505
+ assert isinstance(string, str)
506
+
507
+ compare: str = data("examples/example04namespace-promoted.xml")
508
+
509
+ assert string == compare
@@ -1 +0,0 @@
1
- 1.0.3
File without changes
File without changes
File without changes
File without changes
File without changes