예제 #1
0
    def test_public_file_metadata(self, root_provider_fixtures):
        item = root_provider_fixtures['file_article_metadata']
        public_metadata = FigshareFileMetadata(item, item['files'][0])
        public_metadata.raw['url'] = 'https://api.figshare.com/v2'

        assert public_metadata.name == 'file'
        assert public_metadata.is_public is True
        assert public_metadata.web_view == 'https://figshare.com/articles/4037952'
예제 #2
0
    def test_metadata_article_identifier(self, root_provider_fixtures):
        item = root_provider_fixtures['file_article_metadata']
        article_metadata = FigshareFileMetadata(item, item['files'][0])
        article_metadata.raw['url'] = 'https://api.figshare.com/v2/account/articles/'

        assert article_metadata.article_name == ''
        assert article_metadata.path == '/6530715'
        assert article_metadata.materialized_path == '/file'
    def test_metadata_article_identifier(self, root_provider_fixtures):
        item = root_provider_fixtures['file_article_metadata']
        article_metadata = FigshareFileMetadata(item, item['files'][0])
        article_metadata.raw['url'] = 'https://api.figshare.com/v2/account/articles/'

        assert article_metadata.article_name == ''
        assert article_metadata.path == '/6530715'
        assert article_metadata.materialized_path == '/file'
    def test_public_file_metadata(self, root_provider_fixtures):
        item = root_provider_fixtures['file_article_metadata']
        public_metadata = FigshareFileMetadata(item, item['files'][0])
        public_metadata.raw['url'] = 'https://api.figshare.com/v2'

        assert public_metadata.name == 'file'
        assert public_metadata.is_public is True
        assert public_metadata.web_view == 'https://figshare.com/articles/4037952'
예제 #5
0
    def test_metadata_article_identifier(self, project_article_type_1_metadata):
        item = project_article_type_1_metadata['private']
        article_metadata = FigshareFileMetadata(item, item['files'][0])
        article_metadata.raw['url'] = 'https://api.figshare.com/v2/account/articles/8263730'

        assert article_metadata.id == 15562817
        assert article_metadata.name == 'FigurePrivate01.png'
        assert article_metadata.article_id == 8305859
        assert article_metadata.article_name == ''
        assert article_metadata.path == '/15562817'
        assert article_metadata.materialized_path == '/FigurePrivate01.png'
예제 #6
0
    async def metadata(self, path, **kwargs):
        """Return metadata for entity identified by ``path`` under the parent project.

        :param FigsharePath path: entity whose metadata will be returned
        :rtype: FigshareFileMetadata obj or list of Metadata objs
        """
        if path.is_root:
            # TODO: fix this similar to `validate_path_v1()`
            # NOTE: this won't work for figshare collections since calling `_get_article_metadata()`
            #       on items not owned by the OSF/auth user will fail with 403.
            path.is_public = False
            contents = await asyncio.gather(*[
                # TODO: collections may need to use each['url'] for correct URN
                # Use _get_url_super ? figshare API needs to get fixed first.
                self._get_article_metadata(str(each['id']), path.is_public)
                for each in await self._get_all_articles()
            ])
            return [each for each in contents if each]

        if not path.parts[-1].identifier:
            raise exceptions.NotFoundError(str(path))
        if len(path.parts) > 3:
            raise exceptions.NotFoundError(str(path))
        article_response = await self.make_request(
            'GET',
            self.build_url(path.is_public, *self.root_path_parts,
                           'articles', path.parts[1].identifier),
            expects=(200, 404),
        )
        if article_response.status == 404:
            raise exceptions.NotFoundError(str(path))
        article_json = await article_response.json()

        if len(path.parts) == 2:
            if article_json['defined_type'] not in pd_settings.FOLDER_TYPES:
                raise exceptions.NotFoundError(str(path))
            contents = []
            for file in article_json['files']:
                contents.append(FigshareFileMetadata(article_json, raw_file=file))
            return contents
        elif len(path.parts) == 3:
            for file in article_json['files']:
                if file['id'] == int(path.parts[2].identifier):
                    return FigshareFileMetadata(article_json, raw_file=file)
            raise exceptions.NotFoundError(path.path)
        else:
            raise exceptions.NotFoundError('{} is not a valid path.'.format(path))
