コード例 #1
0
ファイル: utils.py プロジェクト: wavefrontshaping/papis
def locate_document_in_lib(
        document: papis.document.Document,
        library: Optional[str] = None) -> papis.document.Document:
    """Try to figure out if a document is already in a library

    :param document: Document to be searched for
    :type  document: papis.document.Document
    :param library: Name of a valid papis library
    :type  library: str
    :returns: Document in library if found
    :rtype:  papis.document.Document
    :raises IndexError: Whenever document is not found in the library
    """
    db = papis.database.get(library_name=library)
    comparing_keys = papis.config.getlist('unique-document-keys')
    assert comparing_keys is not None

    for k in comparing_keys:
        if not document.has(k):
            continue
        docs = db.query_dict({k: document[k]})
        if docs:
            return docs[0]

    raise IndexError("Document not found in library")
コード例 #2
0
def run(document: papis.document.Document) -> Optional[str]:
    """Browse document's url whenever possible and returns the url

    :document: Document object

    """
    global logger
    url = None
    key = papis.config.getstring("browse-key")

    if document.has(key):
        if "doi" == key:
            url = 'https://doi.org/{}'.format(document['doi'])
        elif "isbn" == key:
            url = 'https://isbnsearch.org/isbn/{}'.format(document['isbn'])
        else:
            url = document[key]

    if url is None or key == 'search-engine':
        params = {
            'q':
            papis.format.format(papis.config.getstring('browse-query-format'),
                                document)
        }
        url = (papis.config.getstring('search-engine') + '/?' +
               urlencode(params))

    logger.info("Opening url %s:" % url)
    papis.utils.general_open(url, "browser", wait=False)
    return url
コード例 #3
0
def run(document):
    """Browse document's url whenever possible.

    :document: Document object
    :returns: Returns the url that is composed from the document
    :rtype:  str

    """
    global logger
    url = None
    key = papis.config.get("browse-key")

    if document.has(key):
        if "doi" == key:
            url = 'https://doi.org/{}'.format(document['doi'])
        elif "isbn" == key:
            url = 'https://isbnsearch.org/isbn/{}'.format(document['isbn'])
        else:
            url = document[key]

    if url is None or key == 'search-engine':
        params = {
            'q':
            papis.utils.format_doc(papis.config.get('browse-query-format'),
                                   document)
        }
        url = papis.config.get('search-engine') + '/?' + urlencode(params)

    logger.info("Opening url %s:" % url)
    papis.utils.general_open(url, "browser", wait=False)
    return url
コード例 #4
0
ファイル: edit.py プロジェクト: wavefrontshaping/papis
def cli(query: str,
        doc_folder: str,
        git: bool,
        notes: bool,
        _all: bool,
        editor: Optional[str],
        sort_field: Optional[str],
        sort_reverse: bool) -> None:
    """Edit document information from a given library"""

    logger = logging.getLogger('cli:edit')

    if doc_folder:
        documents = [papis.document.from_folder(doc_folder)]
    else:
        documents = papis.database.get().query(query)

    if sort_field:
        documents = papis.document.sort(documents, sort_field, sort_reverse)

    if editor is not None:
        papis.config.set('editor', editor)

    if not _all:
        documents = list(papis.pick.pick_doc(documents))

    if len(documents) == 0:
        logger.warning(papis.strings.no_documents_retrieved_message)
        return

    for document in documents:
        if notes:
            logger.debug("Editing notes")
            if not document.has("notes"):
                logger.warning(
                    "The document selected has no notes attached, \n"
                    "creating a notes files"
                )
                document["notes"] = papis.config.getstring("notes-name")
                document.save()
            notes_path = os.path.join(
                str(document.get_main_folder()),
                document["notes"]
            )

            if not os.path.exists(notes_path):
                logger.info("Creating {0}".format(notes_path))
                open(notes_path, "w+").close()

            papis.api.edit_file(notes_path)
            if git:
                papis.git.add_and_commit_resource(
                    str(document.get_main_folder()),
                    str(document.get_info_file()),
                    "Update notes for '{0}'".format(
                        papis.document.describe(document)))

        else:
            run(document, git=git)
コード例 #5
0
def edit_notes(document: papis.document.Document, git: bool = False) -> None:
    logger = logging.getLogger('edit:notes')
    logger.debug("Editing notes")
    if not document.has("notes"):
        document["notes"] = papis.config.getstring("notes-name")
        document.save()
    notes_path = os.path.join(str(document.get_main_folder()),
                              document["notes"])

    if not os.path.exists(notes_path):
        logger.debug("Creating {0}".format(notes_path))
        open(notes_path, "w+").close()

    papis.api.edit_file(notes_path)
    if git:
        papis.git.add_and_commit_resource(
            str(document.get_main_folder()), str(document.get_info_file()),
            "Update notes for '{0}'".format(papis.document.describe(document)))