magpie.api.management.resource.resource_utils

Attributes

SERVICE_TYPE_DICT

Classes

Permission

Applicable Permission values (names) under certain Service and Resource.

Functions

format_resource(→ magpie.typedefs.JSON)

Formats a Resource information into JSON.

sync_services_phoenix(→ bool)

Syncs Magpie services by pushing updates to Phoenix.

service_factory(→ ServiceInterface)

Retrieve the specific service class from the provided database service entry.

check_valid_service_or_resource_permission(...)

Checks if a permission is valid to be applied to a specific service or a resource under a root service.

check_valid_service_resource(→ magpie.models.Service)

Checks if a new Resource can be contained under a parent Resource given the requested type and the corresponding

check_unique_child_resource_name(→ None)

Verify that resource will be unique amongst other resources at the same target position.

crop_tree_with_permission(...)

Recursively prunes all children resources from the tree hierarchy except listed ones matched by ID.

get_resource_path(→ magpie.typedefs.Str)

Obtains the full path representation of the specified resource ID from the root service it resides under using all

get_service_or_resource_types(...)

Obtain the service or resource class and a corresponding "service" or "resource" type identifier.

get_resource_parents(...)

Obtains the parent resource nodes of the input service or resource.

get_resource_children(...)

Obtains the children resource node structure of the input service or resource.

get_resource_permissions(...)

Obtains the applicable permissions on the service or resource, accordingly to what was provided.

get_resource_root_service(...)

Retrieves the service-specialized resource corresponding to the top-level resource in the tree hierarchy.

get_resource_root_service_by_id(...)

Retrieves the service-specialized resource corresponding to the top-level resource in the tree hierarchy.

get_resource_root_service_impl(...)

Obtain the root service implementation.

create_resource(→ pyramid.httpexceptions.HTTPException)

delete_resource(request)

Module Contents

magpie.api.management.resource.resource_utils.format_resource(resource: magpie.models.Resource, permissions: Collection[magpie.typedefs.AnyPermissionType] | None = None, permission_type: magpie.permissions.PermissionType | None = None, basic_info: bool = False, dotted: bool = False) magpie.typedefs.JSON[source]

Formats a Resource information into JSON.

Parameters:
  • resourceResource to be formatted.

  • permissions – Permissions to list along with the resource. By default, these are the applicable permissions for that corresponding resource type.

  • permission_type – Override indication of provenance to apply to permissions. Only applicable when they are provided.

  • basic_info – If True, return only sufficient details to identify the resource, without any additional permissions detail, nor hierarchical resource information is returned.

  • dotted – Employ a dot (.) instead of underscore (_) to separate Resource from its basic information.

class magpie.api.management.resource.resource_utils.Permission[source]

Bases: magpie.utils.ExtendedEnum

Applicable Permission values (names) under certain Service and Resource.

READ = 'read'
WRITE = 'write'
ACCESS = 'access'
BROWSE = 'browse'
GET_CAPABILITIES = 'getcapabilities'
GET_MAP = 'getmap'
GET_FEATURE_INFO = 'getfeatureinfo'
GET_LEGEND_GRAPHIC = 'getlegendgraphic'
GET_METADATA = 'getmetadata'
GET_PROPERTY_VALUE = 'getpropertyvalue'
GET_FEATURE = 'getfeature'
GET_FEATURE_WITH_LOCK = 'getfeaturewithlock'
GET_GML_OBJECT = 'getgmlobject'
DESCRIBE_FEATURE_TYPE = 'describefeaturetype'
DESCRIBE_LAYER = 'describelayer'
DESCRIBE_PROCESS = 'describeprocess'
EXECUTE = 'execute'
LOCK_FEATURE = 'lockfeature'
TRANSACTION = 'transaction'
CREATE_STORED_QUERY = 'createstoredquery'
DROP_STORED_QUERY = 'dropstoredquery'
LIST_STORED_QUERIES = 'liststoredqueries'
DESCRIBE_STORED_QUERIES = 'describestoredqueries'
magpie.api.management.resource.resource_utils.sync_services_phoenix(services: Iterable[magpie.models.Service] | magpie.typedefs.JSON, services_as_dicts: bool = False) bool[source]

Syncs Magpie services by pushing updates to Phoenix.

Services must be one of types specified in magpie.register.SERVICES_PHOENIX_ALLOWED.

Parameters:
  • services

    An iterable of models.Service by default, or a dictionary of {svc-name: {<service-info>}} JSON objects containing each service’s information if services_ad_dicts is True.

    where <service-info> is defined as:

    {"public_url": <url>, "service_name": <name>, "service_type": <type>}
    

  • services_as_dicts – indicate if services must be parsed as JSON definitions.

magpie.api.management.resource.resource_utils.SERVICE_TYPE_DICT[source]
magpie.api.management.resource.resource_utils.service_factory(service: magpie.models.Service, request: pyramid.request.Request) ServiceInterface[source]

Retrieve the specific service class from the provided database service entry.

magpie.api.management.resource.resource_utils.check_valid_service_or_resource_permission(permission_name: magpie.typedefs.Str | magpie.permissions.Permission, service_or_resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session) magpie.permissions.Permission | None[source]

Checks if a permission is valid to be applied to a specific service or a resource under a root service.

Parameters:
  • permission_name – permission name to be validated

  • service_or_resource – resource item corresponding to either a Service or a Resource

  • db_session – db connection

Returns:

valid Permission if allowed by the service/resource

Raises:

HTTPBadRequest – if the permission is not valid for the targeted service/resource

