[py-svn] r33068 - in py/branch/apigen/py: path/local rst test/tracer test/tracer/testing

fijal at codespeak.net fijal at codespeak.net
Mon Oct 9 17:12:15 CEST 2006


Author: fijal
Date: Mon Oct  9 17:12:12 2006
New Revision: 33068

Added:
   py/branch/apigen/py/test/tracer/testing/runtest.py   (contents, props changed)
Modified:
   py/branch/apigen/py/path/local/local.py
   py/branch/apigen/py/rst/rst.py
   py/branch/apigen/py/test/tracer/docstorage.py
   py/branch/apigen/py/test/tracer/genrest.py
   py/branch/apigen/py/test/tracer/testing/test_docgen.py
   py/branch/apigen/py/test/tracer/testing/test_rest.py
Log:
Intermediate checkin...


Modified: py/branch/apigen/py/path/local/local.py
==============================================================================
--- py/branch/apigen/py/path/local/local.py	(original)
+++ py/branch/apigen/py/path/local/local.py	Mon Oct  9 17:12:12 2006
@@ -618,6 +618,27 @@
                         path.remove(rec=1)
                     except py.error.Error:
                         pass
+        
+        # make link...
+        try:
+            username = os.environ['USER']           #linux, et al
+        except:
+            try:
+                username = os.environ['USERNAME']   #windows
+            except:
+                username = 'current'
+
+        src  = str(udir)
+        dest = src[:src.rfind('-')] + '-' + username
+        try:
+            os.unlink(dest)
+        except:
+            pass
+        try:
+            os.symlink(src, dest)
+        except:
+            pass
+
         return udir
     make_numbered_dir = classmethod(make_numbered_dir)
 

Modified: py/branch/apigen/py/rst/rst.py
==============================================================================
--- py/branch/apigen/py/rst/rst.py	(original)
+++ py/branch/apigen/py/rst/rst.py	Mon Oct  9 17:12:12 2006
@@ -216,3 +216,7 @@
         while next.parent is not None:
             next = next.parent
         return next
+
+class Quote(AbstractText):
+    start = '``'
+    end = '``'

Modified: py/branch/apigen/py/test/tracer/docstorage.py
==============================================================================
--- py/branch/apigen/py/test/tracer/docstorage.py	(original)
+++ py/branch/apigen/py/test/tracer/docstorage.py	Mon Oct  9 17:12:12 2006
@@ -96,10 +96,14 @@
     def __init__(self, ds):
         self.ds = ds
 
+    def _get_names(self, filter):
+        return [i for i, desc in self.ds.descs.iteritems() if filter(i, desc)]
+
     def get_function_names(self):
-        # get iterator over all API exposed names
-        return [i for i, desc in self.ds.descs.iteritems() if isinstance(desc, 
-            FunctionDesc)]
+        return self._get_names(lambda i, desc: type(desc) is FunctionDesc)
+    
+    def get_class_names(self):
+        return self._get_names(lambda i, desc: isinstance(desc, ClassDesc))
     
     #def get_function(self, name):
     #    return self.ds.descs[name].pyobj

Modified: py/branch/apigen/py/test/tracer/genrest.py
==============================================================================
--- py/branch/apigen/py/test/tracer/genrest.py	(original)
+++ py/branch/apigen/py/test/tracer/genrest.py	Mon Oct  9 17:12:12 2006
@@ -41,53 +41,107 @@
         relname = filename[len(path):]
         if relname.endswith('.pyc'):
             relname = relname[:-1]
-        return self.basepath + relname[1:] + '?view=markup'
+        return ('%s:%s' % (filename, lineno), self.basepath + relname[1:] + '?view=markup')
+
+class DirectPaste(AbstractLinkWriter):
+    """ No-link writer (inliner)
+    """
+    def getlink(self, filename, lineno):
+        return (py.path.local(filename).open().readlines()[lineno-1], "")
+
+class PipeWriter(object):
+    def __init__(self, output=sys.stdout):
+        self.output = output
+    
+    def write_file(self, filename, data):
+        text = "Written file: %s" % filename
+        self.output.write(text + "\n")
+        self.output.write("=" * len(text) + "\n")
+        self.output.write("\n")
+        self.output.write(data + "\n")
+    
+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_file(self, filename, data):
+        self.directory.ensure(filename).write(data)
 
 class RestGen(object):
-    def __init__(self, ds, linkgen, output=sys.stdout):
+    def __init__(self, ds, linkgen, writer=PipeWriter()):
         self.dsa = DocStorageAccessor(ds)
         self.linkgen = linkgen
-        self.output = output
-        
-    def add(self, item):
-        self.list.append(item)
+        self.writer = writer
     
     def write(self):
