Пример #1
0
 def move_remote_file(self, local_file):
     assert isinstance(local_file, File)
     assert local_file.is_file
     assert local_file.locally_moved
     assert not local_file.is_provider
     AlertHandler.info(local_file.name, AlertHandler.MOVING)
     return (yield from self._move_remote_file_folder(local_file))
Пример #2
0
 def move_remote_file(self, local_file):
     assert isinstance(local_file, File)
     assert local_file.is_file
     assert local_file.locally_moved
     assert not local_file.is_provider
     AlertHandler.info(local_file.name, AlertHandler.MOVING)
     return (yield from self._move_remote_file_folder(local_file))
Пример #3
0
    def upload_folder(self, local_folder):
        assert isinstance(local_folder, File)
        assert local_folder.is_folder
        # PUT /v1/resources/6/providers/osfstorage/21/?kind=folder&name=FUN_FOLDER HTTP/1.1" 200 -

        params = {
            'kind': 'folder',
            'name': local_folder.name,
        }
        files_url = api_url_for(
            RESOURCES,
            node_id=local_folder.node.osf_id,
            provider=local_folder.provider,
            file_id=local_folder.parent.osf_id if local_folder.has_parent else None
        )
        resp_json = yield from self.make_request(files_url, method="PUT", params=params, get_json=True)
        AlertHandler.info(local_folder.name, AlertHandler.UPLOAD)

        # todo: determine whether uploaded folder will contain api url for its children
        new_file_id = resp_json['data']['id'].split('/')[1]
        children_url = api_url_for(NODES, related_type=FILES, node_id=local_folder.node.osf_id,
                                   provider=local_folder.provider, file_id=new_file_id)
        resp_json['data']['relationships'] = {
            'files': {
                'links': {
                    'related': {
                        'href': children_url
                    }
                }
            }
        }
        # https://staging-api.osf.io/v2/nodes/4e6k8/files/osfstorage/562134f1029bdb6c230f2874/
        # ['relationships']['files']['links']['related']['href']

        return dict_to_remote_object(resp_json['data'])
Пример #4
0
    def upload_file(self, local_file):
        """
        THROWS FileNotFoundError !!!!!!
        :param local_file:
        :return:
        """
        assert isinstance(local_file, File)
        assert local_file.is_file
        # /v1/resources/6/providers/osfstorage/21/?kind=file&name=FUN_FILE HTTP/1.1" 200 -
        params = {'provider': local_file.provider, 'name': local_file.name}

        parent_osf_id = local_file.parent.osf_id if local_file.has_parent else None
        files_url = api_url_for(RESOURCES,
                                node_id=local_file.node.osf_id,
                                provider=local_file.provider,
                                file_id=parent_osf_id)
        file = open(local_file.path, 'rb')
        resp_json = yield from self.make_request(files_url,
                                                 method="PUT",
                                                 params=params,
                                                 data=file,
                                                 get_json=True)
        AlertHandler.info(local_file.name, AlertHandler.UPLOAD)

        return RemoteFile(resp_json['data'])
Пример #5
0
 def run(self):
     file_to_delete = ProperPath(self.path, is_dir=False)
     AlertHandler.info(file_to_delete.name, AlertHandler.DELETING)
     try:
         os.remove(file_to_delete.full_path)
     except FileNotFoundError:
         logging.warning(
             'file not deleted because does not exist on local filesystem. inside delete_local_file_folder (2)')
Пример #6
0
 def run(self):
     try:
         new_file_path = ProperPath(self.path, is_dir=False)
     except Exception:
         # TODO: Narrow down this exception and do client side warnings
         logging.exception('Exception caught: Invalid target path for new file.')
         return
     AlertHandler.info(new_file_path.name, AlertHandler.DOWNLOAD)
     yield from _download_file(new_file_path, self.download_url, self.osf_query)
Пример #7
0
 def run(self):
     file_to_delete = ProperPath(self.path, is_dir=False)
     AlertHandler.info(file_to_delete.name, AlertHandler.DELETING)
     try:
         os.remove(file_to_delete.full_path)
     except FileNotFoundError:
         logging.warning(
             'file not deleted because does not exist on local filesystem. inside delete_local_file_folder (2)'
         )
Пример #8
0
def _rename(old_path, new_path):
    if not isinstance(old_path, ProperPath):
        logging.error("Old path for rename is not a ProperPath.")
    if not isinstance(new_path, ProperPath):
        logging.error("Old path for rename is not a ProperPath.")
    try:
        AlertHandler.info(new_path.name, AlertHandler.MODIFYING)
        os.renames(old_path.full_path, new_path.full_path)
    except FileNotFoundError:
        logging.warning('renaming of file/folder failed because file/folder not there')
