def for_user(self, domain, username, bucket): dir = os.path.join(self.dir, urllib.quote(domain, ''), urllib.quote(username, ''), urllib.quote(bucket, '')) if not os.path.exists(dir): os.makedirs(dir) col_filename = os.path.join(dir, 'collection_id.txt') if not os.path.exists(col_filename): collection_id = str(int(self.timer() * 100) % 1000000) with open(col_filename, 'wb') as fp: fp.write(collection_id) else: with open(col_filename, 'rb') as fp: collection_id = fp.read() db_name = os.path.join(dir, 'database') db = Database(db_name) db.collection_id = collection_id return db
def load_users(dir, user_count, records, size, audience='example.com'): now = int(time.time()) for i in xrange(user_count): user = '******' % i if isinstance(records, tuple): rec = random.randint(*records) else: rec = records fn = os.path.join(dir, urllib.quote(audience, ''), urllib.quote(user, '') + '.db') db = Database(fn) items = [] for count in xrange(rec): if isinstance(size, tuple): s = random.randint(*size) else: s = size items.append('{"id": "item-%s-%s", "data": "%s"}' % (now, count, 'a'*s)) db.extend(items) sys.stdout.write('.') sys.stdout.flush() print 'done.'
def create_db(): db = Database(tmp_filename) db.clear() return db