Issue 10 in ezt: support for [define VARIABLE] where VARIABLE contains dotted attributes

4 views
Skip to first unread message

e...@googlecode.com

unread,
May 27, 2015, 4:40:53 PM5/27/15
to ezt-d...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 10 by s...@google.com: support for [define VARIABLE] where
VARIABLE contains dotted attributes
https://code.google.com/p/ezt/issues/detail?id=10

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)

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

e...@googlecode.com

unread,
Jun 30, 2015, 11:41:56 PM6/30/15
to ezt-d...@googlegroups.com

Comment #1 on issue 10 by cmpilato: support for [define VARIABLE] where
VARIABLE contains dotted attributes
https://code.google.com/p/ezt/issues/detail?id=10

I like the proposal. Cannot at this time comment on the patch readiness.
Reply all
Reply to author
Forward
0 new messages