[py-svn] r33496 - in py/dist/py/apigen: . rest rest/testing tracer tracer/testing

guido at codespeak.net guido at codespeak.net
Fri Oct 20 13:40:25 CEST 2006


Author: guido
Date: Fri Oct 20 13:40:23 2006
New Revision: 33496

Added:
   py/dist/py/apigen/rest/__init__.py   (contents, props changed)
   py/dist/py/apigen/rest/genrest.py
      - copied unchanged from r33495, py/dist/py/apigen/tracer/genrest.py
   py/dist/py/apigen/rest/testing/   (props changed)
   py/dist/py/apigen/rest/testing/__init__.py   (contents, props changed)
   py/dist/py/apigen/rest/testing/test_rest.py
      - copied, changed from r33495, py/dist/py/apigen/tracer/testing/test_rest.py
Removed:
   py/dist/py/apigen/tracer/genrest.py
   py/dist/py/apigen/tracer/testing/test_rest.py
Modified:
   py/dist/py/apigen/   (props changed)
   py/dist/py/apigen/__init__.py   (props changed)
   py/dist/py/apigen/rest/   (props changed)
Log:
Moved rest stuff to seperate dir.


Added: py/dist/py/apigen/rest/__init__.py
==============================================================================

Added: py/dist/py/apigen/rest/testing/__init__.py
==============================================================================

Copied: py/dist/py/apigen/rest/testing/test_rest.py (from r33495, py/dist/py/apigen/tracer/testing/test_rest.py)
==============================================================================
--- py/dist/py/apigen/tracer/testing/test_rest.py	(original)
+++ py/dist/py/apigen/rest/testing/test_rest.py	Fri Oct 20 13:40:23 2006
@@ -6,7 +6,7 @@
 from StringIO import StringIO
 
 try:
-    from py.__.apigen.tracer.genrest import ViewVC, RestGen, PipeWriter, \
+    from py.__.apigen.rest.genrest import ViewVC, RestGen, PipeWriter, \
                                             DirWriter, FileWriter, \
                                             DirectPaste, DirectFS
     from py.__.apigen.tracer.tracer import DocStorage, Tracer
@@ -58,7 +58,7 @@
     fname = cut_pyc(__file__)
     title, link = vcview.getlink(fname, 0)
     assert title == '%s:%s' % (fname, 0)
