Exemplo n.º 1
0
def generate_sample_data(n, host, port):
    import string, random
    docs = [ { 'label' : ''.join(random.sample(string.lowercase, random.randint(2,9))),
               'value': i * 1.1 + 0.01,
               '_id': mongo_util.newId() }
             for i in range(n) ]
    for doc in docs:
        doc['length'] = len(doc['label'])
        doc['letters'] = sorted(list(doc['label']))
        
    connection = pymongo.Connection(host, port)
    db = connection.test
    db.drop_collection('posts')
    db.posts.insert(docs)
    return n, 'test', 'posts'
Exemplo n.º 2
0
 def get(self, flag):
     if flag == 'reset':
         db = self.mongo_conn['test']
         db.drop_collection('test')
         collection = db['test']
         
         for value,word in enumerate(['foo', 'bar', 'fee', 'baa', 'baa', 'bar']):
             collection.insert({
                 'word': word, 
                 'value': value, 
                 '_id': mongo_util.newId(),
                 access.OwnerKey: self.getUserId() }, safe=True)
                 
         collection.insert({
             'word': 'another',
             'value': 42,
             '_id': mongo_util.newId(),
             access.OwnerKey: 'some.one@else' }, safe=True)
             
         self.write('ok')
         
     elif re.match(r'\d+', flag):
         code = int(flag)
         raise HTTPError(code)
Exemplo n.º 3
0
    def _imageWorker(self, collection, info):
        acceptable_extensions = set(['.jpg', '.gif', '.jpeg', '.bmp', '.png', '.tif', '.tiff',
                                    '.wbmp', '.jng', '.svg'])
        root, ext = os.path.splitext(info.fname)
        toRemove = [ info.fpath ]
        try:
            if ext not in acceptable_extensions:
                raise TypeError('unknown extension')
            id = mongo_util.newId()
            img = Image.open(file(info.fpath, 'rb'))
            w, h = img.size
            opath = os.path.join(MediaRoot, 'Image', id[-2:], id + ext)
            upath = os.path.join(MediaURL, 'Image', id + ext)
            dirname = os.path.dirname(opath)
            if not os.path.exists(dirname):
                os.makedirs(dirname)
            shutil.move(info.fpath, opath)
        except Exception:
            return (False, traceback.format_exc(), None)
        finally:
            for f in toRemove:
                try:
                    os.remove(f)
                except:
                    pass

        item = {}
        item['width'] = w
        item['height'] = h
        item['originalName'] = sanitize(info.fname)
        item['URL'] = sanitize(upath)
        item['tags'] = [ sanitize(tag) for tag in info.tags ]
        item['title'] = sanitize(info.title)
        item['description'] = sanitize(info.description)
        item['creditURL'] = sanitize(info.creditURL)
        user = self.get_current_user()
        item['uploadedBy'] = user['email']
        item['uploadedOn'] = datetime.now().isoformat()
        item['_id'] = id

        collection.insert(item, safe=True)

        return (True, item, 'Image')
Exemplo n.º 4
0
            s = s.replace('"$ref":', '"_ref":') # hide $ref
            item = json.loads(s, object_hook=pymongo.json_util.object_hook)
        except ValueError, e:
            raise HTTPError(400, unicode(e));

        # validate the schema
        self.validateSchema(db_name, collection_name, item)
        
        # sanitize
        try:
            sanitize(item)
        except ValueError:
            raise HTTPError(400, 'HTML field parse failed')
        
        # add meta items outside schema
        id = mongo_util.newId()
        item['_id'] = id
        item[access.OwnerKey] = self.getUserId()

        collection.insert(item, safe=True)
        # this path should get encoded only one place, fix this
        self.set_header('Location', '/data/%s-%s/%s/%s' % (mode, db_name, collection_name, id))
        s = json.dumps(item, default=pymongo.json_util.default)
        s = s.replace('"_ref":', '"$ref":') # restore $ref
        self.set_header('Content-Length', len(s))
        self.set_header('Content-Type', 'text/javascript')
        self.write(s)

# handle requests with an id
class ItemHandler(access.BaseHandler):
    def get(self, mode, db_name, collection_name, id):
Exemplo n.º 5
0
    def _audioWorker(self, collection, info):
        root, ext = os.path.splitext(info.fname)
        toRemove = [ info.fpath ]
        if not ext:
            return (False, 'bad extension', None)

        id = mongo_util.newId()
        try:
            opath = os.path.join(MediaRoot, 'Audio', id[-2:], id)
            upath = os.path.join(MediaURL, 'Audio', id)

            dirname = os.path.dirname(opath)
            if not os.path.exists(dirname):
                os.makedirs(dirname)
            if info.ftype == 'audio/midi':
                # check the magic number to try to assure it is midi since timidity doesn't seem to error
                ms = magic.open(magic.MAGIC_NONE)
                ms.load()
                if not re.search(r'\smidi\s', ms.file(info.fpath), re.I):
                    raise TypeError('not midi')
                tmp = info.fpath + '.wav'
                toRemove.append(tmp)
                r1 = subprocess.call([TIMIDITYPATH, '-Ow', '-idqq', info.fpath, '-o', tmp])
                if r1:
                    raise RuntimeError('midi decode failed')
            elif ext != '.wav':
                tmp = info.fpath + '.wav'
                r1 = subprocess.call([SOXPATH, '-t', ext, info.fpath, tmp])
                toRemove.append(tmp)
                if r1:
                    raise RuntimeError('wav decode failed')
            else:
                tmp = info.fpath
            # at this point we should have a wav file in tmp
            r1 = subprocess.call([SOXPATH, '-t', '.wav', tmp, opath + '.mp3', 'rate', '22050', 'norm'])
            r2 = subprocess.call([SOXPATH, '-t', '.wav', tmp, opath + '.ogg', 'rate', '22050', 'norm'])
            if r1 or r2:
                raise RuntimeError('encoding failed')
                
            # get the duration
            mf = mad.MadFile(opath + '.mp3')
            duration = mf.total_time() / 1000.0
            
        except Exception:
            return (False, traceback.format_exc(), None)
        finally:
            for f in toRemove:
                try:
                    os.remove(f)
                except:
                    pass
        item = {}
        item['originalName'] = sanitize(info.fname)
        item['URL'] = upath
        item['tags'] = [ sanitize(tag) for tag in info.tags ]
        item['title'] = sanitize(info.title)
        item['description'] = sanitize(info.description)
        item['creditURL'] = sanitize(info.creditURL)
        user = self.get_current_user()
        item['uploadedBy'] = user['email']
        item['uploadedOn'] = datetime.now().isoformat()
        item['duration'] = duration
        item['_id'] = id

        collection.insert(item, safe=True)
        return (True, item, 'Audio')