@@ -0,0 +1,2794 @@
+import pymongo%0Aimport bson%0Afrom bson import json_util%0Aimport warnings%0Afrom cStringIO import StringIO%0Afrom pymongo import Connection, uri_parser%0Aimport bson.son as son%0Aimport json%0Aimport logging%0A%0Adef open(url=None, task=None):%0A    #parses a mongodb uri and returns the database%0A    #%22mongodb://localhost/test.in?query='%7B%22key%22: value%7D'%22%0A    uri = url if url else %22mongodb://localhost/test.in%22%0A%0A    #print 'uri: ' + uri%0A    params = uri.split('?', 1)%0A    uri = params%5B0%5D%0A    uri_info = uri_parser.parse_uri(uri)%0A    query = None%0A    #TODO test flow from a query%0A    #parse json to a dict = q_d%0A    # %5E%5E this is where we use json_util.object_hook%0A    #SON()%5B'query'%5D = q_d%5B'query'%5D%0A    #for k,v  in q_d.iteritems:%0A    #   if k not %22query%22:%0A    #      SON%5Bk%5D = v%0A    options = %7B%7D%0A    if len(params) %3E 1:%0A        params = params%5B1%5D%0A        list_of_params = params.split('&', 1)%0A        for p in params:%0A            name, json_obj = params.split('=')%0A            if name == 'query':%0A                query = son.SON(json.loads(json_obj, object_hook=json_util.object_hook))%0A            else:%0A                options%5Bname%5D = json_obj%0A%0A        '''%0A        query = son.SON()%0A        li_q = json.loads(json_query)%0A        for tupl in li_q:%0A            if tupl%5B0%5D == %22$max%22 or tupl%5B0%5D == %22$min%22:%0A                obj_id = bson.objectid.ObjectId(tupl%5B1%5D)%0A                query%5Btupl%5B0%5D%5D = %7Bu'_id' : obj_id%7D%0A            else:%0A                query%5Btupl%5B0%5D%5D = tupl%5B1%5D%0A        '''%0A    if not query:%0A        query = %7B%7D%0A%0A    #go around: connect to the sonnection then choose db by %5B'dbname'%5D%0A    with warnings.catch_warnings():%0A        warnings.simplefilter(%22ignore%22)%0A        connection = Connection(uri)%0A        database_name = uri_info%5B'database'%5D%0A        collection_name = uri_info%5B'collection'%5D%0A        db = connection%5Bdatabase_name%5D%0A        collection = db%5Bcollection_name%5D%0A%0A        cursor =  collection.find(query, None)%0A%0A        wrapper = MongoWrapper(cursor)%0A        return wrapper%0A        #WRAPPED!%0A%0A%0Aclass MongoWrapper(object):%0A    %22%22%22Want to wrap the cursor in an object that%0A    supports the following operations: %22%22%22%0A%0A    def __init__(self, cursor):%0A        self.cursor = cursor%0A        self.offset = 0%0A%0A    def __iter__(self):%0A        #most important method%0A        for rec in self.cursor:%0A            yield rec%0A%0A    def __len__(self):%0A        #may need to do this more dynamically (see lib/disco/comm.py ln 163)%0A        return self.cursor.count()%0A%0A    def close(self):%0A        self.cursor.close()%0A%0A    @property%0A    def read(self, size=-1):%0A        list_of_records = %5B%5D%0A        if size %3E 0:%0A            for i in range(size):%0A                list_of_records.append(self.cursor.__iter__())%0A        return list_of_records%0A%0A%0Adef input_stream(stream, size, url, params):%0A    mon = open(url)%0A    return mon%0A

add scheme