Skip to content

Commit

Permalink
New option --add-files to papers list
Browse files Browse the repository at this point in the history
  • Loading branch information
perrette committed Jun 27, 2023
1 parent 19d9f33 commit eeb2892
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
17 changes: 17 additions & 0 deletions papers/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,20 @@ def parse_keywords(e):
e['keywords'] = ", ".join(keywords)
savebib(biblio, config)

elif o.add_files:
if len(entries) != 1:
raise PapersExit("list only one entry to use --add-files")
e = entries[0]
files = biblio.get_files(e)
for f in o.add_files:
if not os.path.exists(f):
raise PapersExit(f"file {f} does not exist")
files.extend(o.add_files)
biblio.set_files(e, files)
if o.rename:
biblio.rename_entry_files(e, copy=o.copy)
savebib(biblio, config)

elif o.edit:
otherentries = [e for e in biblio.db.entries if e not in entries]
try:
Expand Down Expand Up @@ -1072,6 +1086,9 @@ def get_parser(config=None):
grp.add_argument('--edit', action='store_true', help='interactive edit text file with entries, and re-insert them')
grp.add_argument('--fetch', action='store_true', help='fetch and fix metadata')
grp.add_argument('--add-keywords', '--add-tag', nargs='+', help='add keywords to the selected entries')
grp.add_argument('--add-files', nargs='+', help='add files to the selected entries (only one entry must be listed)')
grp.add_argument('--rename', action='store_true', help='rename added file(s) into files folder. Used together with --add-files')
grp.add_argument('--copy', action='store_true', help='copy added file(s) into files folder. Used together with --add-files --rename')

# grp.add_argument('--merge-duplicates', action='store_true')

Expand Down
15 changes: 13 additions & 2 deletions tests/test_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import bibtexparser
from tests.common import LocalInstallTest, Biblio
from tests.common import LocalInstallTest, Biblio, tempfile
from papers.utils import strip_all

bibtex = """@article{Perrette_2011,
Expand Down Expand Up @@ -154,4 +154,15 @@ def test_add_tag(self):
# self.assertEqual(strip_all(out), "Perrette_2011: Near-ubiquity of ice-edge blooms in the Arctic (doi:10.5194/bg-8-515-2011, files:2, kiwi | ocean | newtag)")

out = self.papers(f'list --tag newtag -1', sp_cmd='check_output')
self.assertEqual(strip_all(out), "Perrette_2011: Near-ubiquity of ice-edge blooms in the Arctic (doi:10.5194/bg-8-515-2011, files:2, kiwi | ocean | newtag)")
self.assertEqual(strip_all(out), "Perrette_2011: Near-ubiquity of ice-edge blooms in the Arctic (doi:10.5194/bg-8-515-2011, files:2, kiwi | ocean | newtag)")

def test_add_files(self):

with tempfile.NamedTemporaryFile() as temp, tempfile.NamedTemporaryFile() as temp2:
out = self.papers(f'list 2011 perrette -1', sp_cmd='check_output')
self.assertEqual(strip_all(out), "Perrette_2011: Near-ubiquity of ice-edge blooms in the Arctic (doi:10.5194/bg-8-515-2011, files:2, kiwi | ocean)")

out = self.papers(f'list 2011 perrette --add-files {temp.name} {temp2.name} --rename --copy', sp_cmd='check_output')

out = self.papers(f'list 2011 perrette -1', sp_cmd='check_output')
self.assertEqual(strip_all(out), "Perrette_2011: Near-ubiquity of ice-edge blooms in the Arctic (doi:10.5194/bg-8-515-2011, files:4, kiwi | ocean)")

0 comments on commit eeb2892

Please sign in to comment.