# Path: hepdata/utils/file_extractor.py
# def extract(file_path, unzipped_path):
#     if zipfile.is_zipfile(file_path):
#         zipped_submission = zipfile.ZipFile(file_path)
#         zipped_submission.printdir()
#         zipped_submission.extractall(path=unzipped_path)
#         zipped_submission.close()
#         return unzipped_path
#     elif tarfile.is_tarfile(file_path):
#         tar = tarfile.open(file_path)
#         tar.extractall(path=unzipped_path)
#         tar.close()
#         return unzipped_path
#     elif is_gz_file(file_path) and file_path.endswith('.yaml.gz'):
#         with gzip.GzipFile(file_path, 'rb') as gzip_file:
#             with open(unzipped_path, 'wb') as unzipped_file:
#                 unzipped_file.write(gzip_file.read())
#         return unzipped_path
#     return None
# 
# def get_file_in_directory(path, extension):
#     file_info = find_file_in_directory(path, lambda x: x.endswith(extension))
#     return file_info[1] if file_info else None
# 
# Path: hepdata/utils/miscellaneous.py
# def sanitize_html(value, tags=None, attributes=None, strip=False):
#     """Sanitize HTML.
# 
#     :param tags: Allowed HTML ``tags``. Configuration set by Invenio-Config.
#     :param attributes: Allowed HTML ``attributes``. Configuration set by
#         Invenio-Config.
#     :param strip: Whether to strip tags that are not allowed. Defaults to
#         False (escapes rather than strips disallowed tags).
# 
#     Use this function when you need to include unescaped HTML that contains
#     user provided data.
#     """
#     if value is None:
#         return value
# 
#     from flask import current_app
# 
#     value = value.strip()
# 
#     # Look for conditions like v1<x<v2 which look like invalid HTML,
#     # and escape them before running bleach
#     p = re.compile('<([^>]*?)<')
#     escaped = p.sub(r'&lt;\1&lt;', value)
# 
#     tags = tags if tags is not None \
#         else current_app.config.get('ALLOWED_HTML_TAGS', [])
#     attributes = attributes if attributes is not None \
#         else current_app.config.get('ALLOWED_HTML_ATTRS', {})
# 
#     cleaned = bleach.clean(
#         escaped,
#         tags=tags,
#         attributes=attributes,
#         strip=strip
#     )
# 
#     return cleaned
# 
# def splitter(data, predicate):
#     """Split a list according to a given predicate (lambda)."""
#     yes, no = [], []
#     for d in data:
#         (yes if predicate(d) else no).append(d)
#     return yes, no


import os
from hepdata.utils.file_extractor import extract, get_file_in_directory
from hepdata.utils.miscellaneous import sanitize_html, splitter
             "Variation of Tkin with &lt;β&gt; for different energies and centralities."),
            ("""- - - - - - - - Overview of HEPData Record - - - - - - - - <br/><br/>
<b>Background Fit results:</b> <ul> <li><a href=\"89413?version=1&table=Backgroundfit1\">CRs</a>
<li><a href=\"89413?version=1&table=Backgroundfit2\">VRs</a> <li><a href=\"89413?version=1&table=Backgroundfit5\">inclusive
DF-0J SRs</a> <li><a href=\"89413?version=1&table=Backgroundfit6\">inclusive DF-1J
SRs</a> <li><a href=\"89413?version=1&table=Backgroundfit3\">inclusive SF-0J SRs</a>
<li><a href=\"89413?version=1&table=Backgroundfit4\">inclusive SF-1J SRs</a> </ul>""",
              """- - - - - - - - Overview of HEPData Record - - - - - - - - <br><br>
<b>Background Fit results:</b> <ul> <li><a href=\"89413?version=1&amp;table=Backgroundfit1\">CRs</a>
</li><li><a href=\"89413?version=1&amp;table=Backgroundfit2\">VRs</a> </li><li><a href=\"89413?version=1&amp;table=Backgroundfit5\">inclusive
DF-0J SRs</a> </li><li><a href=\"89413?version=1&amp;table=Backgroundfit6\">inclusive DF-1J
SRs</a> </li><li><a href=\"89413?version=1&amp;table=Backgroundfit3\">inclusive SF-0J SRs</a>
</li><li><a href=\"89413?version=1&amp;table=Backgroundfit4\">inclusive SF-1J SRs</a> </li></ul>"""),
            (None, None)
        ]

        # Test default params
        for original, sanitized in test_cases:
            assert(sanitize_html(original) == sanitized)

        # Test other params
        assert(sanitize_html(test_cases[0][0], strip=True)
               == "<b>Here is some bold text</b> and here is a dodgy script")
        assert(sanitize_html(test_cases[0][0], tags=["script", "b"])
               == test_cases[0][0])
        assert(sanitize_html(test_cases[0][0], tags=["i"])
               == "&lt;b&gt;Here is some bold text&lt;/b&gt; and &lt;script&gt;here is a dodgy script&lt;/script&gt;")
        assert(sanitize_html("<a href=\"89413?version=1&amp;table=Backgroundfit3\">inclusive SF-0J SRs</a>",
                             attributes=["title"])
               == "<a>inclusive SF-0J SRs</a>")

