[py-svn] r32920 - in py/branch/apigen/py/rst: . testing
fijal at codespeak.net
fijal at codespeak.net
Thu Oct 5 22:16:07 CEST 2006
Author: fijal
Date: Thu Oct 5 22:16:05 2006
New Revision: 32920
Added:
py/branch/apigen/py/rst/ (props changed)
py/branch/apigen/py/rst/__init__.py (contents, props changed)
py/branch/apigen/py/rst/rst.py (contents, props changed)
py/branch/apigen/py/rst/testing/ (props changed)
py/branch/apigen/py/rst/testing/__init__.py (contents, props changed)
py/branch/apigen/py/rst/testing/test_rst.py (contents, props changed)
Log:
Added first version of simple ReST producer. I think is way easier to use than py.rest (altough who knows?)
Added: py/branch/apigen/py/rst/__init__.py
==============================================================================
Added: py/branch/apigen/py/rst/rst.py
==============================================================================
--- (empty file)
+++ py/branch/apigen/py/rst/rst.py Thu Oct 5 22:16:05 2006
@@ -0,0 +1,113 @@
+
+""" reStructuredText manipulation tools
+"""
+
+from __future__ import generators
+
+import py
+
+class RestError(Exception):
+ pass
+
+class AbstractMetaclass(type):
+ def __new__(cls, *args):
+ obj = super(AbstractMetaclass, cls).__new__(cls, *args)
+ parent_cls = obj.parentclass
+ if parent_cls is not None:
+ if parent_cls.allowed_child:
+ parent_cls.allowed_child = {obj:True}
+ else:
+ parent_cls.allowed_child[obj] = True
+ return obj
+
+class AbstractNode(object):
+ """ Basic class implementing writing rest code
+ """
+ sep = ''
+ __metaclass__ = AbstractMetaclass
+ parentclass = None # this exists to allow parent to know what
+ # children can exist
+ allowed_child = {}
+ defaults = {}
+
+ def __init__(self, *args, **kwargs):
+ self.parent = None
+ self.childs = args
+ for arg in kwargs:
+ setattr(self, arg, kwargs[arg])
+
+ def join(self, child):
+ self._add(child)
+ return self
+
+ def add(self, child):
+ self._add(child)
+ return child
+
+ def _add(self, child):
+ if child.__class__ not in self.allowed_child:
+ raise RestError("%r cannot be child of %r" % \
+ (child.__class__, self.__class__))
+ self.childs.append(child)
+ child.parent = self
+
+ def __getitem__(self, item):
+ return self.childs[item]
+
+ def __setitem__(self, item, value):
+ self.childs[item] = value
+
+ def text(self):
+ return self.sep.join([child.text() for child in self.childs])
+
+ def wordlist(self):
+ return [self.text()]
+
+class Rest(AbstractNode):
+ sep = "\n\n"
+
+class Paragraph(AbstractNode):
+ parentclass = Rest
+ sep = " "
+ indent = ""
+ width = 80
+
+ def text(self):
+ texts = []
+ for child in self.childs:
+ texts += child.wordlist()
+
+ buf = []
+ outcome = []
+ lgt = len(self.indent)
+
+ def grab(buf):
+ outcome.append(self.indent + self.sep.join(buf))
+
+ while texts:
+ next = texts[-1]
+ if lgt + len(self.sep) + len(next) <= self.width or not buf:
+ buf.append(next)
+ lgt += len(next) + len(self.sep)
+ texts.pop()
+ else:
+ grab(buf)
+ lgt = len(self.indent)
+ buf = []
+ grab(buf)
+ outcome.reverse()
+ return "\n".join(outcome)
+##
+class SubParagraph(Paragraph):
+ indent = " "
+
+class Text(AbstractNode):
+ parentclass = Paragraph
+ def __init__(self, _text):
+ self._text = _text
+
+ def text(self):
+ return self._text
+
+ def wordlist(self):
+ return self._text.split(" ")
Added: py/branch/apigen/py/rst/testing/__init__.py
==============================================================================
Added: py/branch/apigen/py/rst/testing/test_rst.py
==============================================================================
--- (empty file)
+++ py/branch/apigen/py/rst/testing/test_rst.py Thu Oct 5 22:16:05 2006
@@ -0,0 +1,15 @@
+
+""" rst generation tests
+"""
+
+from py.__.rst.rst import *
+
+def test_textgen():
+ assert Rest(Paragraph(Text("dupa"))).text() == "dupa"
+ assert Rest(Paragraph(Text("dupa"), Text("dupa"))).text() == "dupa dupa"
+ assert Rest(Paragraph(Text("a")), Paragraph(Text("b"))).text() == "a\n\nb"
+ assert Rest(Paragraph(Text("a"), indent=" ")).text() == " a"
+
+def test_join():
+ txt = Rest(Paragraph(Text("a b c d e f"), width=3, indent=" ")).text()
+ assert txt == ' a\n b\n c\n d\n e\n f'
More information about the pytest-commit
mailing list