[Grok-dev] Middleware and Grok

bribon alvreinoso at gmail.com
Wed May 5 19:02:28 EDT 2010




Martijn Faassen-2 wrote:
> 
> bribon wrote:
> 
>> This is the code at debug.ini:
>> 
>> [app:zope]
>> use = egg:grokserver
>> filter-with = translogger
>> paste.app_factory = zope.paste.application:zope_publisher_app_factory
>> 
>> [pipeline:Paste.Main]
>> pipeline = fileupload main
>> 
>> [filter:fileupload]
>> paste.filter_factory = FileUploadMedia:make_demo
>> #use = egg:gp.fileupload
>> tempdir = %(here)s/data/fileupload
>> 
>> I have some questions:
>> 
>> Where do I have to store the application in the grok server?
> 
> I'm not sure what you mean by 'store the application'. If you're talking 
> about the implementation of the WSGI app, you can just put it in your 
> grok project along with the other code. You can then refer to it from 
> your debug.ini.
> 
>> How can I get the middleware application running?
> 
> Hm, this is a rather broad question. :)
> 
>> Do I need to create a egg?
> 
> If you have code that you'd like to distribute through pypi, then you 
> could make a .tgz distribution (an actual .egg is not recommended unless 
> you have C code included, so no for you). If you just want to have code 
> you distribute internally, you could use:
> 
> python setup.py sdist
> 
> To make a 'tgz' file in the 'dist' directory of your package. You'll 
> need to set up a setup.py in that case.
> 
> But I don't think there's any need to create an egg to get your 
> middleware going. You need to implement your middleware in some Python 
> package or module, and the easiest is in your grok application to get 
> started. Presumably though you will want to reuse the middleware later 
> in other applications, and then it's time to make a library and possibly 
> distribute it to the Python Package index (pypi).
> 
> Regards,
> 
> Martijn
> 
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> https://mail.zope.org/mailman/listinfo/grok-dev
> 
> 

Hi,

Sorry, but I don't get it. I couldn't find any example which I can check it
out....

I'm not sure what you mean by 'store the application'. If you're talking 
about the implementation of the WSGI app, you can just put it in your 
grok project along with the other code. You can then refer to it from 
your debug.ini.

How can I refer to it from this code if I put it in my grok project? is this
code Ok because I didn't work out? 

[app:zope]
use = egg:grokserver
filter-with = translogger
paste.app_factory = zope.paste.application:zope_publisher_app_factory
 
[pipeline:Paste.Main]
pipeline = fileupload main
 
[filter:fileupload]
paste.filter_factory = FileUploadMedia:make_demo
#use = egg:gp.fileupload
tempdir = %(here)s/data/fileupload

It's the first time that I got stuck with Python and Grok for a long time.
Can you check my code and tell me if you see something wrong, please?

I'm trying to get a bar progress working when a file is uploading to the
server. 

from gp.fileupload import make_app
import os

class FileUploadMedia(object):

        def __init__(self, application):
                self.application = application

        def __call__(self, environ, start_response):
                if environ['REQUEST_METHOD'] == 'POST':
                        if 'gp.fileupload.id' in environ['QUERY_STRING']:
                                # need to consume so see how many block we
have to read
                                bsize = 1024
                                length = int(environ['CONTENT_LENGTH'])
                                print 'upload', length
                                blocks = [bsize for i in range(bsize,
length, bsize)]
                                blocks.append(length-len(blocks)*bsize)

                                # read input an write to /dev/null :)
                                rfile = environ['form.input']
                                [rfile.read(size) for size in blocks]
                        # StaticURLParser only deserve GET
                        environ['REQUEST_METHOD'] = 'GET'

                return self.application(environ, start_response)

def make_demo(app, global_conf, **local_conf):
        upload_app = make_app(FileUploadMedia(app), {},
                         max_size= None,
                         tempdir=os.path.join('/tmp', 'fileupload'))

        def application(environ, start_response):
           if '/gp.fileupload/demo.html' in environ['PATH_INFO']:
                  return upload_app(environ, start_response)
           elif '/gp.fileupload.' in environ['PATH_INFO']:
                  return upload_app(environ, start_response)
           else:
                  return app(environ, start_response)
        return application

Thanks in advance!
-- 
View this message in context: http://old.nabble.com/Middleware-and-Grok-tp28378758p28467465.html
Sent from the Grok mailing list archive at Nabble.com.



More information about the Grok-dev mailing list