changeset 3:e21f53582267

adding final dissociation stuff
author k0s <k0scist@gmail.com>
date Fri, 12 Feb 2010 01:37:38 -0500
parents df84e61ae1e4
children 28c7009bc463
files dissociate.ini setup.py wordstream/dispatcher.py wordstream/dissociate.py wordstream/factory.py wordstream/handlers.py wordstream/templates/post.html
diffstat 7 files changed, 168 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- /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
--- 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
       """,
       )
       
--- 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)
--- 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()
--- 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
--- 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='<html><body>%s</body></html>' % 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='<html><body>%s</body></html>' % dissociation)
--- /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 @@
+<html>
+<head>
+</head>
+<body>
+
+<form method="post">
+Type in some text
+<textarea name="text" rows="25" cols="80"></textarea>
+<input type="submit" value="Dissociate"/>
+</form>
+
+<form method="post" enctype="multipart/form-data">
+or upload a file
+<input type="file" name="file"/>
+<input type="submit" value="Dissociate"/>
+</form>
+
+<form method="post">
+or point to a URL
+<input type="text" name="url"/>
+<input type="submit" value="Dissociate"/>
+</form>
+</body>
+</html>
\ No newline at end of file