def _import(self, db, data, flags): # clrln() # sys.stdout.write("writing node %s" % self.node) if self.node_type is str: f = bytemaps.StrStorage method = todb.import_string elif self.node_type is list: f = bytemaps.ListStorage method = todb.import_list elif self.node_type is dict: f = bytemaps.DictStorage for key in data: data[key] = self.wrap(key, data[key]) method = todb.import_dict elif self.node_type is Set: f = bytemaps.ListStorage method = todb.import_set else: raise NodeTypeError(self) merge = bytemaps.is_set(flags, f.is_merge) assume_yes = bytemaps.is_set(flags, f.assume_yes) node_exists = self.exists(db) verbose = bytemaps.is_set(flags, f.verbose) if not merge and node_exists: if assume_yes and not verbose: c = True else: c = confirm("%s already exists. It will be DELETED before writing new data." % self.node, assume_yes) if not c: return else: db.delete(self.node) method(db, self.node, data, flags)
def import_dict (db, node, data, flags): """ @param db the redis database connection @param prefix everything up to the specific dictionary being imported (widgets.numero_uno.events) @name The specific name of this particular dictionary (1) -> widgets.numero_uno.events.1 @data the dictionary @flags a bit-masked integral type that will determine certain specifics (merge/replace, etc.) There is no return type for this function. """ testing = b.is_set(flags, b.DictStorage.is_test) if testing: b.set_bit(flags, b.DictStorage.assume_yes) for entry in data: entry_exists = db.hexists(node, entry) if entry_exists and not b.is_set(flags, b.DictStorage.is_merge): __continue = confirm( "%s:%s already exists in your node and will be overwritten." % (node, entry), (b.is_set(flags, b.DictStorage.assume_yes))) if not __continue: continue data[entry] = clean_string(data[entry]) if not testing: db.hset(node, entry, data[entry])
def import_string(db, node, data, flags): data = clean_string(data) testing = b.is_set(flags, b.StrStorage.is_test) merge = b.is_set(flags, b.StrStorage.is_merge) #unimplemented yes = b.is_set(flags, b.StrStorage.assume_yes) entry_exists = db.exists(node) if entry_exists and not merge: __continue = confirm( "%s already exists in the database and will be overwritten." %node, (b.is_set(flags, b.DictStorage.assume_yes))) if not __continue: return if not testing: db.set(node, data)
def import_list(db, node, data, flags): testing = b.is_set(flags, b.ListStorage.is_test) if testing: b.set_bit(flags, b.ListStorage.assume_yes) if isinstance(data[0], (str,int)): new_list = [] merging = b.is_set(flags, b.ListStorage.is_merge) if merging: no_repeat = b.is_set(flags, b.ListStorage.merge_no_repeat) current_list = r.full_list(db, node) new_list.extend(current_list) for entry in data: if entry in new_list and no_repeat: while entry in new_list: new_list.remove(entry) new_list.append(entry) else: new_list.extend(data) for entry in new_list: if not testing: db.rpush(node, entry) elif isinstance(data[0], dict): if not merging: dictset = db.keys(node+".*") for n in dictset: db.delete(n) dictset = db.keys(node+".*") num = len(dictset) for entry in data: nn = "%s.%d" % (node,num) fl =0 if testing: b.set_bit(fl, b.DictStorage.is_test) if merging: set_bit(fl, b.DictStorage.is_merge) if b.is_set(flags, b.ListStorage.assume_yes): set_bit(fl, b.DictStorage.assume_yes) import_dict(db, nn, data[num], fl) num += 1
def import_set(db, node, data, flags): testing = b.is_set(flags, b.ListStorage.is_test) for element in data: db.sadd(node, element)