Exemplo n.º 1
0
    def get_object_list(self):
        """
        Called to return a list of Figshare collections associated to the user.

        Returns:
            object_list (list of dicts): List of users Figshare objects.
        """
        collections = Collections(self.token)
        object_list = collections.get_list()
        return object_list
Exemplo n.º 2
0
 def publish_article(self, article_id):
     """
     Publishes a single article
     :param article_id: int. Figshare article id number
     :return:
     """
     try:
         Collections.publish_article(self.token, article_id)
         self.create_local_article(article_id)
         return None
     except HTTPError as err:
         return err
Exemplo n.º 3
0
    def initFig(self, collection_id: int):
        """
        Initilizes Figshare information for the given project by retrieving the list of articles in the project.

        Args:
            project_id: Figshare project ID number.

        Returns:
            None
        """
        collections = Collections(self.token)
        self.collection_info = collections.get_info(collection_id)
        self.article_list = collections.get_articles(collection_id)
Exemplo n.º 4
0
    def delete_article(self, collection_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.
        """
        collections = Collections(self.token)
        err_msg = collections.delete_article(collection_id, article_id)
        return err_msg
Exemplo n.º 5
0
    def delete_object(self, object_id: int):
        """
        Called to delete the given figshare object.

        Args:
            object_id:

        Returns:
            bool: True of False dependent on if the deletion was successful.
        """
        collections = Collections(self.token)
        try:
            collections.delete(object_id, safe=False)  # Suppress command line confirmation
            return True
        except:
            return False
Exemplo n.º 6
0
    def search_objects(self, search_text: str):
        """
        Gets a list of objects matching the users search query.

        Args:
            search_text: Figshare style elastic search string

        Returns:
            result (list of dicts): Gives a list of dictionary objects that either match those of the search criteria,
                or returns the full set if no matches found.
        """
        collections = Collections(self.token)
        result = collections.search(search_text)
        if len(result) == 0:
            result = collections.get_list()
        return result
    def initFig(self, show_progress=True):
        """
        Initial data load from Figshare
        :return:
        """
        # Get the list of articles from
        collections = Collections(self.token)
        articles = collections.get_articles(self.collection_id)
        n_articles = len(articles)

        worker = ArticleLoadWorker(self.app, self.token, self.parent,
                                   self.collection_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 get_fields(self):
        """
        Called to return a list of custom metadata fields
        :return: list of strings
        """

        if len(self.articles) > 0:
            collections = Collections(self.token)
            article_id = self.articles[0]['id']
            result = collections.get_article(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 []
Exemplo n.º 9
0
    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 = Collections(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