def initFig(self, project_id): """ Initilizes Fighsare information for the given project :return: """ projects = Projects(self.token) self.project_info = projects.get_info(project_id)
def initFig(self, show_progress=True): """ Initial data load from Figshare :return: """ # Get the list of articles from projects = Projects(self.token) articles = projects.list_articles(self.project_id) n_articles = len(articles) worker = ArticleLoadWorker(self.app, self.token, self.parent, self.project_id, articles) thread = QThread() thread.setObjectName('thread_article_load') self.__threads.append((thread, worker)) worker.moveToThread(thread) worker.sig_step.connect(self.add_to_tree) worker.sig_done.connect(self.update_search_field) worker.sig_done.connect(self.enable_fields) thread.started.connect(worker.work) thread.start() self.articles = articles self.article_ids = set() for article in articles: self.article_ids.add(article['id'])
def update_article_file_metadata(self, article_id): """ Updates an articles custom fields metadata :param article_id: int. Figshare article id number :return: """ # Get the current/old file specific metadata article = self.parent.figshare_articles[str(article_id)] old_file_dicts = article.input_dicts()[2:] old_file_metadata = {} for d in old_file_dicts: for key, value in d.items(): old_file_metadata[key] = value # Get the new/edited figshare metadata new_file_metadata = {} file_grid = self.filespecific_tab.widget().layout() # Get the number of rows in the grid layout n_rows = file_grid.rowCount() # Get the new file metadata for row in range(n_rows): lbl = file_grid.itemAtPosition(row, 0).widget().text() edit = file_grid.itemAtPosition(row, 1).widget().text() new_file_metadata[lbl] = edit # Check for changes update_dict = {} for key, value in new_file_metadata.items(): if value != 'None' and value is not None: if value != old_file_metadata[key]: update_dict[key] = value # Update local version of article article.update_info(update_dict) # Reformat update dictionary update_dict = {'custom_fields': update_dict} try: project = Projects(self.token) proj_info = project.update_article(self.token, article_id, update_dict) # Update local version of article article.update_info(update_dict) # Change up_to_date article.figshare_metadata['up_to_date'] = False return None except HTTPError as err: return err except TypeError as err: return err except ValueError as err: return err
def get_project_list(self, token): """ Returns the users private project list :param token: Figshare OAuth token :return: array of project """ projects = Projects(token) return projects.get_list()
def publish_article(self, article_id): """ Publishes a single article :param article_id: int. Figshare article id number :return: """ try: Projects.publish_article(self.token, article_id) self.create_local_article(article_id) return None except HTTPError as err: return err
def initFig(self, project_id: int): """ Initialises Figshare information for the given project by retrieving the list of articles in the project. Args: project_id: Figshare project ID number. Returns: None """ projects = Projects(self.token) self.project_info = projects.get_info(project_id) self.article_list = projects.list_articles(project_id)
def search_projects(self, search_text, token): """ Returns a list of projects matching the users search criteria :param search_text: String. Figshare style elastic search string :param token: Figshare OAuth token :return: """ projects = Projects(token) result = projects.search(search_text) if len(result) == 0: result = projects.get_list() return result
def delete_article(self, project_id: int, article_id: int): """ Uses the Figshare API Interface package to delete the given article from Figshare. Args: project_id: Figshare project ID number, article is within. article_id: Figshare article ID number. Returns: err_msg (str): Either an empty string or contains an error message that occurred during deletion process. """ projects = Projects(self.token) err_msg = projects.article_delete(project_id, article_id) return err_msg
def create_project(self, title, description, funding, group_id): """ Creates a new private figshare project :param title: :param description: :param funding: :param group_id: :return: Dictionary with information on the new project """ projects = Projects(self.token) project_info = projects.create(title, description, funding, group_id) return project_info
def delete_project(self, token, project_id): """ Deletes the given project from Figshare :param token: :param project_id: Int. Figshare project ID number :return: """ projects = Projects(token) try: projects.delete( project_id, safe=False ) # Suppresses command line requirement for acknowledgement return True except: return False
def update_project(self, project_id, token, update_dict): """ Uploads changes to a figshare project :param project_id: int. Figshare project id number :param token: OAuth token :param update_dict: dict. Dictionary holding named arguments to pass to Projects.update() :return: """ try: projects = Projects(token) info = projects.update(project_id, **update_dict) return True except TypeError as err: return err except ValueError as err: return err except HTTPError as err: return err
def get_fields(self): """ Called to return a list of custom metadata fields :return: list of strings """ if len(self.articles) > 0: project = Projects(self.token) article_id = self.articles[0]['id'] result = project.get_article(self.project_id, article_id) keys = set() for key in result.keys(): if key != 'custom_fields': keys.add(key) for d in result['custom_fields']: keys.add(d['name']) return sorted(list(keys)) else: return []
def upload_to_project(self, local_article_id, project_id): """ Uploads a local file to a given figshare project. :param local_article_id: str. ID of local file to be uploaded. :param project_id: int. Project ID for file to be uploaded to. :return: """ # Get the local article local_article = self.main_window.local_articles[local_article_id] # Generate the upload dictionary. upload_dict = local_article.get_upload_dict() local_file_location = local_article.figshare_desktop_metadata['location'] # Upload file to project. projects = Projects(self.token) try: figshare_article_id = projects.create_article(project_id, upload_dict) projects.upload_file(figshare_article_id, local_file_location) except FileExistsError as err: print(err) except HTTPError as err: print(err)
def invite_collaborators(self, project_id, token, collaborators): """ Invites collaborators to a figshare project :param project_id: int. Figshare project id number :param token: OAuth token :param collaborators: List of Dict. Containing either user ids or email addresses :return: """ for col in collaborators: col['role_name'] = 'collaborator' try: projects = Projects(token) for col in collaborators: info = projects.invite(project_id, col) print(info) return True except TypeError as err: return err except ValueError as err: return err except HTTPError as err: print(err.args) return err
def update_article_figshare_metadata(self, article_id): """ Updates the figshare metadata of a single article :param article_id: :return: """ # Get the current/old figshare metadata article = self.parent.figshare_articles[str(article_id)] old_figshare_metadata = article.figshare_metadata # Get the new/edited figshare metadata new_figshare_metadata = {} figshare_grid = self.figshare_tab.widget().layout() # Title title = figshare_grid.itemAtPosition(0, 1).widget().text() new_figshare_metadata['title'] = title # Description description = figshare_grid.itemAtPosition(1, 1).widget().toPlainText() new_figshare_metadata['description'] = description # References references = figshare_grid.itemAtPosition(2, 1).widget().get_tags() new_figshare_metadata['references'] = references # Tags tags = figshare_grid.itemAtPosition(3, 1).widget().get_tags() new_figshare_metadata['tags'] = tags # Categories cat_list = figshare_grid.itemAtPosition(4, 1).widget().get_tags() new_figshare_metadata['categories'] = cat_list # Authors auth_list = figshare_grid.itemAtPosition(5, 1).widget().get_tags() new_figshare_metadata['authors'] = auth_list # Defined Type defined_type = figshare_grid.itemAtPosition(6, 1).widget().currentText() new_figshare_metadata['defined_type'] = defined_type # Funding fund_tags = figshare_grid.itemAtPosition(7, 1).widget().get_tags() funding = '' for tag in fund_tags: funding += tag + ':_:' new_figshare_metadata['funding'] = funding # License license = figshare_grid.itemAtPosition(8, 1).widget().currentIndex() new_figshare_metadata['license'] = license # Create an empty dictionary to add updates/edits update_dict = {} # Check for changes for key, value in new_figshare_metadata.items(): if value != 'None' and value is not None and value != '': if value != old_figshare_metadata[key]: update_dict[key] = value try: project = Projects(self.token) proj_info = project.update_article(self.token, article_id, update_dict) # Update local version of article article.update_info(update_dict) # Change up_to_date article.figshare_metadata['up_to_date'] = False return None except HTTPError as err: return err except TypeError as err: return err except ValueError as err: return err
def on_download_article_pressed(self): """ Called when the download article button is pressed. :return: """ # Get the list of article id numbers from the selected items in the article list widget article_ids = self.article_list_widget.get_selection() # Ask if all articles are desired if there is no selection if article_ids == []: reply = QMessageBox.question(self, "Download Confirmation", "Download All Articles?", QMessageBox.Yes, QMessageBox.No) if reply == QMessageBox.Yes: article_ids = self.article_list_widget.get_all() else: article_ids = None # If there are articles to download if article_ids is not None: # Get a list of files from all articles # keep the file names and the file download urls downloads = [] for article in article_ids: file_list = Projects(self.token).list_files(article) for f in file_list: if f['name'][-4:] != '.png': downloads.append([f['name'], f['download_url']]) # Ask the user to choose a download directory download_dir = str( QFileDialog.getExistingDirectory(self, "Select Directory")) # An empty list to record any download errors download_statuses = [] # Download all files in the list for f in downloads: local_path = os.path.abspath(download_dir + '/' + f[0]) url = f[1] status = download_file(url, local_path, self.token) # If there is an error in any of the downloads record the file name if status != 200: download_statuses.append(f[0]) # If there are any errors display a message that lists the affected files if download_statuses != []: msg = 'There was an error in downloading the following files.\n' for f in download_statuses: msg += f + '\n' reply = QMessageBox.warning(self, "Download Error", msg, QMessageBox.Ok) if reply == QMessageBox.Ok: pass else: pass # Otherwise confirm that the downloads have been successful else: reply = QMessageBox.information(self, 'Download Confirmation', "All files downloaded", QMessageBox.Ok) if reply == QMessageBox.Ok: pass else: pass