How to store uScopy data in MondoDB

From KitwarePublic
Revision as of 20:07, 18 January 2011 by Dhanannjay.deo (talk | contribs)
Jump to navigationJump to search

Server side code

Python code <source lang="python"> """

Decodes the path info and fetches the corresponding tile from pymongo

""" import pymongo import sys

sys.path.append('/var/www-dj/')

from mongo import m

def application(environ, start_response):

       """ The WSGI test application """
       def error(error_msg):
               status = "200 OK"
               response_headers = [('Content-type', 'text/plain'),
                           ('Content-Length', str(len(error_msg)))]
               start_response(status, response_headers)
               return [error_msg]
       # emit status / headers
       db = m.conn['clay1']
       #print 'starting request ..'
       try:
               str2 = environ['PATH_INFO']
       except:
               str2 = '/tiles3.py/n1/t.jpg'
       path = str2.split('/')
       if len(path) != 3:
               #incorrect syntax
               return error(str(path))
       col = db[path[1]]
       res = col.find_one({"name" : path[2]})
       m.conn.end_request()
       if res == None:
               return error('Item not found in database ..')
       # Create the response headers
       status = "200 OK"
       response_headers = [('Content-type', 'image/jpeg'),
                           ('Content-Length', str(len(res['file'])))]
       start_response(status, response_headers)
       #print 'Started response'
       return [str(res['file'])]
       #except:

if __name__ == '__main__':

   # this runs when script is started directly from commandline
   try:
       # create a simple WSGI server and run the application
       from wsgiref import simple_server
       print "Running test application - point your browser at http://localhost:8000/ ..."
       httpd = simple_server.WSGIServer((, 8000), simple_server.WSGIRequestHandler)
       httpd.set_app(application)
       httpd.serve_forever()
   except ImportError:
       # wsgiref not installed, just output html to stdout
                                                                                                                                                                                            1,1           Top

</source>

Mongo module manages connection pool internally. Another simple script is used to recreate indexes after data has been added.



Demo