Skip to content

Commit 794bee6

Browse files
committed
Merge branch 'feature/jasp-new-manifest' into develop
Closes: #372
2 parents eff2d90 + 8ab1e69 commit 794bee6

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

mfr/extensions/jasp/render.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23

34
from mako.lookup import TemplateLookup
@@ -67,16 +68,30 @@ def _check_file(self, zip_file):
6768
"""
6869
# Extract manifest file content
6970
try:
70-
with zip_file.open('META-INF/MANIFEST.MF') as manifest_data:
71-
manifest = manifest_data.read().decode('utf-8')
71+
try:
72+
# new manifest location
73+
with zip_file.open('manifest.json') as manifest_data:
74+
manifest, flavor = manifest_data.read().decode('utf-8'), 'json'
75+
except KeyError:
76+
# old manifest location
77+
with zip_file.open('META-INF/MANIFEST.MF') as manifest_data:
78+
manifest, flavor = manifest_data.read().decode('utf-8'), 'java'
7279
except KeyError:
7380
raise exceptions.JaspFileCorruptError(
74-
'{} Missing META-INF/MANIFEST.MF'.format(self.MESSAGE_FILE_CORRUPT),
81+
'{} Missing manifest'.format(self.MESSAGE_FILE_CORRUPT),
7582
extension=self.metadata.ext,
7683
corruption_type='key_error',
77-
reason='zip missing ./META-INF/MANIFEST.MF',
84+
reason='zip missing manifest',
7885
)
7986

87+
if flavor == 'java':
88+
self._verify_java_manifest(manifest)
89+
else:
90+
self._verify_json_manifest(manifest)
91+
92+
return True
93+
94+
def _verify_java_manifest(self, manifest):
8095
lines = manifest.split('\n')
8196

8297
# Search for Data-Archive-Version
@@ -121,4 +136,41 @@ def _check_file(self, zip_file):
121136
reason='Data-Archive-Version ({}) not parsable.'.format(dataArchiveVersionStr),
122137
)
123138

124-
return True
139+
return
140+
141+
def _verify_json_manifest(self, manifest):
142+
143+
manifest_data = json.loads(manifest)
144+
145+
jasp_archive_version_str = manifest_data.get('jaspArchiveVersion', None)
146+
if not jasp_archive_version_str:
147+
raise exceptions.JaspFileCorruptError(
148+
'{} jaspArchiveVersion not found.'.format(self.MESSAGE_FILE_CORRUPT),
149+
extension=self.metadata.ext,
150+
corruption_type='manifest_parse_error',
151+
reason='jaspArchiveVersion not found.',
152+
)
153+
154+
# Check that the file is new enough (contains preview content)
155+
jasp_archive_version = LooseVersion(jasp_archive_version_str)
156+
try:
157+
if jasp_archive_version < self.MINIMUM_VERSION:
158+
minimum_version = self.MINIMUM_VERSION.vstring
159+
data_archive_version = jasp_archive_version.vstring
160+
raise exceptions.JaspVersionError(
161+
'This JASP file was created with an older data archive '
162+
'version ({}) and cannot be previewed. Minimum data archive '
163+
'version is {}.'.format(data_archive_version, minimum_version),
164+
extension=self.metadata.ext,
165+
actual_version=data_archive_version,
166+
required_version=minimum_version,
167+
)
168+
except TypeError:
169+
raise exceptions.JaspFileCorruptError(
170+
'{} jaspArchiveVersion not parsable.'.format(self.MESSAGE_FILE_CORRUPT),
171+
extension=self.metadata.ext,
172+
corruption_type='manifest_parse_error',
173+
reason='jaspArchiveVersion ({}) not parsable.'.format(jasp_archive_version_str),
174+
)
175+
176+
return
2.06 MB
Binary file not shown.

tests/extensions/jasp/test_renderer.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ def metadata():
1111
return ProviderMetadata('JASP', '.jasp', 'application/octet-stream', '1234', 'http://wb.osf.io/file/JASP.jasp?token=1234')
1212

1313
@pytest.fixture
14-
def ok_path():
15-
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'files', 'ok.jasp')
14+
def ok_old_manifest_path():
15+
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'files', 'ok-old-manifest.jasp')
16+
17+
@pytest.fixture
18+
def ok_new_manifest_path():
19+
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'files', 'ok-new-manifest.jasp')
1620

1721
@pytest.fixture
1822
def not_a_zip_file_path():
@@ -60,8 +64,8 @@ def extension():
6064

6165

6266
@pytest.fixture
63-
def renderer(metadata, ok_path, url, assets_url, export_url):
64-
return JASPRenderer(metadata, ok_path, url, assets_url, export_url)
67+
def renderer(metadata, ok_new_manifest_path, url, assets_url, export_url):
68+
return JASPRenderer(metadata, ok_new_manifest_path, url, assets_url, export_url)
6569

6670

6771
class TestCodeJASPRenderer:
@@ -70,6 +74,11 @@ def test_render_JASP(self, renderer):
7074
body = renderer.render()
7175
assert '<div style="word-wrap: break-word; overflow: auto;" class="mfrViewer">' in body
7276

77+
def test_render_JASP_old_manifest(self, metadata, ok_old_manifest_path, url, assets_url, export_url):
78+
renderer = JASPRenderer(metadata, ok_old_manifest_path, url, assets_url, export_url)
79+
body = renderer.render()
80+
assert '<div style="word-wrap: break-word; overflow: auto;" class="mfrViewer">' in body
81+
7382
def test_render_JASP_not_a_zip_file(self, metadata, not_a_zip_file_path, url, assets_url, export_url):
7483
try:
7584
renderer = JASPRenderer(metadata, not_a_zip_file_path, url, assets_url, export_url)

0 commit comments

Comments
 (0)