def save_customers(path=None, save_meta_info=False): """ Write current customers list on the disk, ``path`` is a file path to save. """ global _CustomersMetaInfo if path is None: path = settings.CustomerIDsFilename() lst = customers() lst = list(map(strng.to_text, lst)) bpio._write_list(path, lst) if save_meta_info: json_info = id_url.to_bin_dict(_CustomersMetaInfo) local_fs.WriteTextFile( settings.CustomersMetaInfoFilename(), jsn.dumps( json_info, indent=2, sort_keys=True, keys_to_text=True, )) if _Debug: lg.out( _DebugLevel, 'contactsdb.save_customers save_meta_info=%r : %r' % ( save_meta_info, lst, ))
def add_customer_meta_info(customer_idurl, info): """ """ global _CustomersMetaInfo customer_idurl = id_url.field(customer_idurl) if not customer_idurl.is_latest(): if customer_idurl.original() in _CustomersMetaInfo: if customer_idurl.to_bin() not in _CustomersMetaInfo: _CustomersMetaInfo[ customer_idurl.to_bin()] = _CustomersMetaInfo.pop( customer_idurl.original()) lg.info( 'detected and processed idurl rotate for customer meta info : %r -> %r' % (customer_idurl.original(), customer_idurl.to_bin())) customer_idurl = id_url.to_bin(customer_idurl) if 'family_snapshot' in info: info['family_snapshot'] = id_url.to_bin_list(info['family_snapshot']) if 'ecc_map' in info: info['ecc_map'] = strng.to_text(info['ecc_map']) if customer_idurl not in _CustomersMetaInfo: if _Debug: lg.out( _DebugLevel, 'contactsdb.add_customer_meta_info store new meta info for customer %r: %r' % ( customer_idurl, info, )) _CustomersMetaInfo[customer_idurl] = {} else: if _Debug: lg.out( _DebugLevel, 'contactsdb.add_customer_meta_info update existing meta info for customer %r: %r' % ( customer_idurl, info, )) _CustomersMetaInfo[customer_idurl].update(info) json_info = { k: jsn.dict_keys_to_text(v) for k, v in id_url.to_bin_dict(_CustomersMetaInfo).items() } try: raw_data = jsn.dumps( json_info, indent=2, sort_keys=True, keys_to_text=True, values_to_text=True, ) except: lg.exc() return None local_fs.WriteTextFile(settings.CustomersMetaInfoFilename(), raw_data) return _CustomersMetaInfo
def write_customers_meta_info_all(new_customers_info): """ """ global _CustomersMetaInfo _CustomersMetaInfo = new_customers_info json_info = {k: jsn.dict_keys_to_text(v) for k, v in id_url.to_bin_dict(_CustomersMetaInfo).items()} try: raw_data = jsn.dumps( json_info, indent=2, sort_keys=True, keys_to_text=True, values_to_text=True, ) except: lg.exc() return None local_fs.WriteTextFile(settings.CustomersMetaInfoFilename(), raw_data) return _CustomersMetaInfo
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))
def remove_customer_meta_info(customer_idurl): """ """ global _CustomersMetaInfo customer_idurl = id_url.field(customer_idurl) if not customer_idurl.is_latest(): if customer_idurl.original() in _CustomersMetaInfo: if customer_idurl.to_bin() not in _CustomersMetaInfo: _CustomersMetaInfo[ customer_idurl.to_bin()] = _CustomersMetaInfo.pop( customer_idurl.original()) lg.info( 'detected and processed idurl rotate for customer meta info : %r -> %r' % (customer_idurl.original(), customer_idurl.to_bin())) customer_idurl = id_url.to_bin(customer_idurl) if customer_idurl not in _CustomersMetaInfo: lg.warn('meta info for customer %r not exist' % customer_idurl) return False if _Debug: lg.out( _DebugLevel, 'contactsdb.remove_customer_meta_info erase existing meta info for customer %r' % customer_idurl) _CustomersMetaInfo.pop(customer_idurl) json_info = { k: jsn.dict_keys_to_text(v) for k, v in id_url.to_bin_dict(_CustomersMetaInfo).items() } local_fs.WriteTextFile( settings.CustomersMetaInfoFilename(), jsn.dumps( json_info, indent=2, sort_keys=True, keys_to_text=True, values_to_text=True, )) return True