Addon Signals (script.module.addon.signals) not python3 compatible
#1
Addon Signals does not work using Python 3. I opened an issue on git with a patch, but it has been a long time since someone has worked on this tiny project. Below are the patched changes that I made to AddonSignals.py. They are not Python 2 compatible, but could easily be made so. The most difficult part was trying to figure out what was allowed by NotifyAll. Hopefully these changes produce identical encoded data, but I have not tested that since it does not seem to be a requirement.


def _decodeData(json_data):
    # Original Python 2.x code:
    #
    # data = json.loads(data)
    # if data:
    #    return json.loads(binascii.unhexlify(data[0]))
    #
    # See _encodeData, below, for how json_data is created.
    #
    # Get hex-string JSON encoded data value wrapped inside of JSON array.

    data = json.loads(json_data)
    if data:
        ascii_hex_str = data[0]
        #
        ascii_as_bytes = binascii.unhexlify(ascii_hex_str)
        json_string = ascii_as_bytes.decode('utf-8')
        data_values = json.loads(json_string)
        return data_values


def _encodeData(data):
    # Perform a json.dumps on the data, producing a string. Next,
    # get the hex-bytes for this string and return string representation of
    # representation of these bytes so that they can be safely passed as a
    # simple string value in an outer JSON (or other) wrapper without
    # extra escaping, etc.

    # Had to be recoded for Python 3 because of
    #   1) Strings supporting Unicode
    #   2) binascii returning bytes instead of str
    #
    # Original Python 2.x code:
    #
    # return '\\"[\\"{0}\\"]\\"'.format(binascii.hexlify(json.dumps(data)))

    json_data = json.dumps(data)
    encoded = json_data.encode('utf-8')
    ascii_as_bytes = binascii.a2b_qp(encoded)
    ascii_hex_bytes = binascii.hexlify(ascii_as_bytes)
    # Identical ascii representation, but as string and not bytes
    ascii_hex_str = ascii_hex_bytes.decode('ascii')
    # Wrap in brackets: '["<value>"]' to pass as JSON argument to NotifyAll
    formatted = '\\"[\\"{0}\\"]\\"'.format(ascii_hex_str)
    return formatted
Reply

Logout Mark Read Team Forum Stats Members Help
Addon Signals (script.module.addon.signals) not python3 compatible0