def BytesToDict(inp,
                encoding='latin1',
                errors='strict',
                keys_to_text=False,
                values_to_text=False,
                unpack_types=False):
    """
    A smart way to extract input bytes into python dictionary object.
    All input bytes will be decoded into text and then loaded via `json.loads()` method.
    Finally every text key and value in result dict will be encoded back to bytes if `values_to_text` is False.
    Smart feature `unpack_types` can be used to "extract" real types of keys and values from input bytes.
    Can be used to extract dictionaries of mixed types - binary and text values.   
    """
    if not inp:
        return {}
    _t = strng.to_text(inp, encoding=encoding)
    if values_to_text:
        return jsn.loads_text(_t, encoding=encoding)
    if unpack_types:
        return jsn.unpack_dict(jsn.loads(_t, encoding=encoding),
                               encoding=encoding,
                               errors=errors)
    if keys_to_text:
        return jsn.dict_keys_to_text(jsn.loads(_t, encoding=encoding))
    return jsn.loads(_t, encoding=encoding)
Beispiel #2
0
def update_customers_usage(new_space_usage_dict):
    usage_dict = {
        id_url.field(k).to_bin(): v
        for k, v in new_space_usage_dict.items()
    }
    return bpio._write_dict(settings.CustomersUsedSpaceFile(),
                            jsn.dict_keys_to_text(usage_dict))
Beispiel #3
0
def on_incoming_message(request, info, status, error_message):
    """
    Message came in for us
    """
    global _IncomingMessageCallbacks
    if _Debug:
        lg.out(
            _DebugLevel,
            "message.on_incoming_message new PrivateMessage %r from %s" % (
                request.PacketID,
                request.OwnerID,
            ))
    private_message_object = PrivateMessage.deserialize(request.Payload)
    if private_message_object is None:
        lg.err(
            "PrivateMessage deserialize failed, can not extract message from request payload of %d bytes"
            % len(request.Payload))
        return False
    try:
        decrypted_message = private_message_object.decrypt()
        json_message = serialization.BytesToDict(
            decrypted_message,
            unpack_types=True,
            encoding='utf-8',
        )
        json_message = jsn.dict_keys_to_text(
            jsn.dict_values_to_text(json_message))
    except:
        lg.exc()
        return False
    if request.PacketID in received_messages_ids():
        lg.warn("skip incoming message %s because found in recent history" %
                request.PacketID)
        return False
    received_messages_ids().append(request.PacketID)
    if len(received_messages_ids()) > 100:
        received_messages_ids(True)
    handled = False
    try:
        for cb in _IncomingMessageCallbacks:
            handled = cb(request, private_message_object, json_message)
            if _Debug:
                lg.args(_DebugLevel,
                        cb=cb,
                        packet_id=request.PacketID,
                        handled=handled)
            if handled:
                break
    except:
        lg.exc()
    if _Debug:
        lg.args(_DebugLevel, msg=json_message, handled=handled)
    if handled:
        return True
    if config.conf().getBool(
            'services/private-messages/acknowledge-unread-messages-enabled'):
        p2p_service.SendAckNoRequest(request.OwnerID,
                                     request.PacketID,
                                     response='unread')
    return True
Beispiel #4
0
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
Beispiel #5
0
def get_supplier_meta_info(supplier_idurl, customer_idurl=None):
    """
    """
    global _SuppliersMetaInfo
    if not customer_idurl:
        customer_idurl = my_id.getLocalID()
    customer_idurl = id_url.field(customer_idurl)
    supplier_idurl = id_url.field(supplier_idurl)
    return jsn.dict_keys_to_text(jsn.dict_values_to_text(
        _SuppliersMetaInfo.get(customer_idurl, {}).get(supplier_idurl, {})))
Beispiel #6
0
def get_supplier_meta_info(supplier_idurl, customer_idurl=None):
    """
    """
    global _SuppliersMetaInfo
    if not customer_idurl:
        customer_idurl = my_id.getIDURL()
    if not id_url.is_cached(customer_idurl) or not id_url.is_cached(supplier_idurl):
        return {}
    customer_idurl = id_url.field(customer_idurl)
    supplier_idurl = id_url.field(supplier_idurl)
    return jsn.dict_keys_to_text(jsn.dict_values_to_text(
        _SuppliersMetaInfo.get(customer_idurl, {}).get(supplier_idurl, {})))
Beispiel #7
0
def get_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)
    return jsn.dict_keys_to_text(jsn.dict_values_to_text(_CustomersMetaInfo.get(customer_idurl, {})))
Beispiel #8
0
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
Beispiel #9
0
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