|
| 1 | +from distutils.version import LooseVersion |
| 2 | +import os |
| 3 | +from zipfile import ZipFile, BadZipFile |
| 4 | + |
| 5 | +from mako.lookup import TemplateLookup |
| 6 | + |
| 7 | +from mfr.core import extension |
| 8 | +from mfr.extensions.jamovi import exceptions as jamovi_exceptions |
| 9 | +from mfr.extensions.jamovi.html_processor import HTMLProcessor |
| 10 | + |
| 11 | + |
| 12 | +class JamoviRenderer(extension.BaseRenderer): |
| 13 | + |
| 14 | + # Minimum data archive version supported |
| 15 | + MINIMUM_VERSION = LooseVersion('1.0.2') |
| 16 | + |
| 17 | + TEMPLATE = TemplateLookup( |
| 18 | + directories=[ |
| 19 | + os.path.join(os.path.dirname(__file__), 'templates') |
| 20 | + ]).get_template('viewer.mako') |
| 21 | + |
| 22 | + MESSAGE_FILE_CORRUPT = 'This jamovi file is corrupt and cannot be viewed.' |
| 23 | + MESSAGE_NO_PREVIEW = 'This jamovi file does not support previews.' |
| 24 | + |
| 25 | + def render(self): |
| 26 | + try: |
| 27 | + with ZipFile(self.file_path) as zip_file: |
| 28 | + self._check_file(zip_file) |
| 29 | + body = self._render_html(zip_file, self.metadata.ext) |
| 30 | + return self.TEMPLATE.render(base=self.assets_url, body=body) |
| 31 | + except BadZipFile as err: |
| 32 | + raise jamovi_exceptions.JamoviRendererError( |
| 33 | + '{} {}.'.format(self.MESSAGE_FILE_CORRUPT, str(err)), |
| 34 | + extension=self.metadata.ext, |
| 35 | + corruption_type='bad_zip', |
| 36 | + reason=str(err), |
| 37 | + ) |
| 38 | + |
| 39 | + @property |
| 40 | + def file_required(self): |
| 41 | + return True |
| 42 | + |
| 43 | + @property |
| 44 | + def cache_result(self): |
| 45 | + return True |
| 46 | + |
| 47 | + def _render_html(self, zip_file, ext, *args, **kwargs): |
| 48 | + index = None |
| 49 | + try: |
| 50 | + with zip_file.open('index.html') as index_data: |
| 51 | + index = index_data.read().decode('utf-8') |
| 52 | + except KeyError: |
| 53 | + raise jamovi_exceptions.JamoviRendererError( |
| 54 | + self.MESSAGE_NO_PREVIEW, |
| 55 | + ) |
| 56 | + |
| 57 | + processor = HTMLProcessor(zip_file) |
| 58 | + processor.feed(index) |
| 59 | + |
| 60 | + return processor.final_html() |
| 61 | + |
| 62 | + def _check_file(self, zip_file): |
| 63 | + """Check if the file is OK (not corrupt) |
| 64 | + :param zip_file: an opened ZipFile representing the jamovi file |
| 65 | + :return: True |
| 66 | + """ |
| 67 | + # Extract manifest file content |
| 68 | + try: |
| 69 | + with zip_file.open('META-INF/MANIFEST.MF') as manifest_data: |
| 70 | + manifest = manifest_data.read().decode('utf-8') |
| 71 | + except KeyError: |
| 72 | + raise jamovi_exceptions.JamoviFileCorruptError( |
| 73 | + '{} Missing META-INF/MANIFEST.MF'.format(self.MESSAGE_FILE_CORRUPT), |
| 74 | + extension=self.metadata.ext, |
| 75 | + corruption_type='key_error', |
| 76 | + reason='zip missing ./META-INF/MANIFEST.MF', |
| 77 | + ) |
| 78 | + |
| 79 | + lines = manifest.split('\n') |
| 80 | + |
| 81 | + # Search for Data-Archive-Version |
| 82 | + version_str = None |
| 83 | + for line in lines: |
| 84 | + key_value = line.split(':') |
| 85 | + if len(key_value) == 2 and key_value[0].strip() == 'Data-Archive-Version': |
| 86 | + version_str = key_value[1].strip() |
| 87 | + break |
| 88 | + else: |
| 89 | + raise jamovi_exceptions.JamoviFileCorruptError( |
| 90 | + '{} Data-Archive-Version not found.'.format(self.MESSAGE_FILE_CORRUPT), |
| 91 | + extension=self.metadata.ext, |
| 92 | + corruption_type='manifest_parse_error', |
| 93 | + reason='Data-Archive-Version not found.', |
| 94 | + ) |
| 95 | + |
| 96 | + # Check that the file is new enough (contains preview content) |
| 97 | + archive_version = LooseVersion(version_str) |
| 98 | + try: |
| 99 | + if archive_version < self.MINIMUM_VERSION: |
| 100 | + raise jamovi_exceptions.JamoviFileCorruptError( |
| 101 | + '{} Data-Archive-Version is too old.'.format(self.MESSAGE_FILE_CORRUPT), |
| 102 | + extension=self.metadata.ext, |
| 103 | + corruption_type='manifest_parse_error', |
| 104 | + reason='Data-Archive-Version not found.', |
| 105 | + ) |
| 106 | + except TypeError: |
| 107 | + raise jamovi_exceptions.JamoviFileCorruptError( |
| 108 | + '{} Data-Archive-Version not parsable.'.format(self.MESSAGE_FILE_CORRUPT), |
| 109 | + extension=self.metadata.ext, |
| 110 | + corruption_type='manifest_parse_error', |
| 111 | + reason='Data-Archive-Version ({}) not parsable.'.format(version_str), |
| 112 | + ) |
| 113 | + |
| 114 | + return True |
0 commit comments