magpie.api.management.resource.resource_utils.check_valid_service_resource(parent_resource: magpie.typedefs.ServiceOrResourceType, resource_type: magpie.typedefs.Str, db_session: sqlalchemy.orm.session.Session) magpie.models.Service[source]

Checks if a new Resource can be contained under a parent Resource given the requested type and the corresponding Service under which the parent Resource is already assigned.

Parameters:
  • parent_resource – Resource under which the new resource of resource_type must be placed

  • resource_type – desired resource type

  • db_session

Returns:

root Service if all checks were successful

magpie.api.management.resource.resource_utils.check_unique_child_resource_name(resource_name: magpie.typedefs.Str, parent_id: int, error_message: magpie.typedefs.Str, db_session: sqlalchemy.orm.session.Session) None[source]

Verify that resource will be unique amongst other resources at the same target position.

Verifies that the provided resource_name does not already exist amongst other children resources at the level immediately under the parent, for the specified parent resource.

Returns:

nothing if no conflict detected

Raises:

HTTPConflict – if the resource_name conflict with another existing resource

magpie.api.management.resource.resource_utils.crop_tree_with_permission(children: magpie.typedefs.NestedResourceNodes, resource_id_list: List[int]) Tuple[magpie.typedefs.NestedResourceNodes, List[int]][source]

Recursively prunes all children resources from the tree hierarchy except listed ones matched by ID.

Input children is expected to be a dictionary of resource nodes and children resources with their ID as keys:

{
    <res-id>: {
        "node": <res>,
        "children": {
            <res-id>: {
                "node": <res>,
                "children": { <...> }
            },
            <...>
    },
    <...>
}
Parameters:
  • children – full hierarchy of children resource nodes.

  • resource_id_list – resource IDs of nodes to preserve.

Returns:

pruned hierarchy of resource nodes.

magpie.api.management.resource.resource_utils.get_resource_path(resource_id: int, db_session: sqlalchemy.orm.session.Session) magpie.typedefs.Str[source]

Obtains the full path representation of the specified resource ID from the root service it resides under using all respective names of the intermediate resources.

For example, the following hierarchy:

<service-1> (id: 1)
    <resource-1> (id: 2)
        <resource-2> (id: 3)

Will return the following path: /service-1/resource-1/resource-2.

This is the same representation of the resource field within startup permissions configuration file.

magpie.api.management.resource.resource_utils.get_service_or_resource_types(service_or_resource: magpie.typedefs.ServiceOrResourceType) Tuple[Type[magpie.services.ServiceInterface], magpie.typedefs.Str][source]

Obtain the service or resource class and a corresponding "service" or "resource" type identifier.

magpie.api.management.resource.resource_utils.get_resource_parents(resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session, tree_service_builder: ziggurat_foundations.models.services.resource_tree.ResourceTreeService | None = None) List[magpie.typedefs.ServiceOrResourceType][source]

Obtains the parent resource nodes of the input service or resource.

Parameters:
  • resource – Initial resource where to start building the list from.

  • db_session – Database connection to retrieve resources.

  • tree_service_builder – Utility that build the tree (default: models.RESOURCE_TREE_SERVICE).

Returns:

List of resources starting at input resource going all the way down to the root service.

magpie.api.management.resource.resource_utils.get_resource_children(resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session, tree_service_builder: ziggurat_foundations.models.services.resource_tree.ResourceTreeService | None = None, limit_depth: int | None = None) magpie.typedefs.NestedResourceNodes[source]

Obtains the children resource node structure of the input service or resource.

Parameters:
  • resource – Initial resource where to start building the tree from.

  • db_session – Database connection to retrieve resources.

  • tree_service_builder – Utility that build the tree (default: models.RESOURCE_TREE_SERVICE).

  • limit_depth – Maximum depth to look for children resources (very deep if not specified, could be slow).

Returns:

{node: Resource, children: {node_id: <recursive>}}

magpie.api.management.resource.resource_utils.get_resource_permissions(resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session) List[magpie.permissions.Permission][source]

Obtains the applicable permissions on the service or resource, accordingly to what was provided.

When parsing a resource, rewinds the hierarchy up to the top-most service in order to find the context under which the resource resides, and therefore which permissions this resource is allowed to have under that service.

magpie.api.management.resource.resource_utils.get_resource_root_service(resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session) magpie.models.Service | None[source]

Retrieves the service-specialized resource corresponding to the top-level resource in the tree hierarchy.

See also

magpie.api.management.resource.resource_utils.get_resource_root_service_by_id(resource_id: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session) magpie.models.Service | None[source]

Retrieves the service-specialized resource corresponding to the top-level resource in the tree hierarchy.

See also

magpie.api.management.resource.resource_utils.get_resource_root_service_impl(resource: magpie.typedefs.ServiceOrResourceType, request: pyramid.request.Request) magpie.services.ServiceInterface[source]

Obtain the root service implementation.

Retrieves the root-resource from the provided resource within a tree hierarchy and generates the corresponding top-level service’s implementation from the service_factory().

See also

get_resource_root_service() to retrieve only the service flavored resource model

magpie.api.management.resource.resource_utils.create_resource(resource_name: magpie.typedefs.Str, resource_display_name: magpie.typedefs.Str | None, resource_type: magpie.typedefs.Str, parent_id: int, db_session: sqlalchemy.orm.session.Session) pyramid.httpexceptions.HTTPException[source]
magpie.api.management.resource.resource_utils.delete_resource(request)[source]