# Path: bakery/cookies/exceptions.py
# class InvalidRepositoryError(ValueError):
#     pass
# 
# class InvalidContentFileEncoding(ValueError):
#     pass


import json
import pytz
import uuid
import http.client
from base64 import standard_b64decode
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.timezone import make_aware
from github import Github
from bakery.cookies.exceptions import (InvalidRepositoryError,
                                       InvalidContentFileEncoding)


def get_mapping_file_from_repo(repo):
    """
    Finds a ``cookiecutter.json`` or another JSON file in the repository root
    and treat it as the mapping file.

    The candidate selection works as follows:
    #. All files ending with ``.json`` on the root-level will be added to a candidate set.
    #. If now candidates have been found raise ``InvalidRepositoryError``
    #. If more than one candidate has been found:

       #. if there is a ``cookiecutter.json`` in the candidate list, use it
       #. Otherwise raise ``InvalidRepositoryError``

    #. Return there is exactly one ``JSON`` file: use it
    #. If a mapping_file has been found, open it as a ``PyGithub.ContentFile``
       and return the content_file

    :raises: ``InvalidRepositoryError`` if there was no way to
        deterministically find the mapping file.
    """
    candidates = filter_repo(repo, {'name': lambda val: val.endswith('.json')})

    if not candidates:
        raise InvalidRepositoryError('No JSON mapping file found!')

    if len(candidates) > 1:
        mapping_file = candidates.get('cookiecutter.json', None)
        if mapping_file is None:

# Path: bakery/cookies/exceptions.py
# class InvalidRepositoryError(ValueError):
#     pass
# 
# class InvalidContentFileEncoding(ValueError):
#     pass


# Path: bakery/utils/vcs/gh.py
import json
import pytz
import uuid
import http.client
from base64 import standard_b64decode
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.timezone import make_aware
from github import Github
from bakery.cookies.exceptions import (InvalidRepositoryError,
                                       InvalidContentFileEncoding)



def get_mapping_file_from_repo(repo):
    """
    Finds a ``cookiecutter.json`` or another JSON file in the repository root
    and treat it as the mapping file.

    The candidate selection works as follows:
    #. All files ending with ``.json`` on the root-level will be added to a candidate set.
    #. If now candidates have been found raise ``InvalidRepositoryError``
    #. If more than one candidate has been found:

       #. if there is a ``cookiecutter.json`` in the candidate list, use it
       #. Otherwise raise ``InvalidRepositoryError``

    #. Return there is exactly one ``JSON`` file: use it
    #. If a mapping_file has been found, open it as a ``PyGithub.ContentFile``
       and return the content_file

    :raises: ``InvalidRepositoryError`` if there was no way to
        deterministically find the mapping file.
    """
    candidates = filter_repo(repo, {'name': lambda val: val.endswith('.json')})

    if not candidates:
        raise InvalidRepositoryError('No JSON mapping file found!')

    if len(candidates) > 1:
        mapping_file = candidates.get('cookiecutter.json', None)
        if mapping_file is None:

            raise InvalidRepositoryError('Cannot decide for a mapping file! '