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

guido at codespeak.net guido at codespeak.net
Mon Jan 8 16:46:34 CET 2007


Author: guido
Date: Mon Jan  8 16:46:12 2007
New Revision: 36281

Added:
   py/dist/py/apigen/source/index.cgi   (contents, props changed)
Modified:
   py/dist/py/apigen/source/html.py
   py/dist/py/apigen/source/server.py
Log:
Added CGI script as replacement for server.py (which is not removed yet), moved
some HTML related stuff to html.py.


Modified: py/dist/py/apigen/source/html.py
==============================================================================
--- py/dist/py/apigen/source/html.py	(original)
+++ py/dist/py/apigen/source/html.py	Mon Jan  8 16:46:12 2007
@@ -4,6 +4,7 @@
 
 from py.xml import html, raw
 from compiler import ast
+import time
 from py.__.apigen.source.color import Tokenizer, PythonSchema
 
 class HtmlEnchanter(object):
@@ -162,3 +163,88 @@
         doc.add_row(i + 1, row)
     return unicode(doc)
 
+style = html.style("""
+  
+  body, p, td {
+    background-color: #FFF;
+    color: black;
+    font-family: monospace;
+  }
+
+  td.type {
+    width: 2em;
+  }
+
+  td.name {
+    width: 30em;
+  }
+
+  td.mtime {
+    width: 13em;
+  }
+
+  td.size {
+    text-alignment: right;
+  }
+
+""")
+
+def create_dir_html(path, href_prefix=''):
+    h = html.html(
+        html.head(
+            html.title('directory listing of %s' % (path,)),
+            style,
+        ),
+    )
+    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', class_='type')
+        tr.append(td1)
+        href = fpath.basename
+        if href_prefix:
+            href = '%s%s' % (href_prefix, href)
+        if fpath.check(dir=True):
+            href += '/'
+        td2 = html.td(html.a(fpath.basename, href=href), class_='name')
+        tr.append(td2)
+        td3 = html.td(time.strftime('%Y-%m-%d %H:%M:%S',
+                      time.gmtime(fpath.mtime())), class_='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), class_='size')
+        tr.append(td4)
+    return unicode(h)
+
+def create_unknown_html(path):
+    h = html.html(
+        html.head(
+            html.title('Can not display page'),
+            style,
+        ),
+        html.body(
+            html.p('The data URL (%s) does not contain Python code.' % (path,))
+        ),
+    )
+    return h.unicode()
+

Added: py/dist/py/apigen/source/index.cgi
==============================================================================
--- (empty file)
+++ py/dist/py/apigen/source/index.cgi	Mon Jan  8 16:46:12 2007
@@ -0,0 +1,28 @@
+#!/usr/bin/python
+
+import cgitb;cgitb.enable()
+import py
+from py.__.apigen.source.browser import parse_path
+from py.__.apigen.source.html import create_html, create_dir_html, \
+                                     create_unknown_html
+
+BASE_URL='http://codespeak.net/svn/py/dist'
+def cgi_main():
+    import os
+    reqpath = os.environ.get('PATH_INFO', '')
+    path = py.path.svnurl('%s%s' % (BASE_URL, reqpath))
+    if not path.check():
+        return create_unknown_html(path)
+    if path.check(file=True):
+        return unicode(create_html(parse_path(path)))
+    elif path.check(dir=True):
+        prefix = ''
+        if not reqpath:
+            prefix = 'index.cgi/'
+        return create_dir_html(path, href_prefix=prefix)
+    else:
+        return create_unknown_html(path)
+
+print 'Content-Type: text/html; charset=UTF-8'
+print 
+print cgi_main()

Modified: py/dist/py/apigen/source/server.py
==============================================================================
--- py/dist/py/apigen/source/server.py	(original)
+++ py/dist/py/apigen/source/server.py	Mon Jan  8 16:46:12 2007
@@ -3,98 +3,14 @@
 """
 
 import py
-import time
 try:
     from pypy.translator.js.examples import server
 except ImportError:
     py.test.skip("PyPy not found")
 from py.__.apigen.source.browser import parse_path
-from py.__.apigen.source.html import create_html
+from py.__.apigen.source.html import create_html, create_dir_html, create_unknown_html
 from py.xml import html
 
-style = html.style("""
-  
-  body, p, td {
-    background-color: #FFF;
-    color: black;
-    font-family: monospace;
-  }
-
-  td.type {
-    width: 2em;
-  }
-
-  td.name {
-    width: 30em;
-  }
-
-  td.mtime {
-    width: 13em;
-  }
-
-  td.size {
-    text-alignment: right;
-  }
-
-""")
-
-def create_dir_html(path):
-    h = html.html(
-        html.head(
-            html.title('directory listing of %s' % (path,)),
-            style,
-        ),
-    )
-    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', class_='type')
-        tr.append(td1)
-        href = fpath.basename
-        if fpath.check(dir=True):
-            href += '/'
-        td2 = html.td(html.a(fpath.basename, href=href), class_='name')
-        tr.append(td2)
-        td3 = html.td(time.strftime('%Y-%m-%d %H:%M:%S',
-                      time.gmtime(fpath.mtime())), class_='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), class_='size')
-        tr.append(td4)
-    return unicode(h)
-
-def create_unknown_html(path):
-    h = html.html(
-        html.head(
-            html.title('Can not display page'),
-            style,
-        ),
-        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'
 
@@ -129,3 +45,4 @@
 
 if __name__ == '__main__':
     _main()
+


More information about the py-svn mailing list