# Path: mfr/core/extension.py
# class BaseExporter(metaclass=abc.ABCMeta):
# class BaseRenderer(metaclass=abc.ABCMeta):
#     def __init__(self, ext, source_file_path, output_file_path, format, metadata):
#     def export(self):
#     def _get_module_name(self):
#     def __init__(self, metadata, file_path, url, assets_url, export_url):
#     def render(self):
#     def file_required(self):
#     def cache_result(self):
#     def _get_module_name(self):
# 
# Path: mfr/extensions/jasp/exceptions.py
# class JaspRendererError(RendererError):
# class JaspVersionError(JaspRendererError):
# class JaspFileCorruptError(JaspRendererError):
#     def __init__(self, message, *args, **kwargs):
#     def __init__(self, message, *args, code: int=400, created_by: str='',
#                  actual_version: str='', required_version: str='', **kwargs):
#     def __init__(self, message, *args, code: int=400, corruption_type: str='',
#                  reason: str='', **kwargs):
#     __TYPE = 'jasp_version'
#     __TYPE = 'jasp_file_corrupt'
# 
# Path: mfr/extensions/jasp/html_processor.py
# class HTMLProcessor(HTMLParser):
# 
#     # The HTMLProcessor replaces the src attribute in <image> tags with the base64 equivalent
#     # The image content comes from the zip_file (specified with set_src_source())
#     # It also strips <script> and <object> tags from the HTML (potential attack vectors)
# 
#     def __init__(self, zip_file):
#         HTMLParser.__init__(self)
#         self._html = StringIO()  # buffer for the processed HTML
#         self._zip_file = zip_file
# 
#     def set_src_source(self, zip_file):
#         self._zip_file = zip_file
# 
#     def handle_starttag(self, tag, attrs):
#         if tag == 'script' or tag == 'object':  # filter scripts and objects (attack vectors)
#             return
# 
#         self._html.write('<')
#         self._html.write(tag)
# 
#         for attr in attrs:
#             self._html.write(' ')
#             self._html.write(attr[0])
#             if attr[1] is not None:
#                 self._html.write('="')
#                 if attr[0] == 'src':
#                     self._insert_data_uri(attr[1])
#                 else:
#                     self._html.write(attr[1])
# 
#                 self._html.write('"')
# 
#         self._html.write('>')
# 
#     def _insert_data_uri(self, src):
#         with self._zip_file.open(src) as src_file:
#             src_data = src_file.read()
#             src_b64 = base64.b64encode(src_data)
# 
#             self._html.write('data:image/png;base64,')
#             self._html.write(src_b64.decode('utf-8'))
# 
#     def handle_endtag(self, tag):
#         if tag == 'script' or tag == 'object':
#             return
# 
#         self._html.write('</')
#         self._html.write(tag)
#         self._html.write('>')
# 
#     def handle_data(self, data):
#         self._html.write(data)
# 
#     def final_html(self):
#         return self._html.getvalue()


import os
from mako.lookup import TemplateLookup
from mfr.core import extension
from mfr.extensions.jasp import exceptions
from zipfile import ZipFile, BadZipFile
from distutils.version import LooseVersion
from .html_processor import HTMLProcessor



class JASPRenderer(extension.BaseRenderer):

    # Minimum data archive version supported
    MINIMUM_VERSION = LooseVersion('1.0.2')

    TEMPLATE = TemplateLookup(
        directories=[
            os.path.join(os.path.dirname(__file__), 'templates')
        ]).get_template('viewer.mako')

    MESSAGE_FILE_CORRUPT = 'This JASP file is corrupt and cannot be viewed.'

    def render(self):
        try:
            with ZipFile(self.file_path) as zip_file:
                self._check_file(zip_file)
                body = self._render_html(zip_file, self.metadata.ext)
                return self.TEMPLATE.render(base=self.assets_url, body=body)
        except BadZipFile as err:

