<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function
import logging
from gitlab import Gitlab
from gitlab.exceptions import GitlabGetError, GitlabCreateError
from ..errors import BranchExistsError, RepoDoesNotExistError
from base64 import b64encode

logger = logging.getLogger(__name__)


class BadTokenError(Exception):
    pass


class Provider(object):
    name = 'gitlab'

    class Committer(object):
        def __init__(self, login):
            self.login = login

    def __init__(self, bundle, intergration=False, url=None):
        self.bundle = bundle
        self.url = url
        if intergration:
            raise NotImplementedError(
                'Gitlab provider does not support integration mode')

    @classmethod
    def is_same_user(cls, this, that):
        return this.login == that.login

    def _api(self, token):
        parts = token.split('@')
        if len(parts) == 1:
            host = self.url or 'https://gitlab.com'
            auth = parts[0]
        elif len(parts) == 2:
            auth, host = parts
        else:
            raise BadTokenError(
                'Got token "{}": format should be wither "apikey" for '
                'gitlab.com, or "apikey@https://yourgitlab.local"'.format(
                    token))
        return Gitlab(host, auth)

    def get_user(self, token):
        gl = self._api(token)
        gl.auth()
        return gl.user

    def get_repo(self, token, name):
        try:
            return self._api(token).projects.get(name)
        except GitlabGetError as e:
            if e.response_code == 404:
                raise RepoDoesNotExistError()
            raise e

    def get_default_branch(self, repo):
        return repo.default_branch

    def get_pull_request_permissions(self, user, repo):
        # TODO: IDK how this works on gitlab
        return True

    def iter_git_tree(self, repo, branch):
        for item in repo.repository_tree(ref=branch, recursive=True, all=True):
            yield item['type'], item['path']

    def get_file(self, repo, path, branch):
        logger.info("Getting file at {} for branch {}".format(path, branch))
        try:
            contentfile = repo.files.get(file_path=path, ref=branch)
        except GitlabGetError as e:
            if e.response_code == 404:
                logger.warning("Unable to get {path}".format(
                    path=path,
                ))
                return None, None
        else:
            return contentfile.decode().decode("utf-8"), contentfile

    def create_and_commit_file(self, repo, path, branch, content, commit_message, committer):

        # TODO: committer
        return repo.files.create({
            'file_path': path,
            'branch': branch,
            'content': content,
            'commit_message': commit_message
        })

    def get_requirement_file(self, repo, path, branch):
        content, file_obj = self.get_file(repo, path, branch)
        if content is not None and file_obj is not None:<commit_msg>lstrip leading slash to avoid gitlab errors as suggested by @rafaelpivato #375<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function
import logging
from gitlab import Gitlab
from gitlab.exceptions import GitlabGetError, GitlabCreateError
from ..errors import BranchExistsError, RepoDoesNotExistError
from base64 import b64encode

logger = logging.getLogger(__name__)


class BadTokenError(Exception):
    pass


class Provider(object):
    name = 'gitlab'

    class Committer(object):
        def __init__(self, login):
            self.login = login

    def __init__(self, bundle, intergration=False, url=None):
        self.bundle = bundle
        self.url = url
        if intergration:
            raise NotImplementedError(
                'Gitlab provider does not support integration mode')

    @classmethod
    def is_same_user(cls, this, that):
        return this.login == that.login

    def _api(self, token):
        parts = token.split('@')
        if len(parts) == 1:
            host = self.url or 'https://gitlab.com'
            auth = parts[0]
        elif len(parts) == 2:
            auth, host = parts
        else:
            raise BadTokenError(
                'Got token "{}": format should be wither "apikey" for '
                'gitlab.com, or "apikey@https://yourgitlab.local"'.format(
                    token))
        return Gitlab(host, auth)

    def get_user(self, token):
        gl = self._api(token)
        gl.auth()
        return gl.user

    def get_repo(self, token, name):
        try:
            return self._api(token).projects.get(name)
        except GitlabGetError as e:
            if e.response_code == 404:
                raise RepoDoesNotExistError()
            raise e

    def get_default_branch(self, repo):
        return repo.default_branch

    def get_pull_request_permissions(self, user, repo):
        # TODO: IDK how this works on gitlab
        return True

    def iter_git_tree(self, repo, branch):
        for item in repo.repository_tree(ref=branch, recursive=True, all=True):
            yield item['type'], item['path']

    def get_file(self, repo, path, branch):
        logger.info("Getting file at {} for branch {}".format(path, branch))
        # remove unnecessary leading slash to avoid gitlab errors. See #375
        path = path.lstrip('/')
        try:
            contentfile = repo.files.get(file_path=path, ref=branch)
        except GitlabGetError as e:
            if e.response_code == 404:
                logger.warning("Unable to get {path}".format(
                    path=path,
                ))
                return None, None
        else:
            return contentfile.decode().decode("utf-8"), contentfile

    def create_and_commit_file(self, repo, path, branch, content, commit_message, committer):

        # TODO: committer
        return repo.files.create({
            'file_path': path,
            'branch': branch,
            'content': content,
            'commit_message': commit_message
        })

    def get_requirement_file(self, repo, path, branch):
        content, file_obj = self.get_file(repo, path, branch)
        if content is not None and file_obj is not None:
{'fraction_non_alphanumeric': 0.09718913768461172, 'fraction_numerical': 0.005717008099094807, 'mean_word_length': 2.1696024157020632, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5797811', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1731, 'n_words': 506}