# Path: tutelary/exceptions.py
# class InvalidPermissionObjectException(TutelaryException):
#     """Exception raised by authentication backend if the object passed to
#     backend methods is not either a ``tutelary.engine.Object`` or a
#     Django model instance with a ``get_permissions_object`` method.
# 
#     """
#     def __init__(self):
#         super().__init__("invalid object passed to django-tutelary " +
#                          "backend method")
# 
# Path: tutelary/engine.py
# class Action(SimpleSeparated):
#     """Actions are represented by period-separated sequences of elements
#     (e.g. ``parcel.edit``, ``admin.assign-role``) with wildcard
#     elements indicated by ``*`` (e.g. ``party.*``).  A list of
#     *registered* actions is maintained to support permissions set
#     queries to find the set of actions that are permitted on a
#     specified object pattern.
# 
#     """
#     separator = '.'
# 
#     registered = set()
# 
#     def register(action):
#         """Action registration is used to support generating lists of
#         permitted actions from a permission set and an object pattern.
#         Only registered actions will be returned by such queries.
# 
#         """
#         if isinstance(action, str):
#             Action.register(Action(action))
#         elif isinstance(action, Action):
#             Action.registered.add(action)
#         else:
#             for a in action:
#                 Action.register(a)
# 
# class Object(EscapeSeparated):
#     """Objects are represented by slash-separated sequences of elements
#     (e.g. ``Cadasta/Batangas/parcel/123``, ``H4H/PaP/party/118``) with
#     wildcard elements indicated by ``*``
#     (e.g. ``Cadasta/*/parcel/*``).  Slashes can be backslash-escaped,
#     as can backslashes (e.g. ``Cadasta/Village-X\/Y/parcel/943``).
# 
#     """
#     separator = '/'


from django.core.exceptions import ObjectDoesNotExist
from .exceptions import InvalidPermissionObjectException
from .engine import Action, Object
        :param perm: The action to test.
        :type perm: ``str``
        :param obj: The object path to test.
        :type obj: ``tutelary.engine.Object``
        :returns: ``bool`` -- is the action permitted?
        """
        try:
            if not self._obj_ok(obj):
                if hasattr(obj, 'get_permissions_object'):
                    obj = obj.get_permissions_object(perm)
                else:
                    raise InvalidPermissionObjectException
            return user.permset_tree.allow(Action(perm), obj)
        except ObjectDoesNotExist:
            return False

    def permitted_actions(self, user, obj=None):
        """Determine list of permitted actions for an object or object
        pattern.

        :param user: The user to test.
        :type user: ``User``
        :param obj: A function mapping from action names to object
                    paths to test.
        :type obj: callable
        :returns: ``list(tutelary.engine.Action)`` -- permitted actions.

        """
        try:
            if not self._obj_ok(obj):

# Path: tutelary/exceptions.py
# class InvalidPermissionObjectException(TutelaryException):
#     """Exception raised by authentication backend if the object passed to
#     backend methods is not either a ``tutelary.engine.Object`` or a
#     Django model instance with a ``get_permissions_object`` method.
# 
#     """
#     def __init__(self):
#         super().__init__("invalid object passed to django-tutelary " +
#                          "backend method")
# 
# Path: tutelary/engine.py
# class Action(SimpleSeparated):
#     """Actions are represented by period-separated sequences of elements
#     (e.g. ``parcel.edit``, ``admin.assign-role``) with wildcard
#     elements indicated by ``*`` (e.g. ``party.*``).  A list of
#     *registered* actions is maintained to support permissions set
#     queries to find the set of actions that are permitted on a
#     specified object pattern.
# 
#     """
#     separator = '.'
# 
#     registered = set()
# 
#     def register(action):
#         """Action registration is used to support generating lists of
#         permitted actions from a permission set and an object pattern.
#         Only registered actions will be returned by such queries.
# 
#         """
#         if isinstance(action, str):
#             Action.register(Action(action))
#         elif isinstance(action, Action):
#             Action.registered.add(action)
#         else:
#             for a in action:
#                 Action.register(a)
# 
# class Object(EscapeSeparated):
#     """Objects are represented by slash-separated sequences of elements
#     (e.g. ``Cadasta/Batangas/parcel/123``, ``H4H/PaP/party/118``) with
#     wildcard elements indicated by ``*``
#     (e.g. ``Cadasta/*/parcel/*``).  Slashes can be backslash-escaped,
#     as can backslashes (e.g. ``Cadasta/Village-X\/Y/parcel/943``).
# 
#     """
#     separator = '/'


# Path: tutelary/backends.py
from django.core.exceptions import ObjectDoesNotExist
from .exceptions import InvalidPermissionObjectException
from .engine import Action, Object

        :param perm: The action to test.
        :type perm: ``str``
        :param obj: The object path to test.
        :type obj: ``tutelary.engine.Object``
        :returns: ``bool`` -- is the action permitted?
        """
        try:
            if not self._obj_ok(obj):
                if hasattr(obj, 'get_permissions_object'):
                    obj = obj.get_permissions_object(perm)
                else:
                    raise InvalidPermissionObjectException
            return user.permset_tree.allow(Action(perm), obj)
        except ObjectDoesNotExist:
            return False

    def permitted_actions(self, user, obj=None):
        """Determine list of permitted actions for an object or object
        pattern.

        :param user: The user to test.
        :type user: ``User``
        :param obj: A function mapping from action names to object
                    paths to test.
        :type obj: callable
        :returns: ``list(tutelary.engine.Action)`` -- permitted actions.

        """
        try:
            if not self._obj_ok(obj):

                raise InvalidPermissionObjectException