예제 #7
0
    def test_public_file_metadata(self, project_article_type_1_metadata):
        item = project_article_type_1_metadata['public']
        public_metadata = FigshareFileMetadata(item, item['files'][0])

        assert public_metadata.id == 15451592
        assert public_metadata.name == 'Figure01.png'
        assert public_metadata.article_id == 8263730
        assert public_metadata.article_name == 'Figure'
        assert public_metadata.is_public is True
        assert public_metadata.web_view == 'https://figshare.com/articles/Figure01_png/8263730'
        assert public_metadata.extra.get('status') == 'public'
예제 #8
0
    async def metadata(self, path, **kwargs):
        """Return metadata for entity identified by ``path``. May be the containing article or
        a file in a fileset article.

        :param FigsharePath path: entity whose metadata will be returned
        :rtype FigshareFileMetadata obj or list of Metadata objs:
        """
        article = await self._get_article(not path.is_public)
        if path.is_root:  # list files in article
            contents = []
            for file in article['files']:
                contents.append(FigshareFileMetadata(article, raw_file=file))
            return contents
        elif len(path.parts) == 2:  # metadata for a particular file
            for file in article['files']:
                if str(file['id']) == path.parts[1].identifier:
                    return FigshareFileMetadata(article, raw_file=file)

        # Invalid path, e.g. /422313/67709/1234
        raise exceptions.NotFoundError(str(path))
예제 #9
0
    async def _get_article_metadata(self, article_id, is_public: bool):
        """Return Figshare*Metadata object for given article_id. Returns a FolderMetadata object
        for filesets, a FileMetadat object for other article types, and ``None`` if the article
        is not a fileset and has no files attached.

        Defined separately to allow for taking advantage of ``asyncio.gather``.

        :param str article_id: id of article whose metadata is requested
        :param bool is_public: ``True`` if article is accessed through public URN
        """
        response = await self.make_request(
            'GET',
            self.build_url(is_public, *self.root_path_parts, 'articles', article_id),
            expects=(200, ),
        )
        article_json = await response.json()
        if article_json['defined_type'] in pd_settings.FOLDER_TYPES:
            return FigshareFolderMetadata(article_json)
        elif article_json['files']:
            return FigshareFileMetadata(article_json)

        return None  # article without attached file
예제 #10
0
    def test_private_file_metadata(self, project_article_type_1_metadata):
        base_meta = project_article_type_1_metadata['private']
        data = FigshareFileMetadata(base_meta, base_meta['files'][0])

        assert data.id == 15562817
        assert data.name == 'FigurePrivate01.png'
        assert data.article_id == 8305859
        assert data.article_name == 'FigurePrivate'
        assert data.path == '/8305859/15562817'
        assert data.materialized_path == '/FigurePrivate/FigurePrivate01.png'
        assert data.upload_path == '/8305859/15562817'
        assert data.size == 89281
        assert data.size_as_int == 89281
        assert type(data.size_as_int) == int

        assert data.content_type is None
        assert data.modified is None
        assert data.modified_utc is None
        assert data.created_utc is None
        assert data.can_delete is True
        assert data.is_public is False
        assert data.etag == 'draft:8305859:cae3869aa4b144a3aa5cffe979359836'
        assert data.web_view == 'https://figshare.com/account/articles/8305859'
        assert data.extra == {
            'fileId': 15562817,
            'articleId': 8305859,
            'status': 'draft',
            'downloadUrl': 'https://ndownloader.figshare.com/files/15562817',
            'canDelete': True,
            'webView': 'https://figshare.com/account/articles/8305859',
            'hashingInProgress': False,
            'hashes': {
                'md5': 'cae3869aa4b144a3aa5cffe979359836'
            }
        }

        assert data.kind == 'file'
        assert data.serialized() == {
            'extra': {
                'fileId': 15562817,
                'articleId': 8305859,
                'status': 'draft',
                'downloadUrl': 'https://ndownloader.figshare.com/files/15562817',
                'canDelete': True,
                'webView': 'https://figshare.com/account/articles/8305859',
                'hashingInProgress': False,
                'hashes': {
                    'md5': 'cae3869aa4b144a3aa5cffe979359836'
                }
            },
            'kind': 'file',
            'name': 'FigurePrivate01.png',
            'path': '/8305859/15562817',
            'provider': 'figshare',
            'materialized': '/FigurePrivate/FigurePrivate01.png',
            'etag': '0113713d8af08db5fb6a0f6565b115d98d0f6c284b808a58997f1e73bdec397e',
            'contentType': None,
            'modified': None,
            'modified_utc': None,
            'created_utc': None,
            'size': 89281,
            'sizeInt': 89281,
        }

        api_url = 'http://localhost:7777/v1/resources/cn42d/providers/figshare/8305859/15562817'
        assert data.json_api_serialized('cn42d') == {
            'id': 'figshare/8305859/15562817',
            'type': 'files',
            'attributes': {
                'extra': {
                    'fileId': 15562817,
                    'articleId': 8305859,
                    'status': 'draft',
                    'downloadUrl': 'https://ndownloader.figshare.com/files/15562817',
                    'canDelete': True,
                    'webView': 'https://figshare.com/account/articles/8305859',
                    'hashingInProgress': False,
                    'hashes': {
                        'md5': 'cae3869aa4b144a3aa5cffe979359836'
                    }
                },
                'kind': 'file',
                'name': 'FigurePrivate01.png',
                'path': '/8305859/15562817',
                'provider': 'figshare',
                'materialized': '/FigurePrivate/FigurePrivate01.png',
                'etag': '0113713d8af08db5fb6a0f6565b115d98d0f6c284b808a58997f1e73bdec397e',
                'contentType': None,
                'modified': None,
                'modified_utc': None,
                'created_utc': None,
                'size': 89281,
                'sizeInt': 89281,
                'resource': 'cn42d'
            },
            'links': {
                'move': api_url,
                'upload': '{}?kind=file'.format(api_url),
                'delete': api_url,
                'download': api_url,
            }
        }

        assert data._json_api_links('cn42d') == {
            'move': api_url,
            'upload': '{}?kind=file'.format(api_url),
            'delete': api_url,
            'download': api_url,
        }
