summaryrefslogtreecommitdiff
path: root/reconfigure/builders
diff options
context:
space:
mode:
authorAndrew Shadura <andrew@shadura.me>2015-08-20 15:58:26 +0200
committerAndrew Shadura <andrew@shadura.me>2015-08-20 15:58:26 +0200
commitff1408420159488a106492ccd11dd234967029b6 (patch)
tree473420cee1c5229a427ec4cafead1aa6c0a26800 /reconfigure/builders
Imported Upstream version 0.1.29
Diffstat (limited to 'reconfigure/builders')
-rw-r--r--reconfigure/builders/__init__.py16
-rw-r--r--reconfigure/builders/base.py16
-rw-r--r--reconfigure/builders/bound.py18
-rw-r--r--reconfigure/builders/bound_tests.py52
4 files changed, 102 insertions, 0 deletions
diff --git a/reconfigure/builders/__init__.py b/reconfigure/builders/__init__.py
new file mode 100644
index 0000000..d96710d
--- /dev/null
+++ b/reconfigure/builders/__init__.py
@@ -0,0 +1,16 @@
+"""
+Builders are used to convert Node Tree to Data Tree
+"""
+
+from base import BaseBuilder
+from bound import BoundBuilder
+#from nginx import NginxBuilder
+#from crontab import CrontabBuilder
+
+
+__all__ = [
+ 'BaseBuilder',
+ 'BoundBuilder',
+ #'NginxBuilder',
+ #'CrontabBuilder',
+]
diff --git a/reconfigure/builders/base.py b/reconfigure/builders/base.py
new file mode 100644
index 0000000..5708f72
--- /dev/null
+++ b/reconfigure/builders/base.py
@@ -0,0 +1,16 @@
+class BaseBuilder (object):
+ """
+ A base class for builders
+ """
+
+ def build(self, tree):
+ """
+ :param tree: :class:`reconfigure.nodes.Node` tree
+ :returns: Data tree
+ """
+
+ def unbuild(self, tree):
+ """
+ :param tree: Data tree
+ :returns: :class:`reconfigure.nodes.Node` tree
+ """
diff --git a/reconfigure/builders/bound.py b/reconfigure/builders/bound.py
new file mode 100644
index 0000000..a429a08
--- /dev/null
+++ b/reconfigure/builders/bound.py
@@ -0,0 +1,18 @@
+from reconfigure.builders.base import BaseBuilder
+
+
+class BoundBuilder (BaseBuilder):
+ """
+ A builder that uses :class:`reconfigure.items.bound.BoundData` to build stuff
+
+ :param root_class: a ``BoundData`` class that used as processing root
+ """
+
+ def __init__(self, root_class):
+ self.root_class = root_class
+
+ def build(self, nodetree):
+ return self.root_class(nodetree)
+
+ def unbuild(self, tree):
+ pass
diff --git a/reconfigure/builders/bound_tests.py b/reconfigure/builders/bound_tests.py
new file mode 100644
index 0000000..8c1faa6
--- /dev/null
+++ b/reconfigure/builders/bound_tests.py
@@ -0,0 +1,52 @@
+from reconfigure.items.bound import BoundData
+from reconfigure.nodes import Node, PropertyNode
+import unittest
+
+
+class BoundDataTest (unittest.TestCase):
+
+ def test_bind_property(self):
+ class TestBoundData (BoundData):
+ pass
+ TestBoundData.bind_property('prop', 'dataprop', getter=lambda x: 'd' + x, setter=lambda x: x[1:])
+
+ n = Node('name', children=[
+ PropertyNode('prop', 'value')
+ ])
+
+ d = TestBoundData(n)
+
+ self.assertEqual(d.dataprop, 'dvalue')
+ d.dataprop = 'dnew'
+ self.assertEqual(d.dataprop, 'dnew')
+ self.assertEqual(n.get('prop').value, 'new')
+
+ def test_bind_collection(self):
+ class TestBoundData (BoundData):
+ pass
+
+ class TestChildData (BoundData):
+ def template(self):
+ return Node('', children=[PropertyNode('value', None)])
+
+ TestBoundData.bind_collection('items', item_class=TestChildData, selector=lambda x: x.name != 'test')
+ TestChildData.bind_property('value', 'value')
+ n = Node('name', children=[
+ Node('1', children=[PropertyNode('value', 1)]),
+ Node('2', children=[PropertyNode('value', 2)]),
+ Node('test', children=[PropertyNode('value', 3)]),
+ Node('3', children=[PropertyNode('value', 3)]),
+ ])
+
+ d = TestBoundData(n)
+ self.assertEqual(d.items[0].value, 1)
+ self.assertEqual(len(d.items), 3)
+ c = TestChildData()
+ c.value = 4
+ d.items.append(c)
+ self.assertEqual(len(d.items), 4)
+ self.assertEqual(d.items[-1].value, 4)
+
+
+if __name__ == '__main__':
+ unittest.main()