archive.py 4.67 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
import nomad_meta_info
26
from nomadcore.local_meta_info import load_metainfo
27

28
from nomad.files import UploadFiles, Restricted
29

30
from .app import api
31
32
from .auth import login_if_available, create_authorization_predicate, \
    signature_token_argument, with_signature_token
33
34
35
from .common import calc_route

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


40
41
42
43
archive_file_request_parser = api.parser()
archive_file_request_parser.add_argument(**signature_token_argument)


44
45
@calc_route(ns, '/logs')
class ArchiveCalcLogResource(Resource):
46
    @api.doc('get_archive_logs')
47
    @api.response(404, 'The upload or calculation does not exist')
48
    @api.response(401, 'Not authorized to access the data.')
49
    @api.response(200, 'Archive data send', headers={'Content-Type': 'application/plain'})
50
    @api.expect(archive_file_request_parser, validate=True)
51
    @login_if_available
52
    @with_signature_token
53
    def get(self, upload_id, calc_id):
54
55
56
        """
        Get calculation processing log.

57
        Calcs are references via *upload_id*, *calc_id* pairs.
58
        """
59
        archive_id = '%s/%s' % (upload_id, calc_id)
60

61
        upload_files = UploadFiles.get(
62
            upload_id, is_authorized=create_authorization_predicate(upload_id, calc_id))
63

64
        if upload_files is None:
65
            abort(404, message='Upload %s does not exist.' % upload_id)
66
67
68

        try:
            return send_file(
69
                upload_files.archive_log_file(calc_id, 'rb'),
70
71
72
73
                mimetype='text/plain',
                as_attachment=True,
                attachment_filename='%s.log' % archive_id)
        except Restricted:
74
            abort(401, message='Not authorized to access %s/%s.' % (upload_id, calc_id))
75
76
        except KeyError:
            abort(404, message='Calculation %s does not exist.' % archive_id)
77
78
79
80


@calc_route(ns)
class ArchiveCalcResource(Resource):
81
    @api.doc('get_archive_calc')
82
    @api.response(404, 'The upload or calculation does not exist')
83
    @api.response(401, 'Not authorized to access the data.')
84
    @api.response(200, 'Archive data send')
85
    @api.expect(archive_file_request_parser, validate=True)
86
    @login_if_available
87
    @with_signature_token
88
    def get(self, upload_id, calc_id):
89
90
91
        """
        Get calculation data in archive form.

92
        Calcs are references via *upload_id*, *calc_id* pairs.
93
        """
94
        archive_id = '%s/%s' % (upload_id, calc_id)
95

96
        upload_file = UploadFiles.get(
97
            upload_id, is_authorized=create_authorization_predicate(upload_id, calc_id))
98

99
        if upload_file is None:
100
            abort(404, message='Archive %s does not exist.' % upload_id)
101
102
103

        try:
            return send_file(
104
                upload_file.archive_file(calc_id, 'rb'),
105
106
107
108
                mimetype='application/json',
                as_attachment=True,
                attachment_filename='%s.json' % archive_id)
        except Restricted:
109
            abort(401, message='Not authorized to access %s/%s.' % (upload_id, calc_id))
110
        except KeyError:
111
            abort(404, message='Calculation %s does not exist.' % archive_id)
112
113
114


@ns.route('/metainfo/<string:metainfo_path>')
115
@api.doc(params=dict(nomad_metainfo_path='A path or metainfo definition file name.'))
116
117
118
119
120
121
122
123
124
125
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__))
126
            metainfo_file = os.path.normpath(os.path.join(file_dir, metainfo_path.strip()))
127

128
129
            meta_info, _ = load_metainfo(metainfo_file)
            return meta_info.toJsonList(False), 200
130
131
        except FileNotFoundError:
            abort(404, message='The metainfo %s does not exist.' % metainfo_path)