def _get_resource()

in kinto-remote-settings/src/kinto_remote_settings/signer/utils.py [0:0]


def _get_resource(resource):
    # Use the default NameGenerator in Kinto resources to check if the resource
    # URIs seem valid.
    # XXX: if a custom ID generator is specified in settings, this verification would
    # not result as expected.
    name_generator = NameGenerator()

    parts = resource.split("/")
    if len(parts) == 2:
        bucket, collection = parts
    elif len(parts) == 3 and parts[1] == "buckets":
        # /buckets/bid
        _, _, bucket = parts
        collection = None
    elif len(parts) == 5 and parts[1] == "buckets" and parts[3] == "collections":
        # /buckets/bid/collections/cid
        _, _, bucket, _, collection = parts
    else:
        raise ValueError("should be a bucket or collection URI")
    valid_ids = name_generator.match(bucket) and (
        collection is None or name_generator.match(collection)
    )
    if not valid_ids:
        raise ValueError("bucket or collection id is invalid")
    return {"bucket": bucket, "collection": collection}