From f729740011d5083a13ee81d8f6052c3ce776a8e7 Mon Sep 17 00:00:00 2001 From: Michael Graf Date: Wed, 12 Jun 2019 09:33:43 +0200 Subject: [PATCH 1/6] prepare for callable data-attributes, for bluedynamics/yafowil.plone#26 --- HISTORY.rst | 4 +++- src/yafowil/yaml/parser.py | 10 +++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index ff3c535..2d00eb1 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -5,7 +5,9 @@ History 1.4 (unreleased) ---------------- -- No changes yet. +- Parse values of attributes (data-Attributes) if type() is dict. + HTML5 Data-Attributes with i18n or callables are possible now + [2silver, 2019-06-12] 1.3 (2018-07-16) diff --git a/src/yafowil/yaml/parser.py b/src/yafowil/yaml/parser.py index 73dc8cc..8e8e2c1 100644 --- a/src/yafowil/yaml/parser.py +++ b/src/yafowil/yaml/parser.py @@ -98,7 +98,15 @@ def create_tree(self, data): def call_factory(defs): props = dict() for k, v in defs.get('props', dict()).items(): - props[k] = self.parse_definition_value(v) + if isinstance(v, dict): + dict_attrs = list() + for vk, vv in v.items(): + val = self.parse_definition_value(vv) + part = [vk, val] + dict_attrs.append(part) + props[k] = dict_attrs + else: + props[k] = self.parse_definition_value(v) custom = dict() for custom_key, custom_value in defs.get('custom', dict()).items(): custom_props = list() From 7b9eeb25fd1b7ae501f254d1ea6d4c242a0fbeb0 Mon Sep 17 00:00:00 2001 From: Robert Niederreiter Date: Mon, 16 Jul 2018 15:38:36 +0200 Subject: [PATCH 2/6] prepare 1.3 --- HISTORY.rst | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 67e384c..1443d43 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,7 +2,7 @@ History ======= -1.3 (unreleased) +1.3 (2018-07-16) ---------------- - Python 3 compatibility. diff --git a/setup.py b/setup.py index ab247fc..1e4b285 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ import os -version = '1.3.dev0' +version = '1.3' shortdesc = 'YAFOWIL - YAML-/ JSON-parser for widget trees.' longdesc = open(os.path.join(os.path.dirname(__file__), 'README.rst')).read() longdesc += open(os.path.join(os.path.dirname(__file__), 'HISTORY.rst')).read() From be053aaccb5ef06ecd8c0c6df3c92c9be3f1b211 Mon Sep 17 00:00:00 2001 From: Robert Niederreiter Date: Mon, 16 Jul 2018 15:39:50 +0200 Subject: [PATCH 3/6] Bump version --- HISTORY.rst | 6 ++++++ setup.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/HISTORY.rst b/HISTORY.rst index 1443d43..ff3c535 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,12 @@ History ======= +1.4 (unreleased) +---------------- + +- No changes yet. + + 1.3 (2018-07-16) ---------------- diff --git a/setup.py b/setup.py index 1e4b285..8c0770b 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ import os -version = '1.3' +version = '1.4.dev0' shortdesc = 'YAFOWIL - YAML-/ JSON-parser for widget trees.' longdesc = open(os.path.join(os.path.dirname(__file__), 'README.rst')).read() longdesc += open(os.path.join(os.path.dirname(__file__), 'HISTORY.rst')).read() From 338a4da821b43de7e89d57681b66baa76749ab6d Mon Sep 17 00:00:00 2001 From: Michael Graf Date: Wed, 12 Jun 2019 09:33:43 +0200 Subject: [PATCH 4/6] prepare for callable data-attributes, for bluedynamics/yafowil.plone#26 --- HISTORY.rst | 4 +++- src/yafowil/yaml/parser.py | 10 +++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index ff3c535..2d00eb1 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -5,7 +5,9 @@ History 1.4 (unreleased) ---------------- -- No changes yet. +- Parse values of attributes (data-Attributes) if type() is dict. + HTML5 Data-Attributes with i18n or callables are possible now + [2silver, 2019-06-12] 1.3 (2018-07-16) diff --git a/src/yafowil/yaml/parser.py b/src/yafowil/yaml/parser.py index 73dc8cc..8e8e2c1 100644 --- a/src/yafowil/yaml/parser.py +++ b/src/yafowil/yaml/parser.py @@ -98,7 +98,15 @@ def create_tree(self, data): def call_factory(defs): props = dict() for k, v in defs.get('props', dict()).items(): - props[k] = self.parse_definition_value(v) + if isinstance(v, dict): + dict_attrs = list() + for vk, vv in v.items(): + val = self.parse_definition_value(vv) + part = [vk, val] + dict_attrs.append(part) + props[k] = dict_attrs + else: + props[k] = self.parse_definition_value(v) custom = dict() for custom_key, custom_value in defs.get('custom', dict()).items(): custom_props = list() From a5679f6b1be0900193d67380bb3620d4cd32c9b3 Mon Sep 17 00:00:00 2001 From: Michael Graf Date: Sat, 22 Jun 2019 14:08:25 +0200 Subject: [PATCH 5/6] add recursive parse for attributes --- src/yafowil/yaml/parser.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/yafowil/yaml/parser.py b/src/yafowil/yaml/parser.py index 8e8e2c1..dbbdacb 100644 --- a/src/yafowil/yaml/parser.py +++ b/src/yafowil/yaml/parser.py @@ -97,16 +97,7 @@ def load_yaml(self, path): def create_tree(self, data): def call_factory(defs): props = dict() - for k, v in defs.get('props', dict()).items(): - if isinstance(v, dict): - dict_attrs = list() - for vk, vv in v.items(): - val = self.parse_definition_value(vv) - part = [vk, val] - dict_attrs.append(part) - props[k] = dict_attrs - else: - props[k] = self.parse_definition_value(v) + props = self.parse_attribute(defs.get('props', dict())) custom = dict() for custom_key, custom_value in defs.get('custom', dict()).items(): custom_props = list() @@ -155,6 +146,16 @@ def create_children(node, children_defs): create_children(root, data.get('widgets', [])) return root + def parse_attribute(self, value): + if not isinstance(value, dict): + return self.parse_definition_value(value) + for k,v in value.items(): + if isinstance(v, dict): + self.parse_attribute(v) + else: + value[k] = self.parse_definition_value(v) + return value + def parse_definition_value(self, value): if not isinstance(value, STR_TYPE): return value @@ -164,7 +165,7 @@ def fetch_value(widget=None, data=None): return eval(value[5:], {'context': self.context, 'widget': widget, 'data': data}, {}) - return fetch_value + return fetch_value() if value.startswith('i18n:'): parts = value.split(":") if len(parts) > 3: From b7bddb21b3f7723cbb9bd9b038457ffaffcf8875 Mon Sep 17 00:00:00 2001 From: Michael Graf Date: Mon, 24 Jun 2019 16:24:10 +0200 Subject: [PATCH 6/6] add test data --- src/yafowil/yaml/parser.py | 2 +- src/yafowil/yaml/tests.py | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/yafowil/yaml/parser.py b/src/yafowil/yaml/parser.py index dbbdacb..7bfed26 100644 --- a/src/yafowil/yaml/parser.py +++ b/src/yafowil/yaml/parser.py @@ -165,7 +165,7 @@ def fetch_value(widget=None, data=None): return eval(value[5:], {'context': self.context, 'widget': widget, 'data': data}, {}) - return fetch_value() + return fetch_value if value.startswith('i18n:'): parts = value.split(":") if len(parts) > 3: diff --git a/src/yafowil/yaml/tests.py b/src/yafowil/yaml/tests.py index 29a384a..b424331 100644 --- a/src/yafowil/yaml/tests.py +++ b/src/yafowil/yaml/tests.py @@ -79,6 +79,11 @@ def tearDown(self): label: i18n:First Field description: I am the description required: I am required + data: + flat: I am a flat data-attribute + i18n: i18n:nested_firstfield:i18n Nested First Field + nested: + nested-i18n: i18n:nested_firstfield:i18n Nested First Field - secondfield: factory: field:label:*custom_stuff:error:select value: ['a', 'b'] @@ -108,7 +113,14 @@ def test_load_yaml(self): 'props': { 'description': 'I am the description', 'label': 'i18n:First Field', - 'required': 'I am required' + 'required': 'I am required', + "data": { + "flat": "I am a flat data-attribute", + "i18n": "i18n:nested_firstfield:i18n Nested First Field", + "nested": { + "nested-i18n": "i18n:nested_firstfield:i18n Nested First Field" + } + } } } }, { @@ -234,9 +246,15 @@ def test_parse_from_yaml(self): id="form-demoform" method="post" novalidate="novalidate">
- +