Source code for magpie.permissions

from enum import Enum
from typing import TYPE_CHECKING

from six import with_metaclass

from magpie.utils import ExtendedEnumMeta

if TYPE_CHECKING:
    # pylint: disable=W0611,unused-import
    from magpie.typedefs import Iterable, List, Optional, Str, AnyPermissionType  # noqa: F401


[docs]class Permission(with_metaclass(ExtendedEnumMeta, Enum)): """ Applicable :term:`Permission` values under certain :term:`Service` and :term:`Resource`. """ # file/dir permissions
[docs] READ = "read"
[docs] READ_MATCH = "read-match"
[docs] WRITE = "write"
[docs] WRITE_MATCH = "write-match"
[docs] ACCESS = "access"
# WPS permissions
[docs] GET_CAPABILITIES = "getcapabilities"
[docs] GET_MAP = "getmap"
[docs] GET_FEATURE_INFO = "getfeatureinfo"
[docs] GET_LEGEND_GRAPHIC = "getlegendgraphic"
[docs] GET_METADATA = "getmetadata"
[docs] GET_FEATURE = "getfeature"
[docs] DESCRIBE_FEATURE_TYPE = "describefeaturetype"
[docs] DESCRIBE_PROCESS = "describeprocess"
[docs] EXECUTE = "execute"
[docs] LOCK_FEATURE = "lockfeature"
[docs] TRANSACTION = "transaction"
[docs]def convert_permission(permission): # type: (AnyPermissionType) -> Optional[Permission] """ Converts any permission representation to the :class:`Permission` enum. If the permission cannot be matched to one of the enum's value, ``None`` is returned instead. """ perm = Permission.get(permission) if perm is not None: return perm return Permission.get(getattr(permission, "perm_name", None) or permission)
[docs]def format_permissions(permissions): # type: (Iterable[AnyPermissionType]) -> List[Str] """ Obtains the formatted permission representation after validation that it is a member of :class:`Permission` enum. The returned list is sorted alphabetically and cleaned of any duplicate entries. """ perms = [] for p in permissions: p_enum = convert_permission(p) if p_enum: perms.append(p_enum) return list(sorted(set([p.value for p in perms]))) # remove any duplicates entries