Пример #9
0
 def run(self):
     try:
         new_file_path = ProperPath(self.path, is_dir=False)
     except Exception:
         # TODO: Narrow down this exception and do client side warnings
         logging.exception(
             'Exception caught: Invalid target path for new file.')
         return
     AlertHandler.info(new_file_path.name, AlertHandler.DOWNLOAD)
     yield from _download_file(new_file_path, self.download_url,
                               self.osf_query)
Пример #10
0
def _rename(old_path, new_path):
    if not isinstance(old_path, ProperPath):
        logging.error("Old path for rename is not a ProperPath.")
    if not isinstance(new_path, ProperPath):
        logging.error("Old path for rename is not a ProperPath.")
    try:
        AlertHandler.info(new_path.name, AlertHandler.MODIFYING)
        os.renames(old_path.full_path, new_path.full_path)
    except FileNotFoundError:
        logging.warning(
            'renaming of file/folder failed because file/folder not there')
Пример #11
0
 def run(self):
     if not isinstance(self.osf_query, OSFQuery):
         logging.error('Update file query is not an instance of OSFQuery')
         return
     if not isinstance(self.download_url, str):
         logging.error('Update file download_url is not a str.')
         return
     try:
         updated_file_path = ProperPath(self.path, is_dir=False)
     except Exception:
         # TODO: Narrow down this exception and do client side warnings
         logging.exception('Exception caught: Invalid target path for updated file.')
         return
     AlertHandler.info(updated_file_path.name, AlertHandler.MODIFYING)
     yield from _download_file(updated_file_path, self.download_url, self.osf_query)
Пример #12
0
    def run(self):
        try:
            old_file_path = ProperPath(self.old_path, is_dir=False)
        except Exception:
            # TODO: Narrow down this exception and do client side warnings
            logging.exception('Exception caught: Invalid origin path for renamed file.')
            return
        try:
            new_file_path = ProperPath(self.new_path, is_dir=False)
        except Exception:
            # TODO: Narrow down this exception and do client side warnings
            logging.exception('Exception caught: Invalid target path for renamed folder.')
            return

        AlertHandler.info(new_file_path.name, AlertHandler.MODIFYING)
        yield from _rename(old_file_path, new_file_path)
Пример #13
0
 def run(self):
     # create local node folder on filesystem
     try:
         folder_to_create = ProperPath(self.path, is_dir=True)
     except Exception:
         # TODO: Narrow down this exception and do client side warnings
         logging.exception('Exception caught: Invalid target path for folder.')
         return
     if not os.path.exists(folder_to_create.full_path):
         AlertHandler.info(folder_to_create.name, AlertHandler.DOWNLOAD)
         try:
             os.makedirs(folder_to_create.full_path)
         except Exception:
             # TODO: Narrow down this exception and do client side warnings
             logging.exception('Exception caught: Problem making a directory.')
             return
Пример #14
0
 def run(self):
     if not isinstance(self.osf_query, OSFQuery):
         logging.error('Update file query is not an instance of OSFQuery')
         return
     if not isinstance(self.download_url, str):
         logging.error('Update file download_url is not a str.')
         return
     try:
         updated_file_path = ProperPath(self.path, is_dir=False)
     except Exception:
         # TODO: Narrow down this exception and do client side warnings
         logging.exception(
             'Exception caught: Invalid target path for updated file.')
         return
     AlertHandler.info(updated_file_path.name, AlertHandler.MODIFYING)
     yield from _download_file(updated_file_path, self.download_url,
                               self.osf_query)
Пример #15
0
 def run(self):
     # create local node folder on filesystem
     try:
         folder_to_create = ProperPath(self.path, is_dir=True)
     except Exception:
         # TODO: Narrow down this exception and do client side warnings
         logging.exception(
             'Exception caught: Invalid target path for folder.')
         return
     if not os.path.exists(folder_to_create.full_path):
         AlertHandler.info(folder_to_create.name, AlertHandler.DOWNLOAD)
         try:
             os.makedirs(folder_to_create.full_path)
         except Exception:
             # TODO: Narrow down this exception and do client side warnings
             logging.exception(
                 'Exception caught: Problem making a directory.')
             return
Пример #16
0
    def run(self):
        try:
            folder_to_delete = ProperPath(self.path, is_dir=True)
        except Exception:
            # TODO: Narrow down this exception and do client side warnings
            logging.exception('Exception caught: Invalid source path for deleted folder.')
            return

        AlertHandler.info(folder_to_delete.name, AlertHandler.DELETING)
        try:
            shutil.rmtree(
                folder_to_delete.full_path,
                onerror=lambda a, b, c: logging.warning('local node not deleted because it does not exist.')
            )
        except Exception:
            # TODO: Narrow down this exception and do client side warnings
            logging.exception('Exception caught: Problem removing the tree.')
            return