-        self.list = []
-        self.add(Title("Module: %s" % self.dsa.get_module_name(), belowchar="="))
-        self.add(Paragraph(Text(self.dsa.get_module_info())))
-        self.write_functions()
-        self.write_classes()
-    
-    def write_functions(self):
-        self.add(Title("Exported functions:", belowchar="-"))
-        for key in self.dsa.get_function_names():
-            title = Title("%s description:" % key, belowchar="^")
-            docstr = Paragraph(self.dsa.get_function_doc(key))
-            args = self.dsa.get_function_args(key)
-            arg_str = "(%s)" % (",".join([str(i) for i in args]))
-            arg_quote = BlockQuote("Function type: %s" % arg_str)
-            
-            call_site_title = Title("Call sites:", belowchar="-")
-            call_sites = []
-            
-            for call_site in self.dsa.get_function_callpoints(key):
-                link_str = "File %s:%s" % (call_site.filename,
-                    call_site.lineno)
-                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(BlockQuote(call_site.source))
-            
-            for item in [title, docstr, arg_quote, call_site_title] + call_sites:
-                self.add(item)
-            
-            #for call_site in self.dsa.get_function_callpoints(key):
-            #    self.writeline("File %s:%s\n%s" % call_site.get_tuple())
-        self.output.write(Rest(*self.list).text())
-
-    def write_classes(self):
-        self.add(Title("Exported classes:", belowchar="-"))
+        # first write down a file with all the exported interfaces,
+        # sorted by type
+        self.write_interface("index.txt")
+    
+    def write_interface(self, filename):
+        lst = [Title("Module: %s" % self.dsa.get_module_name(), belowchar="="),
+            Paragraph(self.dsa.get_module_info()),
+            Title("Exported functions:", belowchar="-")]
+        self.write_function_list(lst)
+        self.write_class_list(lst)
+        self.writer.write_file(filename, Rest(*lst).text())
+    
+    def write_function_list(self, lst):
+        for name in self.dsa.get_function_names():
+            # XXX: should be .html here?
+            lst.append(ListItem(Text("Function: "), Link(name, "function_%s" % name)))
+    
+    def write_class_list(self, lst):
+        for name in self.dsa.get_class_names():
+            lst.append(ListItem(Text("Class: "), Link(name, "class_%s" % name)))
+##class RestGen(object):
+##    def __init__(self, ds, linkgen, output=sys.stdout):
+##        self.dsa = DocStorageAccessor(ds)
+##        self.linkgen = linkgen
+##        self.output = output
+##        
+##    def add(self, item):
+##        self.list.append(item)
+##    
+##    def write(self):
+##        self.list = []
+##        self.add(Title("Module: %s" % self.dsa.get_module_name(), belowchar="="))
+##        self.add(Paragraph(Text(self.dsa.get_module_info())))
+##        self.write_functions()
+##        self.write_classes()
+##    
+##    def write_functions(self):
+##        self.add(Title("Exported functions:", belowchar="-"))
+##        for key in self.dsa.get_function_names():
+##            title = Title("%s description:" % key, belowchar="^")
+##            docstr = Paragraph(self.dsa.get_function_doc(key))
+##            args = self.dsa.get_function_args(key)
+##            arg_str = "(%s)" % (",".join([str(i) for i in args]))
+##            arg_quote = BlockQuote("Function type: %s" % arg_str)
+##            
+##            call_site_title = Title("Call sites:", belowchar="-")
+##            call_sites = []
+##            
+##            for call_site in self.dsa.get_function_callpoints(key):
+##                link_str = "File %s:%s" % (call_site.filename,
+##                    call_site.lineno)
+##                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(BlockQuote(call_site.source))
+##            
+##            for item in [title, docstr, arg_quote, call_site_title] + call_sites:
+##                self.add(item)
+##            
+##            #for call_site in self.dsa.get_function_callpoints(key):
+##            #    self.writeline("File %s:%s\n%s" % call_site.get_tuple())
+##        self.output.write(Rest(*self.list).text())
+##
+##    def write_classes(self):
+##        self.add(Title("Exported classes:", belowchar="-"))
         

Added: py/branch/apigen/py/test/tracer/testing/runtest.py
==============================================================================
--- (empty file)
+++ py/branch/apigen/py/test/tracer/testing/runtest.py	Mon Oct  9 17:12:12 2006
@@ -0,0 +1,5 @@
+
+def cut_pyc(f_name):
+    if f_name.endswith('.pyc'):
+        return f_name[:-1]
+    return f_name

Modified: py/branch/apigen/py/test/tracer/testing/test_docgen.py
==============================================================================
--- py/branch/apigen/py/test/tracer/testing/test_docgen.py	(original)
+++ py/branch/apigen/py/test/tracer/testing/test_docgen.py	Mon Oct  9 17:12:12 2006
@@ -6,6 +6,7 @@
 import sys
 
 from py.__.test.tracer.tracer import DocStorage, Tracer
