def load_suppliers(path=None, customer_idurl=None, all_customers=False): """ Load suppliers list from disk. """ if all_customers: for customer_id in os.listdir(settings.SuppliersDir()): if not global_id.IsValidGlobalUser(customer_id): lg.warn('invalid customer record %s found in %s' % (customer_id, settings.SuppliersDir())) continue path = os.path.join(settings.SuppliersDir(), customer_id, 'supplierids') lst = bpio._read_list(path) if lst is None: lst = list() set_suppliers( lst, customer_idurl=global_id.GlobalUserToIDURL(customer_id)) lg.out( 4, 'contactsdb.load_suppliers %d items from %s' % (len(lst), path)) return True if path is None: if customer_idurl is None: path = settings.SupplierIDsFilename() else: path = os.path.join(settings.SuppliersDir(), global_id.UrlToGlobalID(customer_idurl), 'supplierids') lst = bpio._read_list(path) if lst is None: lst = list() set_suppliers(lst) lg.out(4, 'contactsdb.load_suppliers %d items from %s' % (len(lst), path)) return True
def cache_suppliers(path=None): """ Make sure identities of all suppliers we know are cached. """ dl = [] list_local_customers = list(os.listdir(settings.SuppliersDir())) for customer_id in list_local_customers: if not global_id.IsValidGlobalUser(customer_id): lg.warn('invalid customer record %s found in %s' % (customer_id, settings.SuppliersDir())) continue try: one_customer_idurl = global_id.GlobalUserToIDURL(customer_id) except Exception as exc: lg.err('idurl caching failed: %r' % exc) continue if not id_url.is_cached(one_customer_idurl): dl.append(identitycache.immediatelyCaching(one_customer_idurl)) path = os.path.join(settings.SuppliersDir(), customer_id, 'supplierids') lst = bpio._read_list(path) if lst is None: lg.warn('did not found suppliers ids at %s' % path) continue for one_supplier_idurl in lst: if one_supplier_idurl: if not id_url.is_cached(one_supplier_idurl): dl.append(identitycache.immediatelyCaching(one_supplier_idurl)) if _Debug: lg.out(_DebugLevel, 'contactsdb.cache_suppliers prepared %d idurls to be cached' % len(dl)) return DeferredList(dl, consumeErrors=True)
def load_suppliers(path=None, customer_idurl=None, all_customers=False): """ Load suppliers list from disk. """ if all_customers: for customer_id in os.listdir(settings.SuppliersDir()): if not global_id.IsValidGlobalUser(customer_id): lg.warn('invalid customer record %s found in %s' % (customer_id, settings.SuppliersDir())) continue path = os.path.join(settings.SuppliersDir(), customer_id, 'supplierids') lst = bpio._read_list(path) if lst is None: lg.warn('did not found suppliers ids at %s' % path) continue lst = list(map(strng.to_bin, lst)) set_suppliers(lst, customer_idurl=global_id.GlobalUserToIDURL(customer_id)) lg.out(4, 'contactsdb.load_suppliers %d items from %s' % (len(lst), path)) return True if not customer_idurl: customer_idurl = my_id.getLocalID() customer_idurl = strng.to_bin(customer_idurl.strip()) if path is None: path = os.path.join(settings.SuppliersDir(), global_id.UrlToGlobalID(customer_idurl), 'supplierids') lst = bpio._read_list(path) if lst is None: lst = list() lst = list(map(strng.to_bin, lst)) set_suppliers(lst) lg.out(4, 'contactsdb.load_suppliers %d items from %s' % (len(lst), path)) return True
def load_suppliers(path=None, customer_idurl=None, all_customers=False): """ Load suppliers list from disk. """ if all_customers: list_local_customers = list(os.listdir(settings.SuppliersDir())) if _Debug: lg.out(_DebugLevel, 'contactsdb.load_suppliers %d known customers' % len(list_local_customers)) for customer_id in list_local_customers: if not global_id.IsValidGlobalUser(customer_id): lg.warn('invalid customer record %s found in %s' % (customer_id, settings.SuppliersDir())) continue path = os.path.join(settings.SuppliersDir(), customer_id, 'supplierids') lst = bpio._read_list(path) if lst is None: lg.warn('did not found suppliers ids at %s' % path) continue one_customer_idurl = global_id.GlobalUserToIDURL(customer_id) if not id_url.is_cached(one_customer_idurl): lg.warn('customer identity %r not cached yet' % one_customer_idurl) continue if not one_customer_idurl.is_latest(): latest_customer_path = os.path.join(settings.SuppliersDir(), one_customer_idurl.to_id()) old_customer_path = os.path.join(settings.SuppliersDir(), customer_id) if not os.path.exists(latest_customer_path): os.rename(old_customer_path, latest_customer_path) lg.info('detected and processed idurl rotate when loading suppliers for customer : %r -> %r' % (customer_id, one_customer_idurl.to_id())) else: bpio._dir_remove(old_customer_path) lg.warn('found old customer dir %r and removed' % old_customer_path) continue lst = list(map(lambda i: i if id_url.is_cached(i) else b'', lst)) set_suppliers(lst, customer_idurl=one_customer_idurl) if _Debug: lg.out(_DebugLevel, ' loaded %d known suppliers for customer %r' % (len(lst), one_customer_idurl)) return True if not customer_idurl: customer_idurl = my_id.getLocalID() customer_idurl = id_url.field(customer_idurl) if path is None: path = os.path.join(settings.SuppliersDir(), global_id.UrlToGlobalID(customer_idurl), 'supplierids') lst = bpio._read_list(path) if lst is None: lst = list() lst = list(map(lambda i: i if id_url.is_cached(i) else b'', lst)) set_suppliers(lst, customer_idurl=customer_idurl) if _Debug: lg.out(_DebugLevel, 'contactsdb.load_suppliers %d items from %s' % (len(lst), path)) return True
def IsGlobalPathCorrect(globPath, customer_id_mandatory=False): """ Validate a given ``globPath``, must have such format: [email protected]:myfiles/flowers/cactus.png """ customerGlobalID, remotePath = SplitPacketID(globPath) if customer_id_mandatory: from userid import global_id return global_id.IsValidGlobalUser(customerGlobalID) if not remotePath: return False parts = remotePath.split('/') if len(parts) > 50: return False # TODO: more strict validation return True
def add_friend(trusted_user_id, alias='', share_person_key=True): idurl = strng.to_text(trusted_user_id) if global_id.IsValidGlobalUser(trusted_user_id): idurl = global_id.GlobalUserToIDURL(trusted_user_id, as_field=False) idurl = id_url.field(idurl) if not idurl: return None ret = Deferred() # if id_url.is_cached(idurl): # _add(idurl, ret) # return ret # # d = identitycache.immediatelyCaching(idurl) # d.addErrback(lambda *args: ret.callback(ERROR('failed caching user identity'))) # d.addCallback(lambda *args: _add(idurl, ret)) # return ret
def on_my_message(self, message): if message.startswith('!add '): idurl = message[5:] if global_id.IsValidGlobalUser(idurl): gid = global_id.ParseGlobalID(idurl) idurl = gid['idurl'] if idurl.strip() and idurl not in self.users: self.users.append(idurl) name = nameurl.GetName(idurl) self.history.append({ 'text': 'user "%s" was added to the channel' % name, 'name': '', 'time': time.time(), }) return if message.startswith('!find ') or message.startswith('!search '): _, _, inp = message.partition(' ') if not self.search_user_func: self.history.append({ 'text': 'search failed, method not defined', 'name': '', 'time': time.time(), }) return self.search_user_func(inp).addBoth(self.on_nickname_search_result) self.history.append({ 'text': 'looking for "%s" ...' % inp, 'name': '', 'time': time.time(), }) return self.history.append({ 'text': message, 'name': 'you', 'time': time.time(), }) if self.send_message_func is not None: for to in self.users: reactor.callFromThread(self.send_message_func, to, message)