Пример #17
0
    def run(self):
        try:
            old_folder_path = ProperPath(self.old_path, is_dir=True)
        except Exception:
            # TODO: Narrow down this exception and do client side warnings
            logging.exception(
                'Exception caught: Invalid origin path for renamed folder.')
            return
        try:
            new_folder_path = ProperPath(self.new_path, is_dir=True)
        except Exception:
            # TODO: Narrow down this exception and do client side warnings
            logging.exception(
                'Exception caught: Invalid target path for renamed folder.')
            return

        AlertHandler.info(new_folder_path.name, AlertHandler.MODIFYING)
        yield from _rename(old_folder_path, new_folder_path)
Пример #18
0
    def run(self):
        try:
            folder_to_delete = ProperPath(self.path, is_dir=True)
        except Exception:
            # TODO: Narrow down this exception and do client side warnings
            logging.exception(
                'Exception caught: Invalid source path for deleted folder.')
            return

        AlertHandler.info(folder_to_delete.name, AlertHandler.DELETING)
        try:
            shutil.rmtree(
                folder_to_delete.full_path,
                onerror=lambda a, b, c: logging.warning(
                    'local node not deleted because it does not exist.'))
        except Exception:
            # TODO: Narrow down this exception and do client side warnings
            logging.exception('Exception caught: Problem removing the tree.')
            return
Пример #19
0
    def upload_folder(self, local_folder):
        assert isinstance(local_folder, File)
        assert local_folder.is_folder
        # PUT /v1/resources/6/providers/osfstorage/21/?kind=folder&name=FUN_FOLDER HTTP/1.1" 200 -

        params = {
            'kind': 'folder',
            'name': local_folder.name,
        }
        files_url = api_url_for(RESOURCES,
                                node_id=local_folder.node.osf_id,
                                provider=local_folder.provider,
                                file_id=local_folder.parent.osf_id
                                if local_folder.has_parent else None)
        resp_json = yield from self.make_request(files_url,
                                                 method="PUT",
                                                 params=params,
                                                 get_json=True)
        AlertHandler.info(local_folder.name, AlertHandler.UPLOAD)

        # todo: determine whether uploaded folder will contain api url for its children
        new_file_id = resp_json['data']['id'].split('/')[1]
        children_url = api_url_for(NODES,
                                   related_type=FILES,
                                   node_id=local_folder.node.osf_id,
                                   provider=local_folder.provider,
                                   file_id=new_file_id)
        resp_json['data']['relationships'] = {
            'files': {
                'links': {
                    'related': {
                        'href': children_url
                    }
                }
            }
        }
        # https://staging-api.osf.io/v2/nodes/4e6k8/files/osfstorage/562134f1029bdb6c230f2874/
        # ['relationships']['files']['links']['related']['href']

        return dict_to_remote_object(resp_json['data'])
Пример #20
0
    def upload_file(self, local_file):
        """
        THROWS FileNotFoundError !!!!!!
        :param local_file:
        :return:
        """
        assert isinstance(local_file, File)
        assert local_file.is_file
        # /v1/resources/6/providers/osfstorage/21/?kind=file&name=FUN_FILE HTTP/1.1" 200 -
        params = {
            'provider': local_file.provider,
            'name': local_file.name
        }

        parent_osf_id = local_file.parent.osf_id if local_file.has_parent else None
        files_url = api_url_for(RESOURCES, node_id=local_file.node.osf_id, provider=local_file.provider,
                                file_id=parent_osf_id)
        file = open(local_file.path, 'rb')
        resp_json = yield from self.make_request(files_url, method="PUT", params=params, data=file, get_json=True)
        AlertHandler.info(local_file.name, AlertHandler.UPLOAD)

        return RemoteFile(resp_json['data'])
Пример #21
0
 def rename_remote_folder(self, local_folder, remote_folder):
     assert isinstance(local_folder, File)
     assert local_folder.is_folder
     assert isinstance(remote_folder, RemoteFolder)
     AlertHandler.info(local_folder.name, AlertHandler.MODIFYING)
     return (yield from self._rename_remote(local_folder, remote_folder))
Пример #22
0
 def delete_remote_folder(self, remote_folder):
     assert isinstance(remote_folder, RemoteFolder)
     yield from self._delete_file_folder(remote_folder)
     AlertHandler.info(remote_folder.name, AlertHandler.DELETING)
Пример #23
0
 def delete_remote_folder(self, remote_folder):
     assert isinstance(remote_folder, RemoteFolder)
     yield from self._delete_file_folder(remote_folder)
     AlertHandler.info(remote_folder.name, AlertHandler.DELETING)
Пример #24
0
 def rename_remote_folder(self, local_folder, remote_folder):
     assert isinstance(local_folder, File)
     assert local_folder.is_folder
     assert isinstance(remote_folder, RemoteFolder)
     AlertHandler.info(local_folder.name, AlertHandler.MODIFYING)
     return (yield from self._rename_remote(local_folder, remote_folder))