def migrate(source, destination): """ Very basic for now """ dbs = Database(source) dbt = Database(destination) dbs.open() dbt.create() dbt.close() for curr in os.listdir(os.path.join(dbs.path, '_indexes')): if curr != '00id.py': shutil.copyfile(os.path.join(dbs.path, '_indexes', curr), os.path.join(dbt.path, '_indexes', curr)) dbt.open() for c in dbs.all('id'): del c['_rev'] dbt.insert(c) return True
def main(): db = Database('/tmp/demo_secure') key = 'abcdefgh' id_ind = EncUniqueHashIndex(db.path, 'id') db.set_indexes([id_ind]) db.create() db.id_ind.enc_key = key print db.id_ind.storage for x in xrange(100): db.insert(dict(x=x, data='testing')) db.close() dbr = Database('/tmp/demo_secure') dbr.open() dbr.id_ind.enc_key = key for curr in dbr.all('id', limit=5): print curr
class Cache(object): """ cache for word morphological analysis """ def __init__(self, dp_path=False): """ Create Analex Cache """ DB_PATH = os.path.join(os.path.expanduser('~'), '.qalsadiCache') self.cache = { 'checkedWords': {}, 'FreqWords': { 'noun': {}, 'verb': {}, 'stopword': {} }, } if not dp_path: dp_path = self.DB_PATH else: dp_path = os.path.join(os.path.dirname(dp_path), '.qalsadiCache') #~ sys.stderr.write("Tahahahah\n"+" "+ dp_path +" "+str(type(dp_path))) #~ self.db = Database("/tmp/QC") try: #~ if True: self.db = Database(str(dp_path)) if not self.db.exists(): self.db.create() x_ind = WithAIndex(self.db.path, "a") self.db.add_index(x_ind) else: self.db.open() except: #~ else: print("Can't Open data base") self.db = None def __del__(self): """ Delete instance and clear cache """ self.cache = None if self.db: self.db.close() def is_already_checked(self, word): """ return if ``word`` is already cached""" try: return bool(self.db.get('a', word)) except: return False #~ except: return False; def get_checked(self, word): """ return checked ``word`` form cache""" #~ word = bytes(word, "utf-8") if self.db: xxx = self.db.get('a', word, with_doc=True) yyy = xxx.get('doc', False) if yyy: return yyy.get('d', []) else: return [] else: return [] def add_checked(self, word, data): """ add checked ``word`` form cache""" #~ word = bytes(word, "utf-8") idata = {"a": word, 'd': data} if self.db: self.db.insert(idata) def exists_cache_freq(self, word, wordtype): """ return if word exists in freq cache""" return word in self.cache['FreqWords'] def get_freq(self, originalword, wordtype): """ return ``word`` frequency form cache""" return self.cache['FreqWords'][wordtype].get(originalword, 0) def add_freq(self, original, wordtype, freq): """ add ``original`` frequency ``freq`` to cache""" self.cache['FreqWords'][wordtype][original] = freq