import struct


def get_property_value(property_reply):
    """
    A function that takes a property reply object, and turns its value into
    something nice for us.

    In particular, if the format of the reply is '8', then assume that it
    is a string. Moreover, it could be a list of strings that are null
    terminated. In this case, return a list of Python strings. Otherwise, just
    convert it to a string and remove the null terminator if it exists.

    Otherwise, the format must be a list of integers that has to be unpacked.
    Sometimes, these integers are ATOM identifiers, so it is useful to map
    'get_atom_name' over this list if that's the case.

    :param property_reply: An object returned by a cookie's "reply" method.
    :type property_reply: xcb.xproto.GetPropertyReply
    :return: Either a string, a list of strings or a list of integers depending
             upon the format of the property reply.
    """
    if property_reply.format == 8:
        ret = bytes(property_reply.value.buf()).split(b'\0')
        if ret[-1] == '': ret.pop()
        ret = [ x.decode('utf-8') for x in ret ]
        return ret[0] if len(ret) == 1 else ret
    elif property_reply.format in (16, 32):
        return list(struct.unpack('I' * property_reply.value_len,
                                  property_reply.value.buf()))

    return None