# Path: mfr/core/extension.py
# class BaseExporter(metaclass=abc.ABCMeta):
# class BaseRenderer(metaclass=abc.ABCMeta):
#     def __init__(self, ext, source_file_path, output_file_path, format, metadata):
#     def export(self):
#     def _get_module_name(self):
#     def __init__(self, metadata, file_path, url, assets_url, export_url):
#     def render(self):
#     def file_required(self):
#     def cache_result(self):
#     def _get_module_name(self):
# 
# Path: mfr/extensions/jasp/exceptions.py
# class JaspRendererError(RendererError):
# class JaspVersionError(JaspRendererError):
# class JaspFileCorruptError(JaspRendererError):
#     def __init__(self, message, *args, **kwargs):
#     def __init__(self, message, *args, code: int=400, created_by: str='',
#                  actual_version: str='', required_version: str='', **kwargs):
#     def __init__(self, message, *args, code: int=400, corruption_type: str='',
#                  reason: str='', **kwargs):
#     __TYPE = 'jasp_version'
#     __TYPE = 'jasp_file_corrupt'
# 
# Path: mfr/extensions/jasp/html_processor.py
# class HTMLProcessor(HTMLParser):
# 
#     # The HTMLProcessor replaces the src attribute in <image> tags with the base64 equivalent
#     # The image content comes from the zip_file (specified with set_src_source())
#     # It also strips <script> and <object> tags from the HTML (potential attack vectors)
# 
#     def __init__(self, zip_file):
#         HTMLParser.__init__(self)
#         self._html = StringIO()  # buffer for the processed HTML
#         self._zip_file = zip_file
# 
#     def set_src_source(self, zip_file):
#         self._zip_file = zip_file
# 
#     def handle_starttag(self, tag, attrs):
#         if tag == 'script' or tag == 'object':  # filter scripts and objects (attack vectors)
#             return
# 
#         self._html.write('<')
#         self._html.write(tag)
# 
#         for attr in attrs:
#             self._html.write(' ')
#             self._html.write(attr[0])
#             if attr[1] is not None:
#                 self._html.write('="')
#                 if attr[0] == 'src':
#                     self._insert_data_uri(attr[1])
#                 else:
#                     self._html.write(attr[1])
# 
#                 self._html.write('"')
# 
#         self._html.write('>')
# 
#     def _insert_data_uri(self, src):
#         with self._zip_file.open(src) as src_file:
#             src_data = src_file.read()
#             src_b64 = base64.b64encode(src_data)
# 
#             self._html.write('data:image/png;base64,')
#             self._html.write(src_b64.decode('utf-8'))
# 
#     def handle_endtag(self, tag):
#         if tag == 'script' or tag == 'object':
#             return
# 
#         self._html.write('</')
#         self._html.write(tag)
#         self._html.write('>')
# 
#     def handle_data(self, data):
#         self._html.write(data)
# 
#     def final_html(self):
#         return self._html.getvalue()


# Path: mfr/extensions/jasp/render.py
import os
from mako.lookup import TemplateLookup
from mfr.core import extension
from mfr.extensions.jasp import exceptions
from zipfile import ZipFile, BadZipFile
from distutils.version import LooseVersion
from .html_processor import HTMLProcessor




class JASPRenderer(extension.BaseRenderer):

    # Minimum data archive version supported
    MINIMUM_VERSION = LooseVersion('1.0.2')

    TEMPLATE = TemplateLookup(
        directories=[
            os.path.join(os.path.dirname(__file__), 'templates')
        ]).get_template('viewer.mako')

    MESSAGE_FILE_CORRUPT = 'This JASP file is corrupt and cannot be viewed.'

    def render(self):
        try:
            with ZipFile(self.file_path) as zip_file:
                self._check_file(zip_file)
                body = self._render_html(zip_file, self.metadata.ext)
                return self.TEMPLATE.render(base=self.assets_url, body=body)
        except BadZipFile as err:

            raise exceptions.JaspFileCorruptError(