import re

from unipath import Path

from pyolite.models.user import User


ACCEPTED_PERMISSIONS = set('RW+CD')


class ListUsers(object):
  def __init__(self, repo):
    self.repo = repo
    self.repo_config = Path(self.repo.path, 'conf/repos/',
                            "%s.conf" % self.repo.name)

    self._users = self._get_users()

  def _get_users(self):
    # TODO: check for groups
    users = []

    with open(str(self.repo_config)) as f:
      config = f.read()
      for match in re.compile('=( *)(\w+)').finditer(config):
        users.append(match.group(2))

    # TODO: return a users manager
    return users

  def add(self, user, permission):
    if isinstance(user, basestring):
      user = User.get_by_name(user, self.repo.path, self.repo.git)

    if not isinstance(user, User) or not user:
      message = 'We need an user object. Please see examples/repository'
      raise ValueError(message)

    with open(str(self.repo_config), 'a+') as f:
      # check if we have the user in repo
      users = [item[1] for item in re.compile('=( *)(\w+)').findall(f.read())]
      if user.name in users:
        raise ValueError('User %s already exists. Please check '
                         'example/repository.py in order to see how you can '
                         'delete or change permissions' % user.name)

      # check user's permissions
      if set(map(lambda permission: permission.upper(), permission)) - \
         ACCEPTED_PERMISSIONS != set([]):
        raise ValueError('Invalid permissions. They must be from %s' %
                         ACCEPTED_PERMISSIONS)
      # add user to repo
      f.write("    %s     =    %s\n" % (permission, user.name))
      self.repo.git.commit(['conf'],
                           'User %s added to repo %s with permissions: %s' %
                           (user, self.repo.name, permission))

    user.repos.append(self.repo)
    return user

  def __iter__(self):
    for user in self._user:
      yield user

  def __getitem__(self, item):
    return self._users[item]

  def __setitem__(self, item, value):
    self._users[item] = value

  def __add__(self, items):
    for item in items:
      self.append(item)

  def __str__(self):
    return "['%s']" % ', '.join(self._users)

@@ -629,19 +629,25 @@
 %0A%0A  def 
-add
+_get_user
 (self, u
@@ -649,28 +649,16 @@
 lf, user
-, permission
 ):%0A    i
@@ -906,24 +906,109 @@
 r(message)%0A%0A
+    return user%0A%0A  def add(self, user, permission):%0A    user = self._get_user(user)%0A%0A
     with ope
@@ -1986,24 +1986,399 @@
 eturn user%0A%0A
+  def edit(self, user, permission):%0A    user = self._get_user(user)%0A%0A    with open(str(self.repo_config), 'r+') as f:%0A      content = f.read()%0A      content = re.sub(r'(%5Cs*)(%5BRW+DC%5D*)(%5Cs*)=(%5Cs*)%25s' %25 user.name,%0A                       r%22%5Cn    %25s    =    %25s%22 %25 (permission, user.name),%0A                       content)%0A      f.seek(0)%0A      f.write(content)%0A      f.truncate()%0A%0A
   def __iter

Edit user's permissions