import sys
from abc import ABCMeta, abstractmethod
from collections import namedtuple

from six import add_metaclass


class SaveException(Exception):
    def __init__(self, other):
        self.traceback = sys.exc_info()
        super(SaveException, self).__init__(str(other))


class DeleteException(Exception):
    def __init__(self, other):
        self.traceback = sys.exc_info()
        super(DeleteException, self).__init__(str(other))


class Role(namedtuple("Role", ["role_name"])):
    def to_dict(self):
        return {
            "role": self.role_name,
        }


class UserPermission(
    namedtuple(
        "UserPermission",
        [
            "role_name",
            "username",
            "is_robot",
            "avatar",
            "is_org_member",
            "has_org",
        ],
    )
):
    def to_dict(self):
        perm_dict = {
            "role": self.role_name,
            "name": self.username,
            "is_robot": self.is_robot,
            "avatar": self.avatar,
        }
        if self.has_org:
            perm_dict["is_org_member"] = self.is_org_member
        return perm_dict


class RobotPermission(
    namedtuple(
        "RobotPermission",
        [
            "role_name",
            "username",
            "is_robot",
            "is_org_member",
        ],
    )
):
    def to_dict(self, user=None, team=None, org_members=None):
        return {
            "role": self.role_name,
            "name": self.username,
            "is_robot": True,
            "is_org_member": self.is_org_member,
        }


class TeamPermission(
    namedtuple(
        "TeamPermission",
        [
            "role_name",
            "team_name",
            "avatar",
        ],
    )
):
    def to_dict(self):
        return {
            "role": self.role_name,
            "name": self.team_name,
            "avatar": self.avatar,
        }


@add_metaclass(ABCMeta)
class PermissionDataInterface(object):
    """
    Data interface used by permissions API.
    """

    @abstractmethod
    def get_repo_permissions_by_user(self, namespace_name, repository_name):
        """

        Args:
          namespace_name: string
          repository_name: string

        Returns:
          list(UserPermission)
        """

    @abstractmethod
    def get_repo_roles(self, username, namespace_name, repository_name):
        """

        Args:
          username: string
          namespace_name: string
          repository_name: string

        Returns:
          list(Role) or None
        """

    @abstractmethod
    def get_repo_permission_for_user(self, username, namespace_name, repository_name):
        """

        Args:
          username: string
          namespace_name: string
          repository_name: string

        Returns:
          UserPermission
        """

    @abstractmethod
    def set_repo_permission_for_user(self, username, namespace_name, repository_name, role_name):
        """

        Args:
          username: string
          namespace_name: string
          repository_name: string
          role_name: string

        Returns:
          UserPermission

        Raises:
          SaveException
        """

    @abstractmethod
    def delete_repo_permission_for_user(self, username, namespace_name, repository_name):
        """

        Args:
          username: string
          namespace_name: string
          repository_name: string

        Returns:
          void

        Raises:
          DeleteException
        """

    @abstractmethod
    def get_repo_permissions_by_team(self, namespace_name, repository_name):
        """

        Args:
          namespace_name: string
          repository_name: string

        Returns:
          list(TeamPermission)
        """

    @abstractmethod
    def get_repo_role_for_team(self, team_name, namespace_name, repository_name):
        """

        Args:
          team_name: string
          namespace_name: string
          repository_name: string

        Returns:
          Role
        """

    @abstractmethod
    def set_repo_permission_for_team(self, team_name, namespace_name, repository_name, permission):
        """

        Args:
          team_name: string
          namespace_name: string
          repository_name: string
          permission: string

        Returns:
          TeamPermission

        Raises:
          SaveException
        """

    @abstractmethod
    def delete_repo_permission_for_team(self, team_name, namespace_name, repository_name):
        """

        Args:
          team_name: string
          namespace_name: string
          repository_name: string

        Returns:
          TeamPermission

        Raises:
          DeleteException
        """

[((456, 489), 'collections.namedtuple', 'namedtuple', (['"""Role"""', "['role_name']"], {}), "('Role', ['role_name'])\n", (466, 489), False, 'from collections import namedtuple\n'), ((606, 715), 'collections.namedtuple', 'namedtuple', (['"""UserPermission"""', "['role_name', 'username', 'is_robot', 'avatar', 'is_org_member', 'has_org']"], {}), "('UserPermission', ['role_name', 'username', 'is_robot', 'avatar',\n    'is_org_member', 'has_org'])\n", (616, 715), False, 'from collections import namedtuple\n'), ((1160, 1249), 'collections.namedtuple', 'namedtuple', (['"""RobotPermission"""', "['role_name', 'username', 'is_robot', 'is_org_member']"], {}), "('RobotPermission', ['role_name', 'username', 'is_robot',\n    'is_org_member'])\n", (1170, 1249), False, 'from collections import namedtuple\n'), ((1599, 1665), 'collections.namedtuple', 'namedtuple', (['"""TeamPermission"""', "['role_name', 'team_name', 'avatar']"], {}), "('TeamPermission', ['role_name', 'team_name', 'avatar'])\n", (1609, 1665), False, 'from collections import namedtuple\n'), ((1899, 1921), 'six.add_metaclass', 'add_metaclass', (['ABCMeta'], {}), '(ABCMeta)\n', (1912, 1921), False, 'from six import add_metaclass\n'), ((207, 221), 'sys.exc_info', 'sys.exc_info', ([], {}), '()\n', (219, 221), False, 'import sys\n'), ((370, 384), 'sys.exc_info', 'sys.exc_info', ([], {}), '()\n', (382, 384), False, 'import sys\n')]