Browse Source

Adiciona leitura de layout a partir de yaml

pull/237/head
Marcio Mazza 9 years ago
parent
commit
bc8b8647aa
  1. 30
      crispy_layout_mixin.py
  2. 1
      requirements/requirements.txt
  3. 29
      test_crispy_layout_mixin.py

30
crispy_layout_mixin.py

@ -1,3 +1,6 @@
from math import ceil
import rtyaml
from crispy_forms.bootstrap import FormActions from crispy_forms.bootstrap import FormActions
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML, Div, Fieldset, Layout, Submit from crispy_forms.layout import HTML, Div, Fieldset, Layout, Submit
@ -99,3 +102,30 @@ class CrispyLayoutFormMixin(object):
for fieldname, span in row] for fieldname, span in row]
for row in rows] for row in rows]
} for legend, *rows in self.layout] } for legend, *rows in self.layout]
# TODO cache this
def read_yaml_from_file(filename):
with open(filename, 'r') as yamlfile:
return rtyaml.load(yamlfile)
def read_layout_from_yaml(filename, key):
# TODO cache this
yaml = read_yaml_from_file(filename)
base = yaml[key]
def line_to_namespans(line):
split = [cell.split(':') for cell in line.split()]
namespans = [[s[0], int(s[1]) if len(s) > 1 else 0] for s in split]
remaining = 12 - sum(s for n, s in namespans)
nondefined = [ns for ns in namespans if not ns[1]]
while nondefined:
span = ceil(remaining / len(nondefined))
namespan = nondefined.pop(0)
namespan[1] = span
remaining = remaining - span
return list(map(tuple, namespans))
return [(legend, [line_to_namespans(l) for l in lines])
for legend, lines in base.items()]

1
requirements/requirements.txt

@ -15,4 +15,5 @@ libsass
psycopg2 psycopg2
pytz pytz
pyyaml pyyaml
rtyaml
unipath unipath

29
test_crispy_layout_mixin.py

@ -0,0 +1,29 @@
from crispy_layout_mixin import read_layout_from_yaml
def test_read_layout_from_yaml(tmpdir):
contents = '''
ModelName:
Cool Legend:
- name:9 place tiny
- field nature:2
- kind:1 date unit:5 status
More data:
- equalA equalB equalC
- highlander '''
file = tmpdir.join('zzz.yaml')
file.write(contents)
expected = [
('Cool Legend', [
[('name', 9), ('place', 2), ('tiny', 1)],
[('field', 10), ('nature', 2)],
[('kind', 1), ('date', 3), ('unit', 5), ('status', 3)],
]),
('More data', [
[('equalA', 4), ('equalB', 4), ('equalC', 4)],
[('highlander', 12)],
]),
]
assert read_layout_from_yaml(file.strpath, 'ModelName') == expected
Loading…
Cancel
Save