[py-svn] r35370 - py/dist/py/apigen/source

guido at codespeak.net guido at codespeak.net
Wed Dec 6 12:11:46 CET 2006


Author: guido
Date: Wed Dec  6 12:11:44 2006
New Revision: 35370

Modified:
   py/dist/py/apigen/source/server.py
Log:
Added support for browsing directories (not yet styled and such).


Modified: py/dist/py/apigen/source/server.py
==============================================================================
--- py/dist/py/apigen/source/server.py	(original)
+++ py/dist/py/apigen/source/server.py	Wed Dec  6 12:11:44 2006
@@ -3,9 +3,66 @@
 """
 
 import py
+import time
 from pypy.translator.js.examples import server
 from py.__.apigen.source.browser import parse_path
 from py.__.apigen.source.html import create_html
+from py.xml import html
+
+def create_dir_html(path):
+    h = html.html(
+        html.head(
+            html.title('directory listing of %s' % (path,)),
+        ),
+    )
+    body = html.body(
+        html.h1('directory listing of %s' % (path,)),
+    )
+    h.append(body)
+    table = html.table()
+    body.append(table)
+    tbody = html.tbody()
+    table.append(tbody)
+    items = list(path.listdir())
+    items.sort(key=lambda p: p.basename)
+    items.sort(key=lambda p: not p.check(dir=True))
+    for fpath in items:
+        tr = html.tr()
+        tbody.append(tr)
+        td1 = html.td(fpath.check(dir=True) and 'D' or 'F')
+        tr.append(td1)
+        href = fpath.basename
+        if fpath.check(dir=True):
+            href += '/'
+        td2 = html.td(html.a(fpath.basename, href=href))
+        tr.append(td2)
+        td3 = html.td(time.strftime('%Y-%m-%d %H:%M:%S',
+                      time.gmtime(fpath.mtime())))
+        tr.append(td3)
+        if fpath.check(dir=True):
+            size = '-'
+            unit = ''
+        else:
+            size = fpath.size()
+            unit = 'B'
+            for u in ['kB', 'MB', 'GB', 'TB']:
+                if size > 1024:
+                    size = round(size / 1024.0, 2)
+                    unit = u
+        td4 = html.td('%s %s' % (size, unit))
+        tr.append(td4)
+    return unicode(h)
+
+def create_unknown_html(path):
+    h = html.html(
+        html.head(
+            html.title('Can not display page'),
+        ),
+        html.body(
+            html.p('The data URL (%s) does not contain Python code.' % (path,))
+        ),
+    )
+    return h.unicode()
 
 class Handler(server.TestHandler):
     BASE_URL='http://codespeak.net/svn/py/dist'
@@ -16,11 +73,20 @@
             url = url[:-3] + '.py'
         path = py.path.svnurl(url)
         if not path.check():
-            raise AttributeError()
+            def f(rev=None):
+                return create_unknown_html(path)
+            f.exposed = True
+            f.func_name = attr
+            return f
         def f(rev='HEAD'):
             path = py.path.svnurl(url, rev)
             # some try.. except.. here
-            return unicode(create_html(parse_path(path)))
+            if path.check(file=True):
+                return unicode(create_html(parse_path(path)))
+            elif path.check(dir=True):
+                return create_dir_html(path)
+            else:
+                return create_unknown_html(path)
         f.exposed = True
         f.func_name = attr
         return f


More information about the py-svn mailing list