# Path: pypi_redirect/pypi_redirect/server_app/handler/file_handler.py
# class FileHandler(object):
#     def __init__(self, pypi_base_url, http_get_fn, parse_index_fn):
#         self.pypi_base_url = pypi_base_url
#         self.http_get_fn = http_get_fn
#         self.parse_index_fn = parse_index_fn
# 
#     def _is_checksum_file(self, filename):
#         return filename.lower().endswith(('.md5', '.sha1'))
# 
#     def _handle_checksum(self, checksum_filename, index_rows, response):
#         filename_base, ext = os.path.splitext(checksum_filename)
#         ext_no_dot = ext[1:].lower()
#         _ensure_file_in_index(filename=filename_base, index_rows=index_rows)
#         checksums = index_rows[filename_base].checksums
# 
#         return _get_checksum(
#             checksums=checksums,
#             checksum_ext=ext_no_dot,
#             response_headers=response.headers)
# 
#     def _redirect_to_download_url(self, filename, index_rows):
#         _ensure_file_in_index(filename=filename, index_rows=index_rows)
#         download_url = index_rows[filename].download_url
#         raise http_302(download_url)
# 
#     @ensure_python_dir
#     def handle(self, path, request, response):
#         py, package_name, filename = path
# 
#         index_url = '{}/{}/'.format(self.pypi_base_url, package_name)
# 
#         index_rows = fetch_and_parse_index(
#             http_get_fn=self.http_get_fn,
#             parse_index_fn=self.parse_index_fn,
#             pypi_base_url=self.pypi_base_url,
#             index_url=index_url,
#             package_path=package_name)
# 
#         if self._is_checksum_file(filename=filename):
#             return self._handle_checksum(
#                 checksum_filename=filename,
#                 index_rows=index_rows,
#                 response=response)
# 
#         self._redirect_to_download_url(
#             filename=filename,
#             index_rows=index_rows)
# 
# Path: pypi_redirect/pypi_redirect/server_app/index_parser.py
# def _all_internal_links_and_directories(html_root):
# def parse(
#         base_url,
#         package_path,
#         html_str,
#         strict_html=True,
#         find_links_fn=_all_internal_links_and_directories):
# def _parse_internal_links(base_url, html_root, package_path, find_links_fn):
# def _is_ascii(s):
# def _parse_html(html_str, strict_html):
# def _is_absolute_url(url):
# def _make_url_absolute(base_url, package_path, url):
# def _determine_checksums(href):


from collections import OrderedDict, namedtuple
from functools import partial
from lxml.etree import ParseError
from nose.tools import eq_
from requests import RequestException
from ...server_app.handler.file_handler import FileHandler
from ...server_app.index_parser import IndexRow, Checksums
from _utils import FunctionStub, RequestStub, ResponseStub
from _utils import assert_http_redirect, assert_http_not_found


FileEntry = namedtuple('FileEntry', ('pkg_name', 'filename', 'index_row'))


def _generate_file_entry(has_md5=True, has_sha1=True):
    checksums = Checksums(
        md5='MD5-nose-1.2.1.tar.gz' if has_md5 else None,
        sha1='SHA1-nose-1.2.1.tar.gz' if has_sha1 else None)

    file_entry = FileEntry(
        pkg_name='nose',
        filename='nose-1.2.1.tar.gz',

# Path: pypi_redirect/pypi_redirect/server_app/handler/file_handler.py
# class FileHandler(object):
#     def __init__(self, pypi_base_url, http_get_fn, parse_index_fn):
#         self.pypi_base_url = pypi_base_url
#         self.http_get_fn = http_get_fn
#         self.parse_index_fn = parse_index_fn
# 
#     def _is_checksum_file(self, filename):
#         return filename.lower().endswith(('.md5', '.sha1'))
# 
#     def _handle_checksum(self, checksum_filename, index_rows, response):
#         filename_base, ext = os.path.splitext(checksum_filename)
#         ext_no_dot = ext[1:].lower()
#         _ensure_file_in_index(filename=filename_base, index_rows=index_rows)
#         checksums = index_rows[filename_base].checksums
# 
#         return _get_checksum(
#             checksums=checksums,
#             checksum_ext=ext_no_dot,
#             response_headers=response.headers)
# 
#     def _redirect_to_download_url(self, filename, index_rows):
#         _ensure_file_in_index(filename=filename, index_rows=index_rows)
#         download_url = index_rows[filename].download_url
#         raise http_302(download_url)
# 
#     @ensure_python_dir
#     def handle(self, path, request, response):
#         py, package_name, filename = path
# 
#         index_url = '{}/{}/'.format(self.pypi_base_url, package_name)
# 
#         index_rows = fetch_and_parse_index(
#             http_get_fn=self.http_get_fn,
#             parse_index_fn=self.parse_index_fn,
#             pypi_base_url=self.pypi_base_url,
#             index_url=index_url,
#             package_path=package_name)
# 
#         if self._is_checksum_file(filename=filename):
#             return self._handle_checksum(
#                 checksum_filename=filename,
#                 index_rows=index_rows,
#                 response=response)
# 
#         self._redirect_to_download_url(
#             filename=filename,
#             index_rows=index_rows)
# 
# Path: pypi_redirect/pypi_redirect/server_app/index_parser.py
# def _all_internal_links_and_directories(html_root):
# def parse(
#         base_url,
#         package_path,
#         html_str,
#         strict_html=True,
#         find_links_fn=_all_internal_links_and_directories):
# def _parse_internal_links(base_url, html_root, package_path, find_links_fn):
# def _is_ascii(s):
# def _parse_html(html_str, strict_html):
# def _is_absolute_url(url):
# def _make_url_absolute(base_url, package_path, url):
# def _determine_checksums(href):


# Path: pypi_redirect/pypi_redirect/test/unit_tests/file_handler_test.py
from collections import OrderedDict, namedtuple
from functools import partial
from lxml.etree import ParseError
from nose.tools import eq_
from requests import RequestException
from ...server_app.handler.file_handler import FileHandler
from ...server_app.index_parser import IndexRow, Checksums
from _utils import FunctionStub, RequestStub, ResponseStub
from _utils import assert_http_redirect, assert_http_not_found



FileEntry = namedtuple('FileEntry', ('pkg_name', 'filename', 'index_row'))


def _generate_file_entry(has_md5=True, has_sha1=True):
    checksums = Checksums(
        md5='MD5-nose-1.2.1.tar.gz' if has_md5 else None,
        sha1='SHA1-nose-1.2.1.tar.gz' if has_sha1 else None)

    file_entry = FileEntry(
        pkg_name='nose',
        filename='nose-1.2.1.tar.gz',

        index_row=IndexRow(