# Path: ghtools/exceptions.py
# class GithubAPIError(GithubError):
#     pass


import csv
import logging
from ghtools.exceptions import GithubAPIError

log = logging.getLogger(__name__)


class TeamMigrator(object):
    def __init__(self, src, dst, mapping_path):
        self.src = src
        self.dst = dst
        self._mapping = self._load_members_mapping(mapping_path)
        self._dst_teams = dict((t['name'], t) for t in self.dst.list_teams())

    def migrate(self):
        for team in self.src.list_teams():
            self._create_team(team)
            self._migrate_members(team)
            self._migrate_repos(team)

    def _create_team(self, team):
        if team['name'] in self._dst_teams:
            log.info('Team {0} already exists'.format(team['name']))
        else:
            team = {
                'name': team['name'],
                'permission': team['permission']
            }
            self._dst_teams[team['name']] = self.dst.create_team(team)

    def _migrate_members(self, team):
        for member in self.src.list_team_members(team):
            if member['login'] not in self._mapping:

# Path: ghtools/exceptions.py
# class GithubAPIError(GithubError):
#     pass


# Path: ghtools/migrators/teams.py
import csv
import logging
from ghtools.exceptions import GithubAPIError


log = logging.getLogger(__name__)


class TeamMigrator(object):
    def __init__(self, src, dst, mapping_path):
        self.src = src
        self.dst = dst
        self._mapping = self._load_members_mapping(mapping_path)
        self._dst_teams = dict((t['name'], t) for t in self.dst.list_teams())

    def migrate(self):
        for team in self.src.list_teams():
            self._create_team(team)
            self._migrate_members(team)
            self._migrate_repos(team)

    def _create_team(self, team):
        if team['name'] in self._dst_teams:
            log.info('Team {0} already exists'.format(team['name']))
        else:
            team = {
                'name': team['name'],
                'permission': team['permission']
            }
            self._dst_teams[team['name']] = self.dst.create_team(team)

    def _migrate_members(self, team):
        for member in self.src.list_team_members(team):
            if member['login'] not in self._mapping:

                log.warning('Failed to migrate member to {0}, {1} is not in the mapping file'.format(team['name'], member['login']))