summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>2010-04-13 12:04:53 +0200
committerAdrien Di Mascio <Adrien.DiMascio@logilab.fr>2010-04-13 12:04:53 +0200
commit312c0ecc4930f0b9f0ee5d9f824446e05c2dd35e (patch)
tree54954f32e986db2d4348f16a2d54bcbeed2ed266
parenta957600e6ed21b4d485570780ee50a629ce5100b (diff)
parent0af7f859e9ff3750a57f53858f8f3a56a36bbecc (diff)
downloadlogilab-common-312c0ecc4930f0b9f0ee5d9f824446e05c2dd35e.tar.gz
backport stable branch
-rw-r--r--decorators.py9
-rw-r--r--sphinx_ext.py64
-rw-r--r--test/unittest_testlib.py5
-rw-r--r--testlib.py21
4 files changed, 93 insertions, 6 deletions
diff --git a/decorators.py b/decorators.py
index d5c6b7b..9e5e085 100644
--- a/decorators.py
+++ b/decorators.py
@@ -7,7 +7,7 @@
__docformat__ = "restructuredtext en"
from types import MethodType
-from time import clock
+from time import clock, time
import sys, re
# XXX rewrite so we can use the decorator syntax when keyarg has to be specified
@@ -122,10 +122,11 @@ class iclassmethod(object):
def timed(f):
def wrap(*args, **kwargs):
- t = clock()
- #for i in range(100):
+ t = time()
+ c = clock()
res = f(*args, **kwargs)
- print '%s time: %.9f' % (f.__name__, clock() - t)
+ print '%s clock: %.9f / time: %.9f' % (f.__name__,
+ clock() - c, time() - t)
return res
return wrap
diff --git a/sphinx_ext.py b/sphinx_ext.py
new file mode 100644
index 0000000..9bb82e6
--- /dev/null
+++ b/sphinx_ext.py
@@ -0,0 +1,64 @@
+from logilab.common.decorators import monkeypatch
+
+from sphinx.ext import autodoc
+
+class DocstringOnlyModuleDocumenter(autodoc.ModuleDocumenter):
+ objtype = 'docstring'
+ def format_signature(self):
+ pass
+ def add_directive_header(self, sig):
+ pass
+ def document_members(self, all_members=False):
+ pass
+
+#autodoc.add_documenter(DocstringOnlyModuleDocumenter)
+
+def setup(app):
+ app.add_autodocumenter(DocstringOnlyModuleDocumenter)
+
+
+
+from sphinx.ext.autodoc import (ViewList, Options, AutodocReporter, nodes,
+ assemble_option_dict, nested_parse_with_titles)
+
+@monkeypatch(autodoc.AutoDirective)
+def run(self):
+ self.filename_set = set() # a set of dependent filenames
+ self.reporter = self.state.document.reporter
+ self.env = self.state.document.settings.env
+ self.warnings = []
+ self.result = ViewList()
+
+ # find out what documenter to call
+ objtype = self.name[4:]
+ doc_class = self._registry[objtype]
+ # process the options with the selected documenter's option_spec
+ self.genopt = Options(assemble_option_dict(
+ self.options.items(), doc_class.option_spec))
+ # generate the output
+ documenter = doc_class(self, self.arguments[0])
+ documenter.generate(more_content=self.content)
+ if not self.result:
+ return self.warnings
+
+ # record all filenames as dependencies -- this will at least
+ # partially make automatic invalidation possible
+ for fn in self.filename_set:
+ self.env.note_dependency(fn)
+
+ # use a custom reporter that correctly assigns lines to source
+ # filename/description and lineno
+ old_reporter = self.state.memo.reporter
+ self.state.memo.reporter = AutodocReporter(self.result,
+ self.state.memo.reporter)
+ if self.name in ('automodule', 'autodocstring'):
+ node = nodes.section()
+ # necessary so that the child nodes get the right source/line set
+ node.document = self.state.document
+ nested_parse_with_titles(self.state, self.result, node)
+ else:
+ node = nodes.paragraph()
+ node.document = self.state.document
+ self.state.nested_parse(self.result, 0, node)
+ self.state.memo.reporter = old_reporter
+ return self.warnings + node.children
diff --git a/test/unittest_testlib.py b/test/unittest_testlib.py
index 152ccc7..19aa281 100644
--- a/test/unittest_testlib.py
+++ b/test/unittest_testlib.py
@@ -221,6 +221,11 @@ class TestlibTC(TestCase):
self.assertEquals(self.datadir, expected_datadir)
self.assertEquals(self.datapath('foo'), join(expected_datadir, 'foo'))
+ def test_multiple_args_datadir(self):
+ expected_datadir = join(dirname(abspath(__file__)), 'data')
+ self.assertEquals(self.datadir, expected_datadir)
+ self.assertEquals(self.datapath('foo', 'bar'), join(expected_datadir, 'foo', 'bar'))
+
def test_custom_datadir(self):
class MyTC(TestCase):
datadir = 'foo'
diff --git a/testlib.py b/testlib.py
index 1ff282a..6532814 100644
--- a/testlib.py
+++ b/testlib.py
@@ -95,6 +95,23 @@ def with_tempdir(callable):
tempfile.tempdir = old_tmpdir
return proxy
+def in_tempdir(callable):
+ """A decorator moving the enclosed function inside the tempfile.tempfdir
+ """
+ def proxy(*args, **kargs):
+
+ old_cwd = os.getcwd()
+ os.chdir(tempfile.tempdir)
+ try:
+ return callable(*args, **kargs)
+ finally:
+ os.chdir(old_cwd)
+ return proxy
+
+def within_tempdir(callable):
+ """A decorator run the enclosed function inside a tmpdir removed after execution
+ """
+ return with_tempdir(in_tempdir(callable))
def run_tests(tests, quiet, verbose, runner=None, capture=0):
"""Execute a list of tests.
@@ -993,9 +1010,9 @@ class TestCase(unittest.TestCase):
# instantiated for each test run)
datadir = classproperty(cached(datadir))
- def datapath(cls, fname):
+ def datapath(cls, *fname):
"""joins the object's datadir and `fname`"""
- return osp.join(cls.datadir, fname)
+ return osp.join(cls.datadir, *fname)
datapath = classmethod(datapath)
def set_description(self, descr):