"""
Handles metadata and MongoDB
"""

import config
import logging
import pymongo
import uuid
from ConfigParser import SafeConfigParser

def get_jobs():
    """Fetch approriate database and collection for jobs."""
    connection = pymongo.Connection(host,port)
    db = connection[db]
    jobs = db[collection]
    return jobs

def insert_job(data):
    jobs = get_jobs()
    my_id = str(uuid.uuid4())
    data['_id'] = my_id
    job_id = jobs.insert(data)
    return job_id

def update_job(job_id, field, value):
    logging.info("Updating metadata job %s" % job_id)
    jobs = get_jobs()
    jobs.update({'_id' : job_id},
                {'$set' : {field : value}})
    if jobs.find_one({'_id' : job_id}) is not None:
        logging.info("Job updated: %s:%s:%s" % (job_id, field, value))
    else:
        logging.warning("Job %s not updated!" % job_id)

def list_jobs(user):
    r = []
    jobs = get_jobs()
    for j in jobs.find({'ARASTUSER':user}):
        r.append(j)
    return r
        
parser = SafeConfigParser()
parser.read('arast.conf')    
host = parser.get('meta', 'mongo.host')
port = int(parser.get('meta', 'mongo.port'))
db = parser.get('meta', 'mongo.db')
collection = parser.get('meta', 'mongo.collection')

@@ -207,16 +207,42 @@
 obs.%22%22%22%0A
+    global db, collection%0A
     conn
@@ -285,17 +285,23 @@
 t)%0A    d
-b
+atabase
  = conne
@@ -322,16 +322,23 @@
 jobs = d
+atabase
 b%5Bcollec
@@ -1021,16 +1021,50 @@
 eturn r%0A
+%0Aglobal host, port, db, collection
         

Fix metadata globals