예제 #11
0
    def test_file_metadata(self, root_provider_fixtures):
        base_meta = root_provider_fixtures['file_article_metadata']
        data = FigshareFileMetadata(
            base_meta,
            base_meta['files'][0]
        )

        assert data.id == 6530715
        assert data.name == 'file'
        assert data.article_id == 4037952
        assert data.article_name == 'file_article'
        assert data.path == '/4037952/6530715'
        assert data.materialized_path == '/file_article/file'
        assert data.upload_path == '/4037952/6530715'
        assert data.size == 7
        assert data.content_type is None
        assert data.modified is None
        assert data.modified_utc is None
        assert data.created_utc is None
        assert data.can_delete is True
        assert data.is_public is False
        assert data.etag == 'draft:4037952:b3e656f8b0828a31f3ed396a1c868786'
        assert data.web_view == 'https://figshare.com/account/articles/4037952'
        assert data.extra == {
            'fileId': 6530715,
            'articleId': 4037952,
            'status': 'draft',
            'downloadUrl': 'https://ndownloader.figshare.com/files/6530715',
            'canDelete': True,
            'webView': 'https://figshare.com/account/articles/4037952',
            'hashes': {
                'md5': 'b3e656f8b0828a31f3ed396a1c868786'
            }
        }

        assert data.kind == 'file'
        assert data.serialized() == {
            'extra': {
                'fileId': 6530715,
                'articleId': 4037952,
                'status': 'draft',
                'downloadUrl': 'https://ndownloader.figshare.com/files/6530715',
                'canDelete': True,
                'webView': 'https://figshare.com/account/articles/4037952',
                'hashes': {
                    'md5': 'b3e656f8b0828a31f3ed396a1c868786'
                }
            },
            'kind': 'file',
            'name': 'file',
            'path': '/4037952/6530715',
            'provider': 'figshare',
            'materialized': '/file_article/file',
            'etag': 'c22302dc6826efe0a70f9528a3edd3ec3af340d52ac248b2bf23fd64d6ffea8d',
            'contentType': None,
            'modified': None,
            'modified_utc': None,
            'created_utc': None,
            'size': 7
        }

        api_url = 'http://localhost:7777/v1/resources/cn42d/providers/figshare/4037952/6530715'
        assert data.json_api_serialized('cn42d') == {
            'id': 'figshare/4037952/6530715',
            'type': 'files',
            'attributes': {
                'extra': {
                    'fileId': 6530715,
                    'articleId': 4037952,
                    'status': 'draft',
                    'downloadUrl': 'https://ndownloader.figshare.com/files/6530715',
                    'canDelete': True,
                    'webView': 'https://figshare.com/account/articles/4037952',
                    'hashes': {
                        'md5': 'b3e656f8b0828a31f3ed396a1c868786'
                    }
                },
                'kind': 'file',
                'name': 'file',
                'path': '/4037952/6530715',
                'provider': 'figshare',
                'materialized': '/file_article/file',
                'etag': 'c22302dc6826efe0a70f9528a3edd3ec3af340d52ac248b2bf23fd64d6ffea8d',
                'contentType': None,
                'modified': None,
                'modified_utc': None,
                'created_utc': None,
                'size': 7,
                'resource': 'cn42d'
            },
            'links': {
                'move': api_url,
                'upload': '{}?kind=file'.format(api_url),
                'delete': api_url,
                'download': api_url,
            }
        }

        assert data._json_api_links('cn42d') == {
            'move': api_url,
            'upload': '{}?kind=file'.format(api_url),
            'delete': api_url,
            'download': api_url,
        }
    def test_file_metadata(self, root_provider_fixtures):
        base_meta = root_provider_fixtures['file_article_metadata']
        data = FigshareFileMetadata(
            base_meta,
            base_meta['files'][0]
        )

        assert data.id == 6530715
        assert data.name == 'file'
        assert data.article_id == 4037952
        assert data.article_name == 'file_article'
        assert data.path == '/4037952/6530715'
        assert data.materialized_path == '/file_article/file'
        assert data.upload_path == '/4037952/6530715'
        assert data.size == 7
        assert data.size_as_int == 7
        assert type(data.size_as_int) == int

        assert data.content_type is None
        assert data.modified is None
        assert data.modified_utc is None
        assert data.created_utc is None
        assert data.can_delete is True
        assert data.is_public is False
        assert data.etag == 'draft:4037952:b3e656f8b0828a31f3ed396a1c868786'
        assert data.web_view == 'https://figshare.com/account/articles/4037952'
        assert data.extra == {
            'fileId': 6530715,
            'articleId': 4037952,
            'status': 'draft',
            'downloadUrl': 'https://ndownloader.figshare.com/files/6530715',
            'canDelete': True,
            'webView': 'https://figshare.com/account/articles/4037952',
            'hashes': {
                'md5': 'b3e656f8b0828a31f3ed396a1c868786'
            }
        }

        assert data.kind == 'file'
        assert data.serialized() == {
            'extra': {
                'fileId': 6530715,
                'articleId': 4037952,
                'status': 'draft',
                'downloadUrl': 'https://ndownloader.figshare.com/files/6530715',
                'canDelete': True,
                'webView': 'https://figshare.com/account/articles/4037952',
                'hashes': {
                    'md5': 'b3e656f8b0828a31f3ed396a1c868786'
                }
            },
            'kind': 'file',
            'name': 'file',
            'path': '/4037952/6530715',
            'provider': 'figshare',
            'materialized': '/file_article/file',
            'etag': 'c22302dc6826efe0a70f9528a3edd3ec3af340d52ac248b2bf23fd64d6ffea8d',
            'contentType': None,
            'modified': None,
            'modified_utc': None,
            'created_utc': None,
            'size': 7,
            'sizeInt': 7,
        }

        api_url = 'http://localhost:7777/v1/resources/cn42d/providers/figshare/4037952/6530715'
        assert data.json_api_serialized('cn42d') == {
            'id': 'figshare/4037952/6530715',
            'type': 'files',
            'attributes': {
                'extra': {
                    'fileId': 6530715,
                    'articleId': 4037952,
                    'status': 'draft',
                    'downloadUrl': 'https://ndownloader.figshare.com/files/6530715',
                    'canDelete': True,
                    'webView': 'https://figshare.com/account/articles/4037952',
                    'hashes': {
                        'md5': 'b3e656f8b0828a31f3ed396a1c868786'
                    }
                },
                'kind': 'file',
                'name': 'file',
                'path': '/4037952/6530715',
                'provider': 'figshare',
                'materialized': '/file_article/file',
                'etag': 'c22302dc6826efe0a70f9528a3edd3ec3af340d52ac248b2bf23fd64d6ffea8d',
                'contentType': None,
                'modified': None,
                'modified_utc': None,
                'created_utc': None,
                'size': 7,
                'sizeInt': 7,
                'resource': 'cn42d'
            },
            'links': {
                'move': api_url,
                'upload': '{}?kind=file'.format(api_url),
                'delete': api_url,
                'download': api_url,
            }
        }

        assert data._json_api_links('cn42d') == {
            'move': api_url,
            'upload': '{}?kind=file'.format(api_url),
            'delete': api_url,
            'download': api_url,
        }