|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright 2019 - Swiss Data Science Center (SDSC) |
| 4 | +# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and |
| 5 | +# Eidgenössische Technische Hochschule Zürich (ETHZ). |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +"""Zenodo API integration.""" |
| 19 | +import pathlib |
| 20 | +import re |
| 21 | +import urllib |
| 22 | +from urllib.parse import urlparse |
| 23 | + |
| 24 | +import attr |
| 25 | +import requests |
| 26 | + |
| 27 | +from renku.cli._providers.api import ProviderApi |
| 28 | +from renku.cli._providers.doi import DOIProvider |
| 29 | + |
| 30 | +ZENODO_BASE_URL = 'https://zenodo.org' |
| 31 | +ZENODO_BASE_PATH = 'api' |
| 32 | + |
| 33 | + |
| 34 | +def make_records_url(record_id): |
| 35 | + """Create URL to access record by ID.""" |
| 36 | + return urllib.parse.urljoin( |
| 37 | + ZENODO_BASE_URL, |
| 38 | + pathlib.posixpath.join(ZENODO_BASE_PATH, 'records', record_id) |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@attr.s |
| 43 | +class ZenodoFile: |
| 44 | + """Zenodo record file.""" |
| 45 | + |
| 46 | + checksum = attr.ib() |
| 47 | + links = attr.ib() |
| 48 | + bucket = attr.ib() |
| 49 | + key = attr.ib() |
| 50 | + size = attr.ib() |
| 51 | + type = attr.ib() |
| 52 | + |
| 53 | + @property |
| 54 | + def remote_url(self): |
| 55 | + """Get remote URL as ``urllib.ParseResult``.""" |
| 56 | + return urllib.parse.urlparse(self.links['self']) |
| 57 | + |
| 58 | + @property |
| 59 | + def name(self): |
| 60 | + """Get file name.""" |
| 61 | + return self.remote_url.path.split('/')[-1] |
| 62 | + |
| 63 | + |
| 64 | +@attr.s |
| 65 | +class ZenodoRecord: |
| 66 | + """Zenodo record.""" |
| 67 | + |
| 68 | + id = attr.ib() |
| 69 | + conceptrecid = attr.ib() |
| 70 | + |
| 71 | + doi = attr.ib() |
| 72 | + files = attr.ib() |
| 73 | + links = attr.ib() |
| 74 | + metadata = attr.ib() |
| 75 | + owners = attr.ib() |
| 76 | + revision = attr.ib() |
| 77 | + stats = attr.ib() |
| 78 | + |
| 79 | + created = attr.ib() |
| 80 | + updated = attr.ib() |
| 81 | + |
| 82 | + conceptdoi = attr.ib(default=None) |
| 83 | + _zenodo = attr.ib(kw_only=True, default=None) |
| 84 | + |
| 85 | + @property |
| 86 | + def last_version(self): |
| 87 | + """Check if record is at last possible version.""" |
| 88 | + return self.version['is_last'] |
| 89 | + |
| 90 | + @property |
| 91 | + def version(self): |
| 92 | + """Get record version.""" |
| 93 | + return self.metadata['relations']['version'][0] |
| 94 | + |
| 95 | + @property |
| 96 | + def display_version(self): |
| 97 | + """Get display version.""" |
| 98 | + return 'v{0}'.format(self.version['index']) |
| 99 | + |
| 100 | + @property |
| 101 | + def display_name(self): |
| 102 | + """Get record display name.""" |
| 103 | + return '{0}_{1}'.format( |
| 104 | + re.sub(r'\W+', '', self.metadata['title']).lower()[:16], |
| 105 | + self.display_version |
| 106 | + ) |
| 107 | + |
| 108 | + def get_files(self): |
| 109 | + """Get Zenodo files metadata as ``ZenodoFile``.""" |
| 110 | + if len(self.files) == 0: |
| 111 | + raise LookupError('no files have been found') |
| 112 | + |
| 113 | + return [ZenodoFile(**file_) for file_ in self.files] |
| 114 | + |
| 115 | + |
| 116 | +@attr.s |
| 117 | +class ZenodoProvider(ProviderApi): |
| 118 | + """zenodo.org registry API provider.""" |
| 119 | + |
| 120 | + is_doi = attr.ib(default=False) |
| 121 | + |
| 122 | + @staticmethod |
| 123 | + def record_id(uri): |
| 124 | + """Extract record id from uri.""" |
| 125 | + return urlparse(uri).path.split('/')[-1] |
| 126 | + |
| 127 | + def find_record(self, uri): |
| 128 | + """Retrieves a record from Zenodo. |
| 129 | +
|
| 130 | + :raises: ``LookupError`` |
| 131 | + :param uri: DOI or URL |
| 132 | + :return: ``ZenodoRecord`` |
| 133 | + """ |
| 134 | + if self.is_doi: |
| 135 | + return self.find_record_by_doi(uri) |
| 136 | + |
| 137 | + return self.get_record(uri) |
| 138 | + |
| 139 | + def find_record_by_doi(self, doi): |
| 140 | + """Resolve the DOI and make a record for the retrieved record id.""" |
| 141 | + doi = DOIProvider().find_record(doi) |
| 142 | + return self.get_record(ZenodoProvider.record_id(doi.URL)) |
| 143 | + |
| 144 | + def get_record(self, uri): |
| 145 | + """Retrieve record metadata and return ``ZenodoRecord``.""" |
| 146 | + record_id = ZenodoProvider.record_id(uri) |
| 147 | + response = requests.get(make_records_url(record_id)) |
| 148 | + if response.status_code != 200: |
| 149 | + raise LookupError('record not found') |
| 150 | + |
| 151 | + return ZenodoRecord(**response.json(), zenodo=self) |
0 commit comments