Пример #1
0
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
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)
Пример #5
0
def load_customers(path):
    """
    Load customers list from disk.
    """
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    set_customers(lst)
    lg.out(4, 'contactsdb.load_customers %d items' % len(lst))
Пример #6
0
def load_customers(path):
    """
    Load customers list from disk.
    """
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    set_customers(lst)
    lg.out(4, 'contactsdb.load_customers %d items' % len(lst))
Пример #7
0
def load_customers(path=None):
    """
    Load customers list from disk.
    """
    if path is None:
        path = settings.CustomerIDsFilename()
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    set_customers(lst)
    lg.out(4, 'contactsdb.load_customers %d items' % len(lst))
Пример #8
0
def cache_customers(path=None):
    """
    Make sure identities of all customers we know are cached.
    """
    dl = []
    if path is None:
        path = settings.CustomerIDsFilename()
    lst = bpio._read_list(path) or []
    for one_customer_idurl in lst:
        if one_customer_idurl:
            if not id_url.is_cached(one_customer_idurl):
                dl.append(identitycache.immediatelyCaching(one_customer_idurl))
    if _Debug:
        lg.out(_DebugLevel, 'contactsdb.cache_customers prepared %d idurls to be cached' % len(dl))
    return DeferredList(dl, consumeErrors=True)
Пример #9
0
def load_customers(path=None):
    """
    Load customers list from disk.
    """
    global _CustomersMetaInfo
    if path is None:
        path = settings.CustomerIDsFilename()
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    lst = list(map(strng.to_bin, lst))
    set_customers(lst)
    _CustomersMetaInfo = jsn.loads(
        local_fs.ReadTextFile(settings.CustomersMetaInfoFilename()) or '{}')
    lg.out(4, 'contactsdb.load_customers %d items' % len(lst))
Пример #10
0
def load_correspondents(path):
    """
    Load correspondents list from disk.
    """
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    for i in xrange(len(lst)):
        lst[i] = tuple(lst[i].strip().split(' ', 1))
        if len(lst[i]) < 2:
            lst[i] = (lst[i][0], '')
        if lst[i][1].strip() == '':
            lst[i] = (lst[i][0], nameurl.GetName(lst[i][0]))
    set_correspondents(lst)
    lg.out(4, 'contactsdb.load_correspondents %d items' % len(lst))
Пример #11
0
def load_correspondents(path):
    """
    Load correspondents list from disk.
    """
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    for i in xrange(len(lst)):
        lst[i] = tuple(lst[i].strip().split(' ', 1))
        if len(lst[i]) < 2:
            lst[i] = (lst[i][0], '')
        if lst[i][1].strip() == '':
            lst[i] = (lst[i][0], nameurl.GetName(lst[i][0]))
    set_correspondents(lst)
    lg.out(4, 'contactsdb.load_correspondents %d items' % len(lst))
Пример #12
0
def load_correspondents(path=None):
    """
    Load correspondents list from disk.
    """
    if path is None:
        path = settings.CorrespondentIDsFilename()
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    for i in range(len(lst)):
        lst[i] = tuple(lst[i].strip().split(' ', 1))
        if len(lst[i]) < 2:
            lst[i] = (lst[i][0], '')
        if not lst[i][1].strip():
            lst[i] = (strng.to_bin(lst[i][0]), nameurl.GetName(lst[i][0]))
    set_correspondents(lst)
    lg.out(4, 'contactsdb.load_correspondents %d items' % len(lst))
Пример #13
0
def load_correspondents(path=None):
    """
    Load correspondents list from disk.
    """
    if path is None:
        path = settings.CorrespondentIDsFilename()
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    for i in range(len(lst)):
        lst[i] = tuple(lst[i].strip().split(' ', 1))
        if len(lst[i]) < 2:
            lst[i] = (lst[i][0], '')
        if not lst[i][1].strip():
            lst[i] = (id_url.field(lst[i][0]), nameurl.GetName(lst[i][0]))
    lst = list(filter(lambda i: id_url.is_cached(i[0]), lst))
    set_correspondents(lst)
    if _Debug:
        lg.out(_DebugLevel, 'contactsdb.load_correspondents %d items' % len(lst))
Пример #14
0
def cache_correspondents(path=None):
    """
    Make sure identities of all correspondents we know are cached.
    """
    dl = []
    if path is None:
        path = settings.CorrespondentIDsFilename()
    lst = bpio._read_list(path) or []
    for i in range(len(lst)):
        try:
            one_correspondent_idurl = lst[i].strip().split(' ', 1)[0]
        except:
            lg.exc()
            continue
        if one_correspondent_idurl:
            if not id_url.is_cached(one_correspondent_idurl):
                dl.append(identitycache.immediatelyCaching(one_correspondent_idurl))
    if _Debug:
        lg.out(_DebugLevel, 'contactsdb.cache_correspondents prepared %d idurls to be cached' % len(dl))
    return DeferredList(dl, consumeErrors=True)
Пример #15
0
def load_customers(path=None):
    """
    Load customers list from disk.
    """
    global _CustomersMetaInfo
    if path is None:
        path = settings.CustomerIDsFilename()
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    lst = list(filter(id_url.is_cached, lst))
    set_customers(lst)
    _CustomersMetaInfo = jsn.loads(
        local_fs.ReadTextFile(settings.CustomersMetaInfoFilename()) or '{}',
        keys_to_bin=True,
    )
    _CustomersMetaInfo = id_url.to_bin_dict(_CustomersMetaInfo)
    _CustomersMetaInfo = jsn.dict_values_to_text(_CustomersMetaInfo)
    if _Debug:
        lg.out(_DebugLevel, 'contactsdb.load_customers %d items' % len(lst))