diff options
author | Alejandro Gallo <aamsgallo@gmail.com> | 2018-09-17 19:43:04 +0200 |
---|---|---|
committer | Alejandro Gallo <aamsgallo@gmail.com> | 2018-09-17 19:43:04 +0200 |
commit | 51b195a00a5a7e90d4e728fc78d382e2c517b7f1 (patch) | |
tree | 1b3e284e20ea141a976229900290a55ed99d2972 | |
parent | 6fd3cf6e81fa540f2cdccc995bdb84574cdb07ce (diff) |
Fix duplication checking
-rw-r--r-- | papis/commands/add.py | 20 | ||||
-rw-r--r-- | papis/utils.py | 24 |
2 files changed, 37 insertions, 7 deletions
diff --git a/papis/commands/add.py b/papis/commands/add.py index 874383b1..bdeb418b 100644 --- a/papis/commands/add.py +++ b/papis/commands/add.py @@ -364,15 +364,21 @@ def run( # Duplication checking logger.info("Check if the added document is already existing") - found_document = papis.utils.locate_document( - tmp_document, papis.api.get_documents_in_lib(papis.api.get_lib()) - ) - if found_document is not None: - logger.warning('\n' + papis.document.dump(found_document)) - print("\n\n") + try: + found_document = papis.utils.locate_document_in_lib(tmp_document) + except IndexError: + logger.info("Document not found in library") + else: + logger.warning( + '\n\n\n\n{0}\n\n\n\n'.format(papis.document.dump(found_document)) + ) logger.warning("DUPLICATION WARNING") logger.warning( - "The document above seems to be already in your library: \n\n" + "The document above is in your library and it seems to match" + ) + logger.warning( + "the one that you're trying to add, " + "I will prompt you for confirmation" ) logger.warning( "(Hint) Use the update command if you just want to update" diff --git a/papis/utils.py b/papis/utils.py index 3abb47d7..117c07e6 100644 --- a/papis/utils.py +++ b/papis/utils.py @@ -277,6 +277,30 @@ def git_commit(path="", message=""): call(cmd) +def locate_document_in_lib(document, library=None): + """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=library) + comparing_keys = eval(papis.config.get('unique-document-keys')) + + 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") + + def locate_document(document, documents): """Try to figure out if a document is already within a list of documents. |