Browse Source

order: do not dump empty-list and null attributes; article: do not set delivery date if null

Fabian Peter Hammerle 7 years ago
parent
commit
7aed9bb149
3 changed files with 31 additions and 7 deletions
  1. 15 3
      finoex/__init__.py
  2. 1 1
      setup.py
  3. 15 3
      tests/test_yaml.py

+ 15 - 3
finoex/__init__.py

@@ -38,6 +38,17 @@ class _YamlInitConstructor(yaml.YAMLObject):
         )
 
 
+class _YamlVarsRepresenter(yaml.YAMLObject):
+
+    @classmethod
+    def to_yaml(cls, dumper, obj):
+        return dumper.represent_mapping(
+            cls.yaml_tag,
+            {k: v for k, v in vars(obj).items()
+                if v and (not isinstance(v, list) or len(v) > 0)},
+        )
+
+
 class Sum(ioex.calcex.Figure):
 
     currency_symbol_map = {
@@ -106,7 +117,7 @@ class Sum(ioex.calcex.Figure):
         )
 
 
-class Order(_Object, _YamlInitConstructor):
+class Order(_Object, _YamlInitConstructor, _YamlVarsRepresenter):
 
     yaml_tag = u'!order'
 
@@ -317,8 +328,9 @@ class Article(Item):
         if features is not None:
             assert type(features) is str
             self.features = features
-        assert delivery_date is None or type(delivery_date) is datetime.date
-        self.delivery_date = delivery_date
+        if delivery_date is not None:
+            assert type(delivery_date) is datetime.date
+            self.delivery_date = delivery_date
 
 
 class Service(Item):

+ 1 - 1
setup.py

@@ -4,7 +4,7 @@ import glob
 
 setup(
     name = 'finoex',
-    version = '0.1.3',
+    version = '0.1.4',
     # description = '',
     author = 'Fabian Peter Hammerle',
     author_email = 'fabian.hammerle@gmail.com',

+ 15 - 3
tests/test_yaml.py

@@ -190,8 +190,8 @@ def to_yaml(data):
     finoex.Sum.register_yaml_representer(yaml.Dumper)
     finoex.Distance.register_yaml_representer(yaml.Dumper)
     return yaml.dump(
-        data, 
-        default_flow_style = False, 
+        data,
+        default_flow_style = False,
         allow_unicode = True,
         Dumper = Dumper,
     ) #.decode('utf-8')
@@ -267,12 +267,24 @@ items:
 order_date: 2016-05-08 00:18:17
 order_id: id
 platform: platformπ
+"""],
+    [get_order_a(discounts = False), u"""!order
+customer_id: customer
+items:
+- !item
+  name: item a
+  price_brutto: !sum '1.23 EUR'
+- !item
+  name: item β
+  price_brutto: !sum '20.45 EUR'
+order_date: 2016-05-08 00:18:17
+order_id: id
+platform: platformπ
 """],
     [get_article(), u"""!article
 authors:
 - a
 - b
-delivery_date: null
 depth: !distance '12.3 dm'
 features: supergeil
 height: !distance '123.0 cm'