Beispiel #1
0
    async def intra_move(self,  # type: ignore
                         dest_provider: provider.BaseProvider,
                         src_path: wb_path.WaterButlerPath,
                         dest_path: wb_path.WaterButlerPath) \
                         -> typing.Tuple[BaseGoogleDriveMetadata, bool]:
        self.metrics.add('intra_move.destination_exists', dest_path.identifier is not None)
        if dest_path.identifier:
            await dest_provider.delete(dest_path)

        async with self.request(
            'PATCH',
            self.build_url('files', src_path.identifier),
            headers={
                'Content-Type': 'application/json'
            },
            data=json.dumps({
                'parents': [{
                    'id': dest_path.parent.identifier
                }],
                'title': dest_path.name
            }),
            expects=(200, ),
            throws=exceptions.IntraMoveError,
        ) as resp:
            data = await resp.json()

        created = dest_path.identifier is None
        dest_path.parts[-1]._id = data['id']

        if dest_path.is_dir:
            metadata = GoogleDriveFolderMetadata(data, dest_path)
            metadata._children = await self._folder_metadata(dest_path)
            return metadata, created
        else:
            return GoogleDriveFileMetadata(data, dest_path), created  # type: ignore
    async def intra_move(self,  # type: ignore
                         dest_provider: provider.BaseProvider,
                         src_path: WaterButlerPath,
                         dest_path: WaterButlerPath) -> Tuple[BaseGoogleDriveMetadata, bool]:
        self.metrics.add('intra_move.destination_exists', dest_path.identifier is not None)
        if dest_path.identifier:
            await dest_provider.delete(dest_path)

        async with self.request(
            'PATCH',
            self.build_url('files', src_path.identifier),
            headers={
                'Content-Type': 'application/json'
            },
            data=json.dumps({
                'parents': [{
                    'id': dest_path.parent.identifier
                }],
                'title': dest_path.name
            }),
            expects=(200, ),
            throws=exceptions.IntraMoveError,
        ) as resp:
            data = await resp.json()

        created = dest_path.identifier is None
        dest_path.parts[-1]._id = data['id']

        if dest_path.is_dir:
            metadata = GoogleDriveFolderMetadata(data, dest_path)
            metadata._children = await self._folder_metadata(dest_path)
            return metadata, created
        else:
            return GoogleDriveFileMetadata(data, dest_path), created  # type: ignore
Beispiel #3
0
    def create_folder(self, path, **kwargs):
        GoogleDrivePath.validate_folder(path)

        if path.identifier:
            raise exceptions.FolderNamingConflict(str(path))

        resp = yield from self.make_request(
            'POST',
            self.build_url('files'),
            headers={
                'Content-Type': 'application/json',
            },
            data=json.dumps({
                'title': path.name,
                'parents': [{
                    'id': path.parent.identifier
                }],
                'mimeType': 'application/vnd.google-apps.folder'
            }),
            expects=(200, ),
            throws=exceptions.CreateFolderError,
        )

        return GoogleDriveFolderMetadata((yield from resp.json()),
                                         path).serialized()
Beispiel #4
0
    async def create_folder(self,
                            path: wb_path.WaterButlerPath,
                            folder_precheck: bool=True,
                            **kwargs) -> GoogleDriveFolderMetadata:
        GoogleDrivePath.validate_folder(path)

        if folder_precheck:
            if path.identifier:
                raise exceptions.FolderNamingConflict(path.name)

        async with self.request(
            'POST',
            self.build_url('files'),
            headers={
                'Content-Type': 'application/json',
            },
            data=json.dumps({
                'title': path.name,
                'parents': [{
                    'id': path.parent.identifier
                }],
                'mimeType': self.FOLDER_MIME_TYPE,
            }),
            expects=(200, ),
            throws=exceptions.CreateFolderError,
        ) as resp:
            return GoogleDriveFolderMetadata(await resp.json(), path)
Beispiel #5
0
 def _serialize_item(self,
                     path: wb_path.WaterButlerPath,
                     item: dict,
                     raw: bool=False) -> typing.Union[BaseGoogleDriveMetadata, dict]:
     if raw:
         return item
     if item['mimeType'] == self.FOLDER_MIME_TYPE:
         return GoogleDriveFolderMetadata(item, path)
     return GoogleDriveFileMetadata(item, path)
Beispiel #6
0
def test_folder_metadata_slash():
    item = fixtures.folder_metadata_forward_slash
    path = GoogleDrivePath('/we/love/you/conrad').child(item['title'],
                                                        folder=True)
    parsed = GoogleDriveFolderMetadata(item, path)

    assert parsed.provider == 'googledrive'
    assert parsed.id == item['id']
    assert parsed.name == item['title']
    assert parsed.extra == {'revisionId': item['version']}
    assert parsed.path == '/' + os.path.join(*[x.raw
                                               for x in path.parts]) + '/'
    assert parsed.materialized_path == str(path)
    async def test_folder_metadata(self, provider):
        path = GoogleDrivePath('/hugo/kim/pins/',
                               _ids=[str(x) for x in range(4)])

        body = fixtures.generate_list(3, **fixtures.folder_metadata)
        item = body['items'][0]

        query = provider._build_query(path.identifier)
        url = provider.build_url('files', q=query, alt='json', maxResults=1000)

        aiohttpretty.register_json_uri('GET', url, body=body)

        result = await provider.metadata(path)

        expected = GoogleDriveFolderMetadata(
            item, path.child(item['title'], folder=True))

        assert result == [expected]
        assert aiohttpretty.has_call(method='GET', uri=url)
Beispiel #8
0
 def _serialize_item(self, path, item, raw=False):
     if raw:
         return item
     if item['mimeType'] == self.FOLDER_MIME_TYPE:
         return GoogleDriveFolderMetadata(item, path)
     return GoogleDriveFileMetadata(item, path)
Beispiel #9
0
 def _serialize_item(self, path, item, raw=False):
     if raw:
         return item
     if item['mimeType'] == 'application/vnd.google-apps.folder':
         return GoogleDriveFolderMetadata(item, path)
     return GoogleDriveFileMetadata(item, path)