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 save_suppliers(path=None, customer_idurl=None): """ Write current suppliers list on the disk, ``path`` is a file path to save. """ if not customer_idurl: customer_idurl = my_id.getLocalID() customer_idurl = strng.to_bin(customer_idurl.strip()) customer_id = global_id.UrlToGlobalID(customer_idurl) if path is None: path = os.path.join( settings.SuppliersDir(), customer_id, 'supplierids', ) lst = suppliers(customer_idurl=customer_idurl) lst = list(map(strng.to_text, lst)) if not os.path.exists(os.path.dirname(path)): bpio._dirs_make(os.path.dirname(path)) bpio._write_list(path, lst) if _Debug: lg.out( _DebugLevel, 'contactsdb.save_suppliers for customer [%s]:\n%r' % ( customer_id, lst, )) return True
def save_suppliers(path=None, customer_idurl=None): """ Write current suppliers list on the disk, ``path`` is a file path to save. """ if not customer_idurl: customer_idurl = my_id.getLocalID() if path is None: path = os.path.join( settings.SuppliersDir(), global_id.UrlToGlobalID(customer_idurl), 'supplierids', ) bpio._write_list(path, suppliers(customer_idurl=customer_idurl)) 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