diff --git a/pubs/commands/__init__.py b/pubs/commands/__init__.py index 150dae5..cd17fa3 100644 --- a/pubs/commands/__init__.py +++ b/pubs/commands/__init__.py @@ -15,5 +15,6 @@ from . import export_cmd from . import import_cmd # bonus from . import websearch_cmd +from . import url_cmd from . import edit_cmd diff --git a/pubs/commands/url_cmd.py b/pubs/commands/url_cmd.py new file mode 100644 index 0000000..7ac7b7f --- /dev/null +++ b/pubs/commands/url_cmd.py @@ -0,0 +1,35 @@ +from __future__ import unicode_literals + +import webbrowser + +from .. import repo +from ..uis import get_ui +from ..utils import resolve_citekey_list + + + +def parser(subparsers, conf): + parser = subparsers.add_parser('url', + help="open a paper's url in the default web browser") + parser.add_argument("citekey", nargs = '*', + help="one or more citeKeys to open") + return parser + + +def command(conf, args): + """Open the url of one or several papers in a web browser.""" + + ui = get_ui() + rp = repo.Repository(conf) + + for key in resolve_citekey_list(rp, args.citekey, ui=ui, exit_on_fail=False): + try: + paper = rp.pull_paper(key) + url = paper.bibdata['url'] + ui.info('opening url {}'.format(url)) + webbrowser.open(url) + + except KeyError as e: + ui.warning('{} has no url'.format(key)) + + rp.close() diff --git a/pubs/pubs_cmd.py b/pubs/pubs_cmd.py index 8955cfb..949fd4a 100644 --- a/pubs/pubs_cmd.py +++ b/pubs/pubs_cmd.py @@ -29,6 +29,7 @@ CORE_CMDS = collections.OrderedDict([ ('import', commands.import_cmd), ('websearch', commands.websearch_cmd), + ('url', commands.url_cmd), ('edit', commands.edit_cmd), ]) diff --git a/tests/requirements.txt b/tests/requirements.txt index c3ee05b..2790d94 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -2,3 +2,4 @@ six pyfakefs==3.3 ddt +mock diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 7d98492..8091f69 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -6,6 +6,7 @@ import os import re import sys import shutil +import mock import six import ddt @@ -23,7 +24,6 @@ import fixtures from pubs.commands import init_cmd, import_cmd - # makes the tests very noisy PRINT_OUTPUT = False CAPTURE_OUTPUT = True @@ -104,13 +104,14 @@ class CommandTestCase(fake_env.TestFakeFs): actual_cmd = cmd if not isinstance(cmd, p3.ustr): actual_cmd = cmd[0] - if len(cmd) == 2: # Inputs provided + if len(cmd) >= 2 and cmd[1] is not None: # Inputs provided inputs = cmd[1] - if len(cmd) == 3: # Expected output provided + if len(cmd) >= 3: # Expected output provided capture_output = True - expected_out = color.undye(cmd[2]) - if len(cmd) == 4: # Expected error output provided - expected_err = color.undye(cmd[3]) + if cmd[2] is not None: + expected_out = color.undye(cmd[2]) + if len(cmd) >= 4 and cmd[3] is not None: # Expected error output provided + expected_err = color.undye(cmd[3]) # Always set fake input: test should not ask unexpected user input input = fake_env.FakeInput(inputs, [content, uis, p3]) input.as_global() @@ -497,6 +498,43 @@ class TestTag(DataCommandTestCase): with self.assertRaises(FakeSystemExit): self.execute_cmds(cmds) +class TestURL(DataCommandTestCase): + + def setUp(self): + super(TestURL, self).setUp() + init = ['pubs init', + 'pubs add data/pagerank.bib', + 'pubs add data/turing1950.bib', + 'pubs add data/martius.bib', + ] + self.execute_cmds(init) + + @mock.patch('webbrowser.open') + def test_url_open_one(self, wb_open): + cmds = ['pubs url Page99', + ] + correct = ['info: opening url http://ilpubs.stanford.edu:8090/422/\n', + ] + out = self.execute_cmds(cmds) + self.assertEqual(out, correct) + + @mock.patch('webbrowser.open') + def test_url_open_missing(self, wb_open): + cmds = [('pubs url turing1950computing', None, None, 'warning: turing1950computing has no url\n'), + ] + self.execute_cmds(cmds) + + @mock.patch('webbrowser.open') + def test_url_open_multiple(self, wb_open): + cmds = ['pubs url Page99 10.1371_journal.pone.0063400', + ] + correct = ['info: opening url http://ilpubs.stanford.edu:8090/422/\n' + + 'info: opening url http://dx.doi.org/10.1371%2Fjournal.pone.0063400\n', + ] + out = self.execute_cmds(cmds) + self.assertEqual(out, correct) + + class TestNote(DataCommandTestCase):