-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Description
I have a use case for overriding attributes of objects provided through the
template's fed data, i'd like to be considered for inclusion in EZT.
Next to being useful in my case, i read the documented syntax to leave room for
it (the define takes a VARIABLE, and a variable is described to optionally
contain dotted attributes).
Below is a patch that works for me. It still requires every component of a
provided refname to be a valid reference. If it isn't adequate, i'm glad to
rework it upon your review.
thanks,
-seb.
$ svn diff
Index: ezt.py
===================================================================
--- ezt.py (revision 38)
+++ ezt.py (working copy)
@@ -486,11 +486,16 @@
raise UnknownReference(refname, filename, line_number)
# walk the rest of the dotted reference
+ partial_refname = start
for attr in rest:
- try:
- ob = getattr(ob, attr)
- except AttributeError:
- raise UnknownReference(refname, filename, line_number)
+ partial_refname = '%s.%s' % (partial_refname, attr)
+ if ctx.defines.has_key(partial_refname):
+ ob = ctx.defines[partial_refname]
+ else:
+ try:
+ ob = getattr(ob, attr)
+ except AttributeError:
+ raise UnknownReference(refname, filename, line_number)
# make sure we return a string instead of some various Python types
if isinstance(ob, (IntType, FloatType, LongType)):
Index: tests/ezt_test.py
===================================================================
--- tests/ezt_test.py (revision 38)
+++ tests/ezt_test.py (working copy)
@@ -178,6 +178,16 @@
d = self._runTemplate('[define RED]blue[end]RED = [RED]', {})
self.assertEquals('RED = blue', d)
+ def testDefineAttributes(self):
+ class _BlahBlah:
+ def __init__(self, foo, bar):
+ self.foo = foo
+ self.bar = bar
+ o = _BlahBlah('freedom', 'good')
+ d = self._runTemplate('[define X.bar]slavery[end][X.foo] = [X.bar]',
+ {'X': o})
+ self.assertEquals('freedom = slavery', d)
+
def testDefineUnicode(self):
d = self._runTemplate(u'[define HEART]���[end]HEART = [HEART]', {})
self.assertEquals(u'HEART = ���', d)
Original issue reported on code.google.com by s...@google.com on 27 May 2015 at 8:40