# Path: hepdata/utils/file_extractor.py
# def extract(file_path, unzipped_path):
#     if zipfile.is_zipfile(file_path):
#         zipped_submission = zipfile.ZipFile(file_path)
#         zipped_submission.printdir()
#         zipped_submission.extractall(path=unzipped_path)
#         zipped_submission.close()
#         return unzipped_path
#     elif tarfile.is_tarfile(file_path):
#         tar = tarfile.open(file_path)
#         tar.extractall(path=unzipped_path)
#         tar.close()
#         return unzipped_path
#     elif is_gz_file(file_path) and file_path.endswith('.yaml.gz'):
#         with gzip.GzipFile(file_path, 'rb') as gzip_file:
#             with open(unzipped_path, 'wb') as unzipped_file:
#                 unzipped_file.write(gzip_file.read())
#         return unzipped_path
#     return None
# 
# def get_file_in_directory(path, extension):
#     file_info = find_file_in_directory(path, lambda x: x.endswith(extension))
#     return file_info[1] if file_info else None
# 
# Path: hepdata/utils/miscellaneous.py
# def sanitize_html(value, tags=None, attributes=None, strip=False):
#     """Sanitize HTML.
# 
#     :param tags: Allowed HTML ``tags``. Configuration set by Invenio-Config.
#     :param attributes: Allowed HTML ``attributes``. Configuration set by
#         Invenio-Config.
#     :param strip: Whether to strip tags that are not allowed. Defaults to
#         False (escapes rather than strips disallowed tags).
# 
#     Use this function when you need to include unescaped HTML that contains
#     user provided data.
#     """
#     if value is None:
#         return value
# 
#     from flask import current_app
# 
#     value = value.strip()
# 
#     # Look for conditions like v1<x<v2 which look like invalid HTML,
#     # and escape them before running bleach
#     p = re.compile('<([^>]*?)<')
#     escaped = p.sub(r'&lt;\1&lt;', value)
# 
#     tags = tags if tags is not None \
#         else current_app.config.get('ALLOWED_HTML_TAGS', [])
#     attributes = attributes if attributes is not None \
#         else current_app.config.get('ALLOWED_HTML_ATTRS', {})
# 
#     cleaned = bleach.clean(
#         escaped,
#         tags=tags,
#         attributes=attributes,
#         strip=strip
#     )
# 
#     return cleaned
# 
# def splitter(data, predicate):
#     """Split a list according to a given predicate (lambda)."""
#     yes, no = [], []
#     for d in data:
#         (yes if predicate(d) else no).append(d)
#     return yes, no


# Path: tests/miscellaneous_test.py
import os
from hepdata.utils.file_extractor import extract, get_file_in_directory
from hepdata.utils.miscellaneous import sanitize_html, splitter

             "Variation of Tkin with &lt;β&gt; for different energies and centralities."),
            ("""- - - - - - - - Overview of HEPData Record - - - - - - - - <br/><br/>
<b>Background Fit results:</b> <ul> <li><a href=\"89413?version=1&table=Backgroundfit1\">CRs</a>
<li><a href=\"89413?version=1&table=Backgroundfit2\">VRs</a> <li><a href=\"89413?version=1&table=Backgroundfit5\">inclusive
DF-0J SRs</a> <li><a href=\"89413?version=1&table=Backgroundfit6\">inclusive DF-1J
SRs</a> <li><a href=\"89413?version=1&table=Backgroundfit3\">inclusive SF-0J SRs</a>
<li><a href=\"89413?version=1&table=Backgroundfit4\">inclusive SF-1J SRs</a> </ul>""",
              """- - - - - - - - Overview of HEPData Record - - - - - - - - <br><br>
<b>Background Fit results:</b> <ul> <li><a href=\"89413?version=1&amp;table=Backgroundfit1\">CRs</a>
</li><li><a href=\"89413?version=1&amp;table=Backgroundfit2\">VRs</a> </li><li><a href=\"89413?version=1&amp;table=Backgroundfit5\">inclusive
DF-0J SRs</a> </li><li><a href=\"89413?version=1&amp;table=Backgroundfit6\">inclusive DF-1J
SRs</a> </li><li><a href=\"89413?version=1&amp;table=Backgroundfit3\">inclusive SF-0J SRs</a>
</li><li><a href=\"89413?version=1&amp;table=Backgroundfit4\">inclusive SF-1J SRs</a> </li></ul>"""),
            (None, None)
        ]

        # Test default params
        for original, sanitized in test_cases:
            assert(sanitize_html(original) == sanitized)

        # Test other params
        assert(sanitize_html(test_cases[0][0], strip=True)
               == "<b>Here is some bold text</b> and here is a dodgy script")
        assert(sanitize_html(test_cases[0][0], tags=["script", "b"])
               == test_cases[0][0])
        assert(sanitize_html(test_cases[0][0], tags=["i"])
               == "&lt;b&gt;Here is some bold text&lt;/b&gt; and &lt;script&gt;here is a dodgy script&lt;/script&gt;")
        assert(sanitize_html("<a href=\"89413?version=1&amp;table=Backgroundfit3\">inclusive SF-0J SRs</a>",
                             attributes=["title"])
               == "<a>inclusive SF-0J SRs</a>")

        assert(sanitize_html(test_cases[1][0], tags=[], strip=True)