# HG changeset patch # User k0s # Date 1263227544 18000 # Node ID 29805d442afcf704ab0d85dbfece99cee5cba288 initial commit of contenttransformer; still in the stub stage diff -r 000000000000 -r 29805d442afc contenttransformer/__init__.py --- /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 @@ +# diff -r 000000000000 -r 29805d442afc contenttransformer/app.py --- /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) diff -r 000000000000 -r 29805d442afc contenttransformer/factory.py --- /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) + diff -r 000000000000 -r 29805d442afc contenttransformer/transformers.py --- /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) diff -r 000000000000 -r 29805d442afc contenttransformer/web.py --- /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) diff -r 000000000000 -r 29805d442afc example.ini --- /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 diff -r 000000000000 -r 29805d442afc setup.py --- /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 + """, + ) +