from abc import ABCMeta, abstractmethod
from collections import namedtuple
from six import add_metaclass

class RepositoryToken(namedtuple('RepositoryToken', ['friendly_name', 'code', 'role'])):
  """
  RepositoryToken represents a (deprecated) repository access token.
  :type friendly_name: string
  :type code: string
  :type role: string
  """

  def to_dict(self):
    return {
      'friendlyName': self.friendly_name,
      'code': self.code,
      'role': self.role.name,
    }


@add_metaclass(ABCMeta)
class RepoTokenDataInterface(object):
  """
  Interface that represents all data store interactions required by the repository tokens API.
  Note that this API is *deprecated*.
  """

  @abstractmethod
  def get_repository_tokens(self, namespace_name, repository_name):
    """
    Returns an iterator of all the RepositoryToken's found for the repository with the given
    namespace and name. If the repository doesn't exist, an empty iterator is returned.
    """

  @abstractmethod
  def create_repository_token(self, namespace_name, repository_name, friendly_name):
    """
    Creates and returns a new RepositoryToken under the matching repository.
    """

  @abstractmethod
  def get_repository_token(self, namespace_name, repository_name, code):
    """
    Returns the repository access token with the given code under the matching repository or None
    if none.
    """

  @abstractmethod
  def delete_repository_token(self, namespace_name, repository_name, code):
    """
    Deletes the repository access token with the given code under the matching repository, if any.
    Returns the token deleted or None if none.
    """

  @abstractmethod
  def set_repository_token_role(self, namespace_name, repository_name, code, role_name):
    """
    Sets the role of the repository token with the given code under the matching repository, to the
    role given.
    """