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

support for [define VARIABLE] where VARIABLE contains dotted attributes #10

Open
GoogleCodeExporter opened this issue Aug 22, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 27 May 2015 at 8:40

@gstein
Copy link
Owner

gstein commented Aug 22, 2015

CC: @cmpilato

@gstein gstein self-assigned this Aug 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants