Permalink
Browse files

- Options now bubble up to the renderers.

- Few more tests.
- General cleanup and fixes.
- Version bump.
  • Loading branch information...
1 parent 6463134 commit 8bbda09ba68d63df5080447d99032b23da49e0ee @bigjason committed May 23, 2011
View
@@ -1,2 +1 @@
include README.md
-
@@ -2,4 +2,4 @@
__all__ = ["Node", "S", "SubNode"]
-VERSION = (0, 1, 1, "alpha")
+VERSION = (0, 1, "beta", 1)
@@ -11,7 +11,7 @@ class NodeBase(object):
def __get_methods__(self):
return set(["render", "register_renderer"])
- def render(self, renderer="xml"):
+ def render(self, renderer="xml", **options):
"""Render the datatree from this node down using the provided renderer.
:keyword renderer: The name of the renderer to use. You may add more
@@ -30,7 +30,7 @@ def render(self, renderer="xml"):
plugin[1] = render_kls
break
# TODO: Should the renderers be instantiated?
- return render_kls().render(self)
+ return render_kls().render(self, **options)
@staticmethod
def register_renderer(klass):
View
@@ -1,6 +1,6 @@
from StringIO import StringIO
-from datatree.nodebase import NodeBase
+from datatree.base import NodeBase
__all__ = ["Node", "SubNode", "S"]
@@ -0,0 +1,2 @@
+
+from datatree.render.base import Renderer
@@ -5,15 +5,18 @@ class Renderer(object):
def friendly_names(self):
raise NotImplementedError()
- def render_node(self, node):
+ def render_node(self, node, options={}):
raise NotImplementedError()
- def render_final(self, rendered):
+ def render_final(self, rendered, options={}):
raise NotImplementedError()
- def render(self, base_node):
+ def render_native(self, options={}):
+ raise NotImplementedError("No render_native() method is provided for this renderer.")
+
+ def render(self, base_node, **options):
"""Renders the entire tree under base_node as a string."""
- return self.render_final(self.render_node(base_node))
+ return self.render_final(self.render_node(base_node, options=options), options=options)
class InternalRenderer(Renderer):
- friendly_names = []
+ friendly_names = []
@@ -1,7 +1,7 @@
from .base import InternalRenderer
class DictTreeRenderer(InternalRenderer):
- def render_node(self, node, parent={}):
+ def render_node(self, node, parent={}, options={}):
# TODO: Figure out how to handle attributes here.
if len(node.__children__) > 0:
children = {}
@@ -13,6 +13,6 @@ def render_node(self, node, parent={}):
parent[node.__node_name__] = children
return parent
- def render_final(self, rendered):
+ def render_final(self, rendered, options={}):
return rendered
@@ -7,7 +7,7 @@
from .base import InternalRenderer
class ETreeRenderer(InternalRenderer):
- def render_node(self, node, parent=None):
+ def render_node(self, node, parent=None, options={}):
attrs = { key: str(value) for key, value in node.__attrs__.iteritems() }
if parent is not None:
root = e.SubElement(parent, node.__node_name__, attrs)
@@ -21,11 +21,11 @@ def render_node(self, node, parent=None):
self.render_node(child, root)
return root
-
+
def to_etree(self):
return e.ElementTree(self.to_xml())
-
- def render_final(self, rendered, pretty=True):
+
+ def render_final(self, rendered, pretty=True, options={}):
return e.tostring(rendered)
@staticmethod
@@ -21,9 +21,9 @@ def test_single_level(self):
root = Node("a", "Here", href="url", title="A Title")
actual = root.render("xml")
result = e.fromstring(actual)
-
+
self.assertEqual(result.tag, "a")
- self.assertEqual(result.attrib["href"], "url")
+ self.assertEqual(result.attrib["href"], "url")
self.assertEqual(result.attrib["title"], "A Title")
self.assertEqual(result.text, "Here")
@@ -32,19 +32,20 @@ def test_multi_level(self):
with root.second(level=2) as two:
two.third("Three", level=3)
root.fourth("four", level=2)
-
+
actual = root.render("xml")
-
+
+ # Parse the xml and break it into pieces to assert.
result = e.fromstring(actual)
second = result.find("second")
third = result.find(".//third")
fourth = result.find("fourth")
- self.assertEqual(result.tag, "root")
+ self.assertEqual(result.tag, "root")
self.assertEqual(result.attrib["level"], "1")
self.assertEqual(second.attrib["level"], "2")
self.assertEqual(third.text, "Three")
self.assertEqual(third.attrib["level"], "3")
self.assertEqual(fourth.text, "four")
self.assertEqual(fourth.attrib["level"], "2")
-
+
@@ -0,0 +1,16 @@
+import unittest
+
+from datatree.render import Renderer
+
+
+class test_Renderer(unittest.TestCase):
+
+ def test_friendly_names_error(self):
+ with self.assertRaises(NotImplementedError):
+ self.assertListEqual(Renderer().friendly_names, [])
+
+ def test_friendly_names_ok(self):
+ class Tester(Renderer):
+ friendly_names = ['A']
+
+ self.assertListEqual(Tester().friendly_names, ['A'])

0 comments on commit 8bbda09

Please sign in to comment.