# HG changeset patch # User k0s # Date 1265956658 18000 # Node ID e21f535822676467e1f8d6a04db5e53bd660c3e3 # Parent df84e61ae1e4cb53ff1a735b080fc3c1c8168028 adding final dissociation stuff diff -r df84e61ae1e4 -r e21f53582267 dissociate.ini --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dissociate.ini Fri Feb 12 01:37:38 2010 -0500 @@ -0,0 +1,21 @@ +#!/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 = 5326 + +[composite:main] +use = egg:Paste#urlmap +/ = dissociate + +set debug = false + +[app:dissociate] +paste.app_factory = wordstream.factory:dissociate_factory diff -r df84e61ae1e4 -r e21f53582267 setup.py --- a/setup.py Fri Feb 12 00:38:25 2010 -0500 +++ b/setup.py Fri Feb 12 01:37:38 2010 -0500 @@ -29,7 +29,7 @@ [console_scripts] wordstream = wordstream.main:main - dissociate = wordstream.dissociate:dissociate + dissociate = wordstream.dissociate:main """, ) diff -r df84e61ae1e4 -r e21f53582267 wordstream/dispatcher.py --- a/wordstream/dispatcher.py Fri Feb 12 00:38:25 2010 -0500 +++ b/wordstream/dispatcher.py Fri Feb 12 01:37:38 2010 -0500 @@ -5,7 +5,7 @@ import os -from handlers import Index, Feed, Eat, ViewCorpus +from handlers import Index, Feed, Eat, ViewCorpus, Dissociate from genshi.template import TemplateLoader from paste.fileapp import FileApp @@ -67,3 +67,49 @@ # get response res = handler() return res(environ, start_response) + + +class Scrambler(object): + + ### class level variables + defaults = { 'auto_reload': 'False', + 'template_dirs': '', } + + def __init__(self, **kw): + + # set instance parameters from kw and defaults + for key in self.defaults: + setattr(self, key, kw.get(key, self.defaults[key])) + self.auto_reload = self.auto_reload.lower() == 'true' + + # request handlers + self.handlers = [ Dissociate ] + + # template loader + self.template_dirs = self.template_dirs.split() + self.template_dirs.append(resource_filename(__name__, 'templates')) + self.loader = TemplateLoader(self.template_dirs, + auto_reload=self.auto_reload) + + def __call__(self, environ, start_response): + + # get a request object + request = Request(environ) + + # get the path + path = request.path_info.strip('/').split('/') + if path == ['']: + path = [] + request.environ['path'] = path + + # match the request to a handler + for h in self.handlers: + handler = h.match(self, request) + if handler is not None: + break + else: + handler = exc.HTTPNotFound + + # get response + res = handler() + return res(environ, start_response) diff -r df84e61ae1e4 -r e21f53582267 wordstream/dissociate.py --- a/wordstream/dissociate.py Fri Feb 12 00:38:25 2010 -0500 +++ b/wordstream/dissociate.py Fri Feb 12 01:37:38 2010 -0500 @@ -8,23 +8,26 @@ from pprint import pprint from wordstream.api import Corpus -def dissociate(args=sys.argv[1:]): +def dissociate(corpus, output=sys.stdout): + while corpus: + word = random.choice(corpus.keys()) + inedible = True + while corpus.get(word): + inedible = False + print>> output, word, + word = corpus.eat(word) + if inedible: # eat it anyway + corpus.eat(word) + + +def main(args=sys.argv[1:]): parser = OptionParser() options, args = parser.parse_args() corpus = Corpus() corpus.feed_stuff(*args) corpus.scramble() - - while corpus: - word = random.choice(corpus.keys()) - inedible = True - while corpus.get(word): - inedible = False - print word + ' ', - word = corpus.eat(word) - if inedible: # eat it anyway - corpus.eat(word) + dissociate(corpus) if __name__ == '__main__': - dissociate() + main() diff -r df84e61ae1e4 -r e21f53582267 wordstream/factory.py --- a/wordstream/factory.py Fri Feb 12 00:38:25 2010 -0500 +++ b/wordstream/factory.py Fri Feb 12 01:37:38 2010 -0500 @@ -1,6 +1,7 @@ import os from dispatcher import Dispatcher +from dispatcher import Scrambler from paste.httpexceptions import HTTPExceptionHandler from paste.urlparser import StaticURLParser from pkg_resources import resource_filename @@ -29,3 +30,11 @@ app = Dispatcher(**args) return HTTPExceptionHandler(PassthroughFileserver(app, resource_filename(__name__, 'static'))) +def dissociate_factory(global_conf, **app_conf): + keystr = 'dissociate.' + args = dict([(key.split(keystr, 1)[-1], value) + for key, value in app_conf.items() + if key.startswith(keystr) ]) + app = Scrambler(**args) + return HTTPExceptionHandler(app) + \ No newline at end of file diff -r df84e61ae1e4 -r e21f53582267 wordstream/handlers.py --- a/wordstream/handlers.py Fri Feb 12 00:38:25 2010 -0500 +++ b/wordstream/handlers.py Fri Feb 12 01:37:38 2010 -0500 @@ -3,10 +3,14 @@ these are instantiated for every request, then called """ +import urllib2 + from pprint import pprint from urlparse import urlparse from webob import Response, exc from StringIO import StringIO +from wordstream.api import Corpus +from wordstream.dissociate import dissociate class HandlerMatchException(Exception): """the handler doesn't match the request""" @@ -125,3 +129,50 @@ def __call__(self): self.app.corpus.feed_stream(self.request.environ['path']) return exc.HTTPOk() + + +class Dissociate(GenshiHandler): + template = 'post.html' + methods = set(['GET', 'POST']) + + + @classmethod + def match(cls, app, request): + + # check the method + if request.method not in cls.methods: + return None + + return cls(app, request) + + def Get(self): + if 'url' in self.request.GET: + contents = self.url_contents(self.request.GET['url']) + dissociation = self.dissociation(contents) + return Response(content_type='text/html', + body='%s' % dissociation) + + + return GenshiHandler.Get(self) + + def url_contents(self, url): + return urllib2.urlopen(url).read() + + def dissociation(self, contents): + corpus = Corpus() + corpus.feed_stream(contents) + corpus.scramble() + buffer = StringIO() + dissociate(corpus, buffer) + return buffer.getvalue() + + def Post(self): + if 'url' in self.request.POST: + contents = self.url_contents(self.request.POST['url']) + elif 'text' in self.request.POST: + contents = self.request.POST['text'] + elif 'file' in self.request.POST: + contents = self.request.POST['file'].file.read() + dissociation = self.dissociation(contents) + return Response(content_type='text/html', + body='%s' % dissociation) diff -r df84e61ae1e4 -r e21f53582267 wordstream/templates/post.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wordstream/templates/post.html Fri Feb 12 01:37:38 2010 -0500 @@ -0,0 +1,24 @@ + + + + + +
+Type in some text + + +
+ +
+or upload a file + + +
+ +
+or point to a URL + + +
+ + \ No newline at end of file