Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive parse Attributes (ex: Data-Attributes) #6

Merged
merged 7 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions src/yafowil/yaml/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +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():
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()
Expand Down Expand Up @@ -147,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
Expand Down
26 changes: 22 additions & 4 deletions src/yafowil/yaml/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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"
}
}
}
}
}, {
Expand Down Expand Up @@ -234,9 +246,15 @@ def test_parse_from_yaml(self):
id="form-demoform" method="post" novalidate="novalidate">
<div class="field" id="field-demoform-firstfield">
<label for="input-demoform-firstfield">First Field</label>
<input class="required text" id="input-demoform-firstfield"
name="demoform.firstfield" required="required"
type="text" value="First value"/>
<input class="required text"
data-flat="I am a flat data-attribute"
data-i18n="i18n Nested First Field"
data-nested="{&quot;nested-i18n&quot;: &quot;i18n Nested First Field&quot;}"
id="input-demoform-firstfield"
name="demoform.firstfield"
required="required"
type="text"
value="First value"/>
</div>
<div class="field" id="field-demoform-secondfield">
<label for="input-demoform-secondfield"
Expand Down