Projet

Général

Profil

0001-ezt-allow-easy-access-to-array-dict-elements-10074.patch

Frédéric Péters, 24 février 2016 16:50

Télécharger (2,09 ko)

Voir les différences:

Subject: [PATCH] ezt: allow easy access to array/dict elements (#10074)

 tests/test_ezt.py | 26 ++++++++++++++++++++++++++
 wcs/qommon/ezt.py |  8 +++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)
tests/test_ezt.py
90 90
        template.parse('<p>[if foo]Test[end]</p>')
91 91
    except UnmatchedEndError as e:
92 92
        assert e.column == 15 and e.line == 0
93

  
94
def test_array_index():
95
    template = Template()
96
    template.parse('<p>[foo.0]</p>')
97
    output = StringIO()
98
    template.generate(output, {'foo': ['bar']})
99
    assert output.getvalue() == '<p>bar</p>'
100

  
101
    template = Template()
102
    template.parse('<p>[foo.bar]</p>')
103
    output = StringIO()
104
    template.generate(output, {'foo': ['bar']})
105
    assert output.getvalue() == '<p>[foo.bar]</p>'
106

  
107
def test_dict_index():
108
    template = Template()
109
    template.parse('<p>[foo.a]</p>')
110
    output = StringIO()
111
    template.generate(output, {'foo': {'a': 'bar'}})
112
    assert output.getvalue() == '<p>bar</p>'
113

  
114
    template = Template()
115
    template.parse('<p>[foo.b]</p>')
116
    output = StringIO()
117
    template.generate(output, {'foo': {'a': 'bar'}})
118
    assert output.getvalue() == '<p>[foo.b]</p>'
wcs/qommon/ezt.py
638 638
    try:
639 639
      ob = getattr(ob, attr)
640 640
    except AttributeError:
641
      raise UnknownReference(refname)
641
      try:
642
        ob = ob[attr]
643
      except (TypeError, KeyError):
644
        try:
645
          ob = ob[int(attr)]
646
        except (ValueError, TypeError):
647
          raise UnknownReference(refname)
642 648

  
643 649
  # make sure we return a string instead of some various Python types
644 650
  if isinstance(ob, IntType) \
645
-