How to store uScopy data in MondoDB

From KitwarePublic
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

  1. add module path

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

  1. import module with singleton instance

from mongo import m

def application(environ, start_response):

       """ The WSGI application to retrieve images from database """
       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']
       try:
               str2 = environ['PATH_INFO']
       except:
               # Give some default request if 
               # no parameters passed in URL 
               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)
       return [str(res['file'])]

</source>

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



Demo