archive.py 4.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
16
17
18
19
"""
The archive API of the nomad@FAIRDI APIs. This API is about serving processed
(parsed and normalized) calculation data in nomad's *meta-info* format.
"""

20
21
22
import os.path

from flask import send_file
23
from flask_restplus import abort, Resource
24

25
26
import nomad_meta_info

27
from nomad.files import UploadFiles, Restricted
28

29
from .app import api
30
from .auth import login_if_available, create_authorization_predicate
31
32
33
from .common import calc_route

ns = api.namespace(
34
35
    'archive',
    description='Access archive data and archive processing logs.')
36
37
38
39


@calc_route(ns, '/logs')
class ArchiveCalcLogResource(Resource):
40
    @api.doc('get_archive_logs')
41
    @api.response(404, 'The upload or calculation does not exist')
42
    @api.response(401, 'Not authorized to access the data.')
43
    @api.response(200, 'Archive data send', headers={'Content-Type': 'application/plain'})
44
    @login_if_available
45
    def get(self, upload_id, calc_id):
46
47
48
        """
        Get calculation processing log.

49
        Calcs are references via *upload_id*, *calc_id* pairs.
50
        """
51
        archive_id = '%s/%s' % (upload_id, calc_id)
52

53
        upload_files = UploadFiles.get(
54
            upload_id, is_authorized=create_authorization_predicate(upload_id, calc_id))
55

56
        if upload_files is None:
57
            abort(404, message='Upload %s does not exist.' % upload_id)
58
59
60

        try:
            return send_file(
61
                upload_files.archive_log_file(calc_id, 'rt'),
62
63
64
65
                mimetype='text/plain',
                as_attachment=True,
                attachment_filename='%s.log' % archive_id)
        except Restricted:
66
            abort(401, message='Not authorized to access %s/%s.' % (upload_id, calc_id))
67
68
        except KeyError:
            abort(404, message='Calculation %s does not exist.' % archive_id)
69
70
71
72


@calc_route(ns)
class ArchiveCalcResource(Resource):
73
    @api.doc('get_archive_calc')
74
    @api.response(404, 'The upload or calculation does not exist')
75
    @api.response(401, 'Not authorized to access the data.')
76
77
    @api.response(200, 'Archive data send')
    @login_if_available
78
    def get(self, upload_id, calc_id):
79
80
81
        """
        Get calculation data in archive form.

82
        Calcs are references via *upload_id*, *calc_id* pairs.
83
        """
84
        archive_id = '%s/%s' % (upload_id, calc_id)
85

86
        upload_file = UploadFiles.get(
87
            upload_id, is_authorized=create_authorization_predicate(upload_id, calc_id))
88

89
        if upload_file is None:
90
            abort(404, message='Archive %s does not exist.' % upload_id)
91
92
93

        try:
            return send_file(
94
                upload_file.archive_file(calc_id, 'rt'),
95
96
97
98
                mimetype='application/json',
                as_attachment=True,
                attachment_filename='%s.json' % archive_id)
        except Restricted:
99
            abort(401, message='Not authorized to access %s/%s.' % (upload_id, calc_id))
100
        except KeyError:
101
            abort(404, message='Calculation %s does not exist.' % archive_id)
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126


@ns.route('/metainfo/<string:metainfo_path>')
@api.doc(params=dict(metainfo_path='A path or metainfo definition file name.'))
class MetainfoResource(Resource):
    @api.doc('get_metainfo')
    @api.response(404, 'The metainfo does not exist')
    @api.response(200, 'Metainfo data send')
    def get(self, metainfo_path):
        """
        Get a metainfo definition file.
        """
        try:
            file_dir = os.path.dirname(os.path.abspath(nomad_meta_info.__file__))
            meta_info_path = os.path.normpath(os.path.join(file_dir, metainfo_path.strip()))

            rv = send_file(
                meta_info_path,
                mimetype='application/json',
                as_attachment=True,
                attachment_filename=os.path.basename(metainfo_path))

            return rv
        except FileNotFoundError:
            abort(404, message='The metainfo %s does not exist.' % metainfo_path)