def user_policy_to_dict(doc):
    """Convert a bucket policy to a dict mapping principal/prefix names to 'R' or 'W' """

    import json

    if not isinstance(doc, dict):
        doc = json.loads(doc)

    d = {}

    def get_statement(name):
        for s in doc['Statement']:
            if s['Sid'] == name:
                return s

    for r in get_statement('read')['Principal']['AWS']:
        bucket, prefix = r.replace(arn_prefix, '').replace('/*', '').split('/')
        d[(bucket, prefix.strip('/'))] = 'R'

    try:
        for r in get_statement('write')['Resource']:
            bucket, prefix = r.replace(arn_prefix, '').replace('/*', '').split('/')
            d[(bucket, prefix.strip('/'))] = 'W'
    except TypeError:
        pass  # No write section

    return d