changeset 0:29805d442afc

initial commit of contenttransformer; still in the stub stage
author k0s <k0scist@gmail.com>
date Mon, 11 Jan 2010 11:32:24 -0500
parents
children aa491070ccf3
files contenttransformer/__init__.py contenttransformer/app.py contenttransformer/factory.py contenttransformer/transformers.py contenttransformer/web.py example.ini setup.py
diffstat 7 files changed, 157 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contenttransformer/__init__.py	Mon Jan 11 11:32:24 2010 -0500
@@ -0,0 +1,1 @@
+#
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contenttransformer/app.py	Mon Jan 11 11:32:24 2010 -0500
@@ -0,0 +1,29 @@
+import os
+import sys
+from fnmatch import fnmatch
+from paste.fileapp import FileApp
+from pkg_resources import iter_entry_points
+
+class FileTypeTransformer(object):
+    transformers = {}
+
+    def __init__(self, *types, **kwargs):
+        """types is a list of two-tuples: glob pattern (string), transformer name (string, name of entry point)"""
+        self.types = types
+        self.kwargs = kwargs # intended to be arguments to the xformers
+        for blah in iter_entry_points('content_transformers'):
+            try:
+                self.transformers['foo'] = entry_point.load()
+            except:
+                raise
+        for pattern, transformer_name in self.types:
+            assert transformer_name in self.transformers
+
+    def __call__(self, path):
+        """this should return something that is callable with (environ, start_response) to return a response; the transformer thing"""
+        filename = os.path.basename(path)
+        for pattern, transformer_name in self.types:
+            if fnmatch(filename, pattern):
+                content = file(path).read()
+                return self.transformers[transformer_name](content)
+        return FileApp(path)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contenttransformer/factory.py	Mon Jan 11 11:32:24 2010 -0500
@@ -0,0 +1,12 @@
+from dispatcher import Dispatcher
+from paste.httpexceptions import HTTPExceptionHandler
+
+def factory(global_conf, **app_conf):
+    """create a webob view and wrap it in middleware"""
+    keystr = 'typetransformer.'
+    args = dict([(key.split(keystr, 1)[-1], value)
+                 for key, value in app_conf.items()
+                 if key.startswith(keystr) ])
+    app = Dispatcher(**args)
+    return HTTPExceptionHandler(app)
+    
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contenttransformer/transformers.py	Mon Jan 11 11:32:24 2010 -0500
@@ -0,0 +1,23 @@
+import docutils.core
+from webob import Response
+
+class Graphviz(object):
+    def __init__(self, content):
+        """create a Graphviz object"""
+
+    def __call__(self, environ, start_response):
+        """return a WSGI response"""
+   
+
+class RestructuredText(object):
+    settings = { 'report_level': 5 }
+
+    def __init__(self, content):
+        """template: genshi(?) template to use (???)"""
+        self.html = docutils.core.publish_string(content,
+                                                 writer_name='html',
+                                                 settings_overrides=self.settings)
+
+    def __call__(self, environ, start_response):
+        """return a WSGI response"""
+        return Response(content_type='text/html', body=self.html)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contenttransformer/web.py	Mon Jan 11 11:32:24 2010 -0500
@@ -0,0 +1,35 @@
+"""
+example app for how to use TypedTransformer TTW on a directory
+"""
+
+import os
+
+from contenttransformer.app import FileTypeTransformer
+from webob import Request, exc
+
+class Dispatcher(object):
+
+    ### class level variables
+    defaults = { 'app': None, 
+                 'directory': None, 
+                 'transforms': ''}
+
+    def __init__(self, **kw):
+        for key in self.defaults:
+            setattr(self, key, kw.get(key, self.defaults[key]))
+        assert os.path.exists(self.directory)
+        self.transforms = [ [j.strip() for j in i.split('=', 1) ] for i in self.transforms.split(',') if '=' in i]
+        self.handler = FileTypeTransformer(*self.transforms)
+        if self.app:
+            assert hasattr(self.app, '__call__')
+
+    ### methods dealing with HTTP
+    def __call__(self, environ, start_response):
+        request = Request(environ)
+        path = os.path.join(self.directory, request.path_info)
+        if os.path.exists(path):
+            handler = 
+        else:
+            handler = exc.HTTPNotFound()
+            pass # TODO: if self.app ...
+        return res(environ, start_response)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example.ini	Mon Jan 11 11:32:24 2010 -0500
@@ -0,0 +1,22 @@
+#!/usr/bin/env paster
+
+[DEFAULT]
+debug = true
+email_to = k0scist@gmail.com
+smtp_server = localhost
+error_email_from = paste@localhost
+
+[server:main]
+use = egg:Paste#http
+host = 0.0.0.0
+port = 6666
+
+[composite:main]
+use = egg:Paste#urlmap
+/ = typetransformer
+
+set debug = false
+
+[app:typetransformer]
+paste.app_factory = typetransformer.factory:factory
+typetransformer.directory = %(here)s/example
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/setup.py	Mon Jan 11 11:32:24 2010 -0500
@@ -0,0 +1,35 @@
+from setuptools import setup, find_packages
+import sys, os
+
+version = "0.0"
+
+setup(name='contentransformer',
+      version=version,
+      description="transform e.g. file data based on type to be served TTW",
+      long_description="""
+""",
+      classifiers=[], # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers
+      author='Jeff Hammel',
+      author_email='k0scist@gmail.com',
+      url='http://k0s.org',
+      license="",
+      packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
+      include_package_data=True,
+      zip_safe=False,
+      install_requires=[
+          # -*- Extra requirements: -*-
+         'WebOb',	
+         'Paste',
+         'PasteScript',
+      ],
+      entry_points="""
+      # -*- Entry points: -*-
+      [paste.app_factory]
+      main = contenttransformer.factory:factory
+
+      [content_transformers]
+      Graphviz = contenttransformer.transformers:Graphviz
+      ReST = contenttransformer.transformers.RestructuredText
+      """,
+      )
+