commit
1b59aefc08
@ -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
|
||||
|
35
pubs/commands/url_cmd.py
Normal file
35
pubs/commands/url_cmd.py
Normal file
@ -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()
|
@ -29,6 +29,7 @@ CORE_CMDS = collections.OrderedDict([
|
||||
('import', commands.import_cmd),
|
||||
|
||||
('websearch', commands.websearch_cmd),
|
||||
('url', commands.url_cmd),
|
||||
('edit', commands.edit_cmd),
|
||||
])
|
||||
|
||||
|
@ -2,3 +2,4 @@
|
||||
six
|
||||
pyfakefs==3.3
|
||||
ddt
|
||||
mock
|
||||
|
@ -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):
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user