-    assert link == ('http://codespeak.net/viewvc/py/apigen/tracer/'
+    assert link == ('http://codespeak.net/viewvc/py/apigen/rest/'
                         'testing/test_rest.py?view=markup')
 
 def test_fs_link():

Deleted: /py/dist/py/apigen/tracer/genrest.py
==============================================================================
--- /py/dist/py/apigen/tracer/genrest.py	Fri Oct 20 13:40:23 2006
+++ (empty file)
@@ -1,234 +0,0 @@
-
-""" Generating ReST output (raw, not python)
-out of data that we know about function calls
-"""
-
-import py
-import sys
-import re
-
-from py.__.apigen.tracer.docstorage import DocStorageAccessor
-from py.__.rst.rst import * # XXX Maybe we should list it here
-
-class AbstractLinkWriter(object):
-    """ Class implementing writing links to source code.
-    There should exist various classes for that, different for Trac,
-    different for CVSView, etc.
-    """
-    def getlink(self, filename, lineno):
-        raise NotImplementedError("Abstract link writer")
-    
-class ViewVC(AbstractLinkWriter):
-    """ Link writer for ViewVC version control viewer
-    """
-    def __init__(self, basepath):
-        # XXX: should try to guess from a working copy of svn
-        self.basepath = basepath
-    
-    def getpkgpath(self, filename):
-        # XXX: very simple thing
-        path = py.path.local(filename).dirpath()
-        while 1:
-            try:
-                path.join('__init__.py').stat()
-                path = path.dirpath()
-            except py.error.ENOENT:
-                return path
-    
-    def getlink(self, filename, lineno):
-        path = str(self.getpkgpath(filename))
-        assert filename.startswith(path), (
-            "%s does not belong to package %s" % (filename, path))
-        relname = filename[len(path):]
-        if relname.endswith('.pyc'):
-            relname = relname[:-1]
-        return ('%s:%s' % (filename, lineno),
-                self.basepath + relname[1:] + '?view=markup')
-
-class DirectPaste(AbstractLinkWriter):
-    """ No-link writer (inliner)
-    """
-    def getlink(self, filename, lineno):
-        return ('%s:%s' % (filename, lineno), "")
-
-class DirectFS(AbstractLinkWriter):
-    """ Creates links to the files on the file system (for local docs)
-    """
-    def getlink(self, filename, lineno):
-        return ('%s:%s' % (filename, lineno), 'file://%s' % (filename,))
-
-class PipeWriter(object):
-    def __init__(self, output=sys.stdout):
-        self.output = output
-    
-    def write_section(self, name, data):
-        text = "Contents of file %s.txt:" % (name,)
-        self.output.write(text + "\n")
-        self.output.write("=" * len(text) + "\n")
-        self.output.write("\n")
-        self.output.write(data + "\n")
-
-    def getlink(self, type, targetname, targetfilename):
-        return '%s.txt' % (targetfilename,)
-
-class DirWriter(object):
-    def __init__(self, directory=None):
-        if directory is None:
-            self.directory = py.test.ensuretemp("rstoutput")
-        else:
-            self.directory = py.path.local(directory)
-    
-    def write_section(self, name, data):
-        filename = '%s.txt' % (name,)
-        self.directory.ensure(filename).write(data)
-
-    def getlink(self, type, targetname, targetfilename):
-        # we assume the result will get converted to HTML...
-        return '%s.html' % (targetfilename,)
-
-class FileWriter(object):
-    def __init__(self, fpath):
-        self.fpath = fpath
-        self.fp = fpath.open('w+')
-        self._defined_targets = []
-
-    def write_section(self, name, data):
-        self.fp.write(data)
-        self.fp.flush()
-
-    def getlink(self, type, targetname, targetbasename):
-        # XXX problem: because of docutils' named anchor generation scheme,
-        # a method Foo.__init__ would clash with Foo.init (underscores are 
-        # removed)
-        if targetname in self._defined_targets:
-            return None
-        self._defined_targets.append(targetname)
-        targetname = targetname.lower().replace('.', '-').replace('_', '')
-        return '#%s-%s' % (type, targetname)
-
-class RestGen(object):
-    def __init__(self, ds, linkgen, writer=PipeWriter()):
-        self.dsa = DocStorageAccessor(ds)
-        self.linkgen = linkgen
-        self.writer = writer
-    
-    def write(self):
-        """write the data to the writer"""
-        # note that this builds up a list of Rest elements for the index as
-        # 'side effect', the list is passed along and filled, while the actual
-        # sections (also ReST elements) are returned by the write_* methods
-        # XXX this is quite icky! would be nice to have refactored
-        indexlst = [Title("Module: %s" % self.dsa.get_module_name(),
-                          belowchar="="),
-                    Paragraph(self.dsa.get_module_info()),
-                    Title("Exported functions:", belowchar="-")]
-        funclst = self.write_function_list(indexlst)
-        indexlst.append(Title("Exported classes:", belowchar="-"))
-        classlst = self.write_class_list(indexlst)
-        self.writer.write_section('index', Rest(*indexlst).text())
-        for sectionname, restitems in funclst:
-            self.writer.write_section(sectionname, Rest(*restitems).text())
-        for sectionname, classdata in classlst:
-            restitems, classfunclst = classdata
-            self.writer.write_section(sectionname, Rest(*restitems).text())
-            for fsectionname, frestitems in classfunclst:
-                self.writer.write_section(fsectionname,
-                                          Rest(*frestitems).text())
-    
-    def write_function_list(self, indexlst):
-        retlst = []
-        for name in self.dsa.get_function_names():
-            sectionname = 'function_%s' % (name,)
-            linktarget = self.writer.getlink('function', name,
-                                                  sectionname)
-            indexlst.append(ListItem(Text("Function: "),
-                                Link(name, linktarget)))
-            retlst.append((sectionname,
-                           self.write_function(sectionname, name)))
-        return retlst
-    
-    def write_class_list(self, indexlst):
-        retlst = []
-        for name in self.dsa.get_class_names():
-            sectionname = 'class_%s' % (name,)
-            linktarget = self.writer.getlink('class', name, sectionname)
-            indexlst.append(ListItem(Text("Class: "), Link(name, linktarget)))
-            retlst.append((sectionname, self.write_class(sectionname, name)))
-        return retlst
-    
-    def write_class(self, section_name, class_name):
-        classlst = [Title("Class: %s" % class_name, belowchar='-'),
-                    LiteralBlock(self.dsa.get_function_doc(class_name))]
-        
-        # write down exported methods
-        classlst.append(Title("Exported methods:", belowchar="^"))
-        funclist = []
-        for method in self.dsa.get_class_methods(class_name):
-            sectionname = 'method_%s_%s' % (class_name, method)
-            linktext = '%s.%s' % (class_name, method)
-            linktarget = self.writer.getlink('function', linktext,
-                                                  sectionname)
-            classlst.append(ListItem(Link(linktext, linktarget)))
-            # XXX assuming a function is always part of a class section
-            funclist.append((sectionname,
-                             self.write_function(sectionname,
-                                                 class_name + "." + method,
-                                                 '^')))
-        return classlst, funclist
-    
-    def write_function(self, section_name, fun_name, belowchar='-'):
-        # XXX I think the docstring should either be split on \n\n and cleaned
-        # from indentation, or treated as ReST too (although this is obviously
-        # dangerous for non-ReST docstrings)...
-        lst = [Title("Function: %s" % fun_name, belowchar=belowchar),
-               LiteralBlock(self.dsa.get_function_doc(fun_name)),
-               LiteralBlock(self.dsa.get_function_definition(fun_name))]
-        
-        # XXX missing implementation of dsa.get_function_location()
-        #filename, lineno = self.dsa.get_function_location(fun_name)
-        #linkname, linktarget = self.linkgen.getlink(filename, lineno)
-        #if linktarget:
-        #    lst.append(Paragraph("Function source: ",
-        #               Link(linkname, linktarget)))
-        #else:
-        lst.append(Paragraph('Function source: <unknown>'))
-        
-        args, retval = self.dsa.get_function_signature(fun_name)
-        # XXX: we just do "knowntype" here, but we should
-        # present some more informative way, maybe even provide a link
-        # for the type declaration (if this is known)
-        arg_str = "(%s)" % (",".join([str(i.knowntype) for i in args]))
-        ret_str = str(retval.knowntype)
-        arg_quote = Paragraph("Function type:", Quote(arg_str), '->',
-                              Quote(ret_str))
-        lst.append(arg_quote)
-        
-        # call sites..
-        call_site_title = Title("Call sites:", belowchar='^')
-        lst.append(call_site_title)
-        call_sites = lst
-        
-        for call_site, frame in self.dsa.get_function_callpoints(fun_name):
-            link_str = "File %s:%s" % (call_site.filename,
-                                       call_site.lineno)
-            link_str, link_target = self.linkgen.getlink(call_site.filename,
-                                                         call_site.lineno)
-            if link_target: # otherwise it's just inline text
-                call_sites.append(Paragraph(Link(link_str, link_target)))
-            else:
-                call_sites.append(Paragraph(link_str))
-            #call_sites.append(LiteralBlock(call_site.source))
-            # XXX: For now, we just paste here the filename of that
-            #call_sites.append(Paragraph(link_str))
-            source = frame.code.source()
-            lines = []
-            for num, line in enumerate(source):
-                if num == call_site.lineno - frame.code.firstlineno - 1:
-                    m = re.match("^( *)(.*)", line)
-                    lines.append(">%s%s" % ("-" * len(m.group(1)), m.group(2)))
-                else:
-                    lines.append(" " + line)
-            call_sites.append(LiteralBlock("\n".join(lines)))
-        
-        return lst
-

Deleted: /py/dist/py/apigen/tracer/testing/test_rest.py
==============================================================================
--- /py/dist/py/apigen/tracer/testing/test_rest.py	Fri Oct 20 13:40:23 2006
+++ (empty file)
@@ -1,201 +0,0 @@
-
-""" tests document generation
-"""
-
-import py
-from StringIO import StringIO
-
-try:
-    from py.__.apigen.tracer.genrest import ViewVC, RestGen, PipeWriter, \
-                                            DirWriter, FileWriter, \
-                                            DirectPaste, DirectFS
-    from py.__.apigen.tracer.tracer import DocStorage, Tracer
-
-    from py.__.apigen.tracer.testing.runtest import cut_pyc
-except ImportError, s:
-    py.test.skip("Cannot import: %s" % str(s))
-
-def setup_module(mod):
-    mod.temppath = py.test.ensuretemp('restgen')
-
-class SomeClass(object):
-    """Some class definition"""
-    
-    def __init__(self, a):
-        self.a = a
-    
-    def method(self, a, b, c):
-        """method docstring"""
-        return a + b + c
-
-class SomeSubClass(SomeClass):
-    """Some subclass definition"""
-
-def fun(a, b, c):
-    """Some docstring
-    
-        Let's make it span a couple of lines to be interesting...
-
-        Note:
-
-         * rest
-         * should
-         * be
-         * supported
-         * or
-         * ignored...
-    """
-    return "d"
-
-def test_direct_link():
-    fname = cut_pyc(__file__)
-    title, link = DirectPaste().getlink(fname, 2)
-    assert title == '%s:%s' % (fname, 2)
-    assert link == ''
-
-def test_viewvc_link():
-    vcview = ViewVC("http://codespeak.net/viewvc/")
-    fname = cut_pyc(__file__)
-    title, link = vcview.getlink(fname, 0)
-    assert title == '%s:%s' % (fname, 0)
-    assert link == ('http://codespeak.net/viewvc/py/apigen/tracer/'
-                        'testing/test_rest.py?view=markup')
-
-def test_fs_link():
-    title, link = DirectFS().getlink('/foo/bar/baz.py', 100)
-    assert title == '/foo/bar/baz.py:100'
-    assert link == 'file:///foo/bar/baz.py'
-
-class WriterTest(object):
-    def get_filled_writer(self, writerclass, *args, **kwargs):
-        dw = writerclass(*args, **kwargs)
-        dw.write_section('foo', 'foo data')
-        dw.write_section('bar', 'bar data')
-        return dw
-
-class TestDirWriter(WriterTest):
-    def test_write_section(self):
-        tempdir = temppath.ensure('dirwriter', dir=True)
-        dw = self.get_filled_writer(DirWriter, tempdir)
-        fpaths = tempdir.listdir('*.txt')
-        assert len(fpaths) == 2
-        assert sorted([f.basename for f in fpaths]) == ['bar.txt', 'foo.txt']
-        assert tempdir.join('foo.txt').read() == 'foo data'
-        assert tempdir.join('bar.txt').read() == 'bar data'
-    
-    def test_getlink(self):
-        dw = DirWriter(temppath.join('dirwriter_getlink'))
-        link = dw.getlink('function', 'Foo.bar', 'method_foo_bar')
-        assert link == 'method_foo_bar.html'
-
-class TestFileWriter(WriterTest):
-    def test_write_section(self):
-        tempfile = temppath.ensure('filewriter', file=True)
-        fw = self.get_filled_writer(FileWriter, tempfile)
-        data = tempfile.read()
-        assert len(data)
-    
-    def test_getlink(self):
-        fw = FileWriter(temppath.join('filewriter_getlink'))
-        link = fw.getlink('function', 'Foo.bar', 'method_foo_bar')
-        assert link == '#function-foo-bar'
-        # only produce the same link target once...
-        link = fw.getlink('function', 'Foo.bar', 'method_foo_bar')
-        assert link is None
-        link = fw.getlink('function', 'Foo.__init__', 'method_foo___init__')
-        assert link == '#function-foo-init'
-
-class TestPipeWriter(WriterTest):
-    def test_write_section(self):
-        s = StringIO()
-        pw = self.get_filled_writer(PipeWriter, s)
-        data = s.getvalue()
-        assert len(data)
-
-    def test_getlink(self):
-        pw = PipeWriter(StringIO())
-        link = pw.getlink('function', 'Foo.bar', 'method_foo_bar')
-        assert link == 'method_foo_bar.txt'
-
-class TestRest(object):
-    def get_filled_docstorage(self):
-        descs = {'SomeClass': SomeClass,
-                 'SomeSubClass': SomeSubClass,
-                 'fun':fun}
-        ds = DocStorage().from_dict(descs)
-        t = Tracer(ds)
-        t.start_tracing()
-        s1 = SomeClass("a")
-        fun(1, 2, s1)
-        s2 = SomeSubClass("b")
-        s2.method(1,2,3)
-        fun(1, 3, s2)
-        t.end_tracing()
-        return ds
-    
-    def check_rest(self, tempdir):
-        from py.__.misc import rest
-        for path in tempdir.listdir('*.txt'):
-            rest.process(path)
-    
-    def test_generation_simple_api(self):
-        ds = self.get_filled_docstorage()
-        lg = DirectPaste()
-        tempdir = temppath.ensure("simple_api", dir=True)
-        r = RestGen(ds, lg, DirWriter(tempdir))
-        r.write()
-        basenames = [p.basename for p in tempdir.listdir('*.txt')]
-        assert sorted(basenames) == [
-            'class_SomeClass.txt',
-            'class_SomeSubClass.txt',
-            'function_fun.txt',
-            'index.txt',
-            'method_SomeClass___init__.txt',
-            'method_SomeClass_method.txt',
-            'method_SomeSubClass___init__.txt',
-            'method_SomeSubClass_method.txt',
-        ]
-        # now we check out...
-        self.check_rest(tempdir)
-
-    def test_check_internal_links(self):
-        ds = self.get_filled_docstorage()
-        lg = DirectFS()
-        tempdir = temppath.ensure('internal_links', dir=True)
-        r = RestGen(ds, lg, DirWriter(tempdir))
-        r.write()
-        index = tempdir.join('index.txt')
-        assert index.check(file=True)
-        data = index.read()
-        assert data.find('.. _`fun`: function_fun.html\n') > -1
-        assert data.find('.. _`fun`: #function-fun\n') == -1
-
-        tempfile = temppath.ensure('internal_links.txt',
-                                  file=True)
-        r = RestGen(ds, lg, FileWriter(tempfile))
-        r.write()
-        data = tempfile.read()
-        assert data.find('.. _`fun`: #function-fun\n') > -1
-        assert data.find('.. _`fun`: function_fun.html') == -1
-
-    def test_check_section_order(self):
-        # we use the previous method's data
-        tempfile = temppath.join('internal_links.txt')
-        if not tempfile.check():
-            py.test.skip('depends on previous test, which failed')
-        data = tempfile.read()
-        # index should be above the rest
-        assert data.find('Exported classes:') > -1
-        assert data.find('Exported classes:') < data.find('Function: fun')
-        assert data.find('Exported classes:') < data.find('Class: SomeClass')
-        # function definitions should be above class ones
-        assert data.find('Function: fun') < data.find('Class: SomeClass')
-        # class method definitions should be below the class defs
-        assert data.find('Class: SomeClass') < data.find(
-                                            'Function: SomeClass.method')
-        # __init__ should be above other methods
-        assert data.find('Function: SomeClass.__init__') > -1
-        # XXX in the future... 
-        # assert data.find('Function: SomeClass.__init__') < data.find(
-        #                                     'Function: SomeClass.method')
-



More information about the pytest-commit mailing list