+from py.__.test.tracer.testing.runtest import cut_pyc
 from pypy.annotation import model
 
 #def setup_module(mod):
@@ -33,9 +34,7 @@
     assert isinstance(desc.retval, model.SomeChar)
     cs = desc.call_sites
     assert len(cs) == 2
-    f_name = __file__
-    if f_name.endswith('.pyc'):
-        f_name = f_name[:-1]
+    f_name = cut_pyc(__file__)
     assert cs[0].filename == f_name
     assert cs[0].lineno == test_basic.func_code.co_firstlineno + 5
     assert cs[1].filename == f_name

Modified: py/branch/apigen/py/test/tracer/testing/test_rest.py
==============================================================================
--- py/branch/apigen/py/test/tracer/testing/test_rest.py	(original)
+++ py/branch/apigen/py/test/tracer/testing/test_rest.py	Mon Oct  9 17:12:12 2006
@@ -2,34 +2,80 @@
 """ tests document generation
 """
 
-from py.__.test.tracer.genrest import ViewVC, RestGen
+import py
+
+from py.__.test.tracer.genrest import ViewVC, RestGen, PipeWriter, DirWriter,\
+    DirectPaste
 from py.__.test.tracer.tracer import DocStorage, Tracer
 
 from StringIO import StringIO
+from py.__.test.tracer.testing.runtest import cut_pyc
 
 def test_links():
     vcview = ViewVC("http://codespeak.net/viewvc/")
-    linkname = vcview.getlink(__file__, 0)
-    assert linkname == 'http://codespeak.net/viewvc/py/test/'\
-        'tracer/testing/test_rest.py?view=markup'
+    _, linkname = vcview.getlink(cut_pyc(__file__), 0)
+    assert linkname == 'http://codespeak.net/viewvc/py/test/tracer/testing/test_rest.py?view=markup'
+
+class SomeClass(object):
+    def __init__(self, a):
+        self.a = a
+    
+    def method(self, a, b, c):
+        return a + b + c
 
 def fun(a, b, c):
     "Some docstring"
     return "d"
 
-def test_generation():
-    descs = {"fun":fun}
-    ds = DocStorage().from_dict(descs)
-    lg = ViewVC("http://codespeak.net/viewvc/")
-    t = Tracer(ds)
-    t.start_tracing()
-    fun(1, ("g", 3), 8)
-    fun(2., ("a", 1.), "a")
-    t.end_tracing()
-    s = StringIO()
-    r = RestGen(ds, lg, output=s)
-    r.write()
-    # we cannot check the source, cause the rapidly changing
-    # visual effect will break it, so we'll make assertion later
-    # XXX: do that
-    assert s.getvalue()
+def test_dir_writer():
+    p = StringIO()
+    dir = py.test.ensuretemp("dirwriter")
+    w = DirWriter(dir)
+    w.write_file("one", "one data")
+    w.write_file("two", "two data")
+    assert dir.join("one").read() == "one data"
+    assert dir.join("two").read() == "two data"
+
+def test_direct_link():
+    assert DirectPaste().getlink(cut_pyc(__file__), 2)[0] == '""" tests document generation\n'
+    # C-c C-v ....
+
+class TestRest(object):
+    def check_rest(self, tmpdir):
+        pass
+    
+    def test_generation_simple_api(self):
+        descs = {'SomeClass':SomeClass, 'fun':fun}
+        ds = DocStorage().from_dict(descs)
+        lg = DirectPaste()
+        t = Tracer(ds)
+        t.start_tracing()
+        s1 = SomeClass("a")
+        fun(1, 2, s1)
+        s2 = SomeClass("b")
+        s2.method(1,2,3)
+        fun(1, 3, s2)
+        t.end_tracing()
+        tmpdir = py.test.ensuretemp("test_generation")
+        r = RestGen(ds, lg, DirWriter(tmpdir))
+        r.write()
+        # now we check out...
+        self.check_rest(tmpdir)
+
+##def test_generation():
+##    py.test.skip("WIP")
+##    descs = {"fun":fun}
+##    ds = DocStorage().from_dict(descs)
+##    lg = ViewVC("http://codespeak.net/viewvc/")
+##    t = Tracer(ds)
+##    t.start_tracing()
+##    fun(1, ("g", 3), 8)
+##    fun(2., ("a", 1.), "a")
+##    t.end_tracing()
+##    s = StringIO()
+##    r = RestGen(ds, lg, output=s)
+##    r.write()
+##    # we cannot check the source, cause the rapidly changing
+##    # visual effect will break it, so we'll make assertion later
+##    # XXX: do that
+##    assert s.getvalue()


More information about the py-svn mailing list