Skip to content

Commit eeb2892

Browse files
author
perrette
committed
New option --add-files to papers list
1 parent 19d9f33 commit eeb2892

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

papers/__main__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,20 @@ def parse_keywords(e):
757757
e['keywords'] = ", ".join(keywords)
758758
savebib(biblio, config)
759759

760+
elif o.add_files:
761+
if len(entries) != 1:
762+
raise PapersExit("list only one entry to use --add-files")
763+
e = entries[0]
764+
files = biblio.get_files(e)
765+
for f in o.add_files:
766+
if not os.path.exists(f):
767+
raise PapersExit(f"file {f} does not exist")
768+
files.extend(o.add_files)
769+
biblio.set_files(e, files)
770+
if o.rename:
771+
biblio.rename_entry_files(e, copy=o.copy)
772+
savebib(biblio, config)
773+
760774
elif o.edit:
761775
otherentries = [e for e in biblio.db.entries if e not in entries]
762776
try:
@@ -1072,6 +1086,9 @@ def get_parser(config=None):
10721086
grp.add_argument('--edit', action='store_true', help='interactive edit text file with entries, and re-insert them')
10731087
grp.add_argument('--fetch', action='store_true', help='fetch and fix metadata')
10741088
grp.add_argument('--add-keywords', '--add-tag', nargs='+', help='add keywords to the selected entries')
1089+
grp.add_argument('--add-files', nargs='+', help='add files to the selected entries (only one entry must be listed)')
1090+
grp.add_argument('--rename', action='store_true', help='rename added file(s) into files folder. Used together with --add-files')
1091+
grp.add_argument('--copy', action='store_true', help='copy added file(s) into files folder. Used together with --add-files --rename')
10751092

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

tests/test_list.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import bibtexparser
2-
from tests.common import LocalInstallTest, Biblio
2+
from tests.common import LocalInstallTest, Biblio, tempfile
33
from papers.utils import strip_all
44

55
bibtex = """@article{Perrette_2011,
@@ -154,4 +154,15 @@ def test_add_tag(self):
154154
# 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)")
155155

156156
out = self.papers(f'list --tag newtag -1', sp_cmd='check_output')
157-
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)")
157+
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)")
158+
159+
def test_add_files(self):
160+
161+
with tempfile.NamedTemporaryFile() as temp, tempfile.NamedTemporaryFile() as temp2:
162+
out = self.papers(f'list 2011 perrette -1', sp_cmd='check_output')
163+
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)")
164+
165+
out = self.papers(f'list 2011 perrette --add-files {temp.name} {temp2.name} --rename --copy', sp_cmd='check_output')
166+
167+
out = self.papers(f'list 2011 perrette -1', sp_cmd='check_output')
168+
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 commit comments

Comments
 (0)