<commit_before>import os
from json import dumps
from pkg_resources import get_distribution

from click import prompt

from getgist import GetGistCommons
from getgist.request import GetGistRequests


class FileFromGist:
    def __init__(self, name, gist, url, public):
        self.name = name
        self.gist = gist
        self.url = url
        self.public = public

    def __str__(self):
        if self.public:
            return self.name
        return "{} [Secret Gist]".format(self.name)

    def __repr__(self):
        return str(
            {
                "name": self.name,
                "gist": self.gist,
                "url": self.url,
                "public": self.public,
            }
        )

    def __eq__(self, other):
        if self.name != other.name:
            return False
        if self.gist != other.gist:
            return False
        if self.url != other.url:
            return False
        if self.public != other.public:
            return False
        return True


def oauth_only(function):
    """Decorator to restrict some GitHubTools methods to run only with OAuth"""

    def check_for_oauth(self, *args, **kwargs):
        """
        Returns False if GitHubTools instance is not authenticated, or return
        the decorated function if it is.
        """
        if not self.is_authenticated:
            self.oops("To use putgist you have to set your GETGIST_TOKEN")
            self.oops("(see `putgist --help` for details)")
            return False
        return function(self, *args, **kwargs)

    return check_for_oauth


def with_filename_only(function):
    """Decorator to restrict some GitHubTools methods to run only if the
    instance has an attribute `filename` set (which is optional)."""

    def check_for_filename_attribute(self, *args, **kwargs):
        """
        Returns False if GitHubTools instance has no filename attribute, or
        return the decorated function if it has.
        """
        if not hasattr(self, "filename") or not self.filename:
            msg = "To use {} method you need a filename, which was not set."
            self.oops(msg.format(function.__name__))
            return False
        return function(self, *args, **kwargs)

    return check_for_filename_attribute


class GitHubTools(GetGistCommons):
    """Helpers to deal with GitHub API and manipulate gists"""

    version = get_distribution("getgist").version
    api_root_url = "https://api.github.com/"
    headers = {
        "Accept": "application/vnd.github.v3+json",
        "User-Agent": "GetGist v{}".format(version),
    }
    requests = GetGistRequests(headers)
    is_authenticated = False

    def __init__(self, user, file_path, assume_yes=False):
        """
        Save basic variables to all methods, instantiate GetGistrequests and
        calls the OAuth method.
        :param user: (str) GitHub username
        :param file_path: (str or None) file_path to be saved (locally), created or updated (remotelly)
        :param assume_yes: (bool) assume yes (or first option) for all prompts
        :return: (None)
        """
        self.user = user
        self.file_path = file_path
        self.filename = os.path.basename(file_path) if file_path else None
        self.assume_yes = assume_yes
        self.add_oauth_header()

    def add_oauth_header(self):
        """
        Validate token and add the proper header for further requests.
        :return: (None)
        """
        # abort if no token
        oauth_token = self._get_token()
        if not oauth_token:
            return

        # add oauth header & reach the api
        self.headers["Authorization"] = "token " + oauth_token
        url = self._api_url("user")
        raw_resp = self.requests.get(url)
        resp = raw_resp.json()

        # abort & remove header if token is invalid
        if resp.get("login", None) != self.user:
            self.oops("Invalid token for user " + self.user)
            self.headers.pop("Authorization")
            return

        self.is_authenticated = True
        self.yeah("User {} authenticated".format(self.user))

    def get_gists(self, page=1):
        """
        List generator containing gist relevant information
        such as id, description, filenames and raw URL (dict).<commit_msg>Make username comparison case-insensitive.<commit_after>import os
from json import dumps
from pkg_resources import get_distribution

from click import prompt

from getgist import GetGistCommons
from getgist.request import GetGistRequests


class FileFromGist:
    def __init__(self, name, gist, url, public):
        self.name = name
        self.gist = gist
        self.url = url
        self.public = public

    def __str__(self):
        if self.public:
            return self.name
        return "{} [Secret Gist]".format(self.name)

    def __repr__(self):
        return str(
            {
                "name": self.name,
                "gist": self.gist,
                "url": self.url,
                "public": self.public,
            }
        )

    def __eq__(self, other):
        if self.name != other.name:
            return False
        if self.gist != other.gist:
            return False
        if self.url != other.url:
            return False
        if self.public != other.public:
            return False
        return True


def oauth_only(function):
    """Decorator to restrict some GitHubTools methods to run only with OAuth"""

    def check_for_oauth(self, *args, **kwargs):
        """
        Returns False if GitHubTools instance is not authenticated, or return
        the decorated function if it is.
        """
        if not self.is_authenticated:
            self.oops("To use putgist you have to set your GETGIST_TOKEN")
            self.oops("(see `putgist --help` for details)")
            return False
        return function(self, *args, **kwargs)

    return check_for_oauth


def with_filename_only(function):
    """Decorator to restrict some GitHubTools methods to run only if the
    instance has an attribute `filename` set (which is optional)."""

    def check_for_filename_attribute(self, *args, **kwargs):
        """
        Returns False if GitHubTools instance has no filename attribute, or
        return the decorated function if it has.
        """
        if not hasattr(self, "filename") or not self.filename:
            msg = "To use {} method you need a filename, which was not set."
            self.oops(msg.format(function.__name__))
            return False
        return function(self, *args, **kwargs)

    return check_for_filename_attribute


class GitHubTools(GetGistCommons):
    """Helpers to deal with GitHub API and manipulate gists"""

    version = get_distribution("getgist").version
    api_root_url = "https://api.github.com/"
    headers = {
        "Accept": "application/vnd.github.v3+json",
        "User-Agent": "GetGist v{}".format(version),
    }
    requests = GetGistRequests(headers)
    is_authenticated = False

    def __init__(self, user, file_path, assume_yes=False):
        """
        Save basic variables to all methods, instantiate GetGistrequests and
        calls the OAuth method.
        :param user: (str) GitHub username
        :param file_path: (str or None) file_path to be saved (locally), created or updated (remotelly)
        :param assume_yes: (bool) assume yes (or first option) for all prompts
        :return: (None)
        """
        self.user = user
        self.file_path = file_path
        self.filename = os.path.basename(file_path) if file_path else None
        self.assume_yes = assume_yes
        self.add_oauth_header()

    def add_oauth_header(self):
        """
        Validate token and add the proper header for further requests.
        :return: (None)
        """
        # abort if no token
        oauth_token = self._get_token()
        if not oauth_token:
            return

        # add oauth header & reach the api
        self.headers["Authorization"] = "token " + oauth_token
        url = self._api_url("user")
        raw_resp = self.requests.get(url)
        resp = raw_resp.json()

        # abort & remove header if token is invalid
        if resp.get("login", None).lower() != self.user.lower():
            self.oops("Invalid token for user " + self.user)
            self.headers.pop("Authorization")
            return

        self.is_authenticated = True
        self.yeah("User {} authenticated".format(self.user))

    def get_gists(self, page=1):
        """
        List generator containing gist relevant information
        such as id, description, filenames and raw URL (dict).
{'fraction_non_alphanumeric': 0.0926374006221915, 'fraction_numerical': 0.0004608825901601567, 'mean_word_length': 2.2076866223207685, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3577020', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2388, 'n_words': 762}