orchestracities/anubis

check if there is a better way to extract resource linked by accessTo

Opened this issue · 0 comments

# TODO deal with this better.

acl = Namespace("http://www.w3.org/ns/auth/acl#")
oc_acl = Namespace("http://voc.orchestracities.io/oc-acl#")


def parse_rdf_graph(data):
    g = Graph()
    g.bind("acl", acl)
    g.bind("oc_acl", oc_acl)
    g.parse(data=data)
    nm = NamespaceManager(g)
    policies = {}
    for subj, pred, obj in g:
        policy = str(subj).split("/")[-1]
        if not policies.get(policy):
            policies[policy] = {}
        if pred == acl.agentClass:
            policies[policy]["agentClass"] = redux(str(nm.normalizeUri(obj)))
        if pred == acl.mode:
            policies[policy]["mode"] = redux(str(nm.normalizeUri(obj)))
        # TODO deal with this better.
        if pred == acl.accessTo:
            policies[policy]["accessTo"] = str(obj).split("/")[-1]
        if pred == acl.default:
            policies[policy]["accessTo"] = "default"
        if pred == acl.accessToClass:
            policies[policy]["accessToClass"] = str(
                nm.normalizeUri(obj)).split(":")[-1]
    return policies


def redux(uri: str):
    if "http://www.w3.org/ns/auth/acl#" in uri:
        return "acl:" + uri[len("http://www.w3.org/ns/auth/acl#") + 1:-1]
    if "http://voc.orchestracities.io/oc-acl#" in uri:
        return "oc-acl:" + \
            uri[len("http://voc.orchestracities.io/oc-acl#") + 1:-1]
    if "http://xmlns.com/foaf/0.1/" in uri:
        return "foaf:" + uri[len("http://xmlns.com/foaf/0.1/") + 1:-1]
    if "<" in uri[0] and ">" in uri[-1]:
        return uri[1:-1]
    return uri


def serialize(
        db: Session,
        fiware_service: [str],