Merge pull request #201 from dvklopfenstein/master

Added '-a' (--append) arg to append a line of text to the notes file.
main
Fabien C. Y. Benureau 6 years ago committed by GitHub
commit 2b408fe7ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,6 +3,7 @@ from ..uis import get_ui
from ..utils import resolve_citekey from ..utils import resolve_citekey
from ..completion import CiteKeyCompletion from ..completion import CiteKeyCompletion
from ..events import NoteEvent from ..events import NoteEvent
from ..content import write_file
def parser(subparsers, conf): def parser(subparsers, conf):
@ -10,6 +11,8 @@ def parser(subparsers, conf):
help='edit the note attached to a paper') help='edit the note attached to a paper')
parser.add_argument('citekey', help='citekey of the paper', parser.add_argument('citekey', help='citekey of the paper',
).completer = CiteKeyCompletion(conf) ).completer = CiteKeyCompletion(conf)
parser.add_argument('-a', '--append',
help='append a line of text to the notes file', default=None)
return parser return parser
@ -19,6 +22,10 @@ def command(conf, args):
rp = repo.Repository(conf) rp = repo.Repository(conf)
citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True) citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True)
notepath = rp.databroker.real_notepath(citekey, rp.conf['main']['note_extension']) notepath = rp.databroker.real_notepath(citekey, rp.conf['main']['note_extension'])
ui.edit_file(notepath, temporary=False) if args.append is None:
ui.edit_file(notepath, temporary=False)
else:
latestnote = '{txt}\n'.format(txt=args.append)
write_file(notepath, latestnote, 'a')
NoteEvent(citekey).send() NoteEvent(citekey).send()
rp.close() rp.close()

@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-
"""Test appending a note file from the command-line using the '-a' arg"""
# If you store your pubs in a directory other than home, you may want to
# set this prior to running this test:
# export PUBSCONF=~/.pubsrc
# vim -p tests/test_note_append.py tests/test_usecase.py
from __future__ import print_function, unicode_literals
import unittest
import os
from tests.test_usecase import DataCommandTestCase
class TestNoteAppend(DataCommandTestCase):
"""Test appending a note file from the command-line using the '-a' arg"""
def setUp(self, nsec_stat=True):
"""Initialize a bib entry containing citation key, Page99, for testing"""
super(TestNoteAppend, self).setUp()
init = ['pubs init',
'pubs add data/pagerank.bib',
]
self.execute_cmds(init)
self.note_dir = os.path.join(self.default_pubs_dir, 'notes')
def test_note_append(self):
"""Test appending the note file using the command-line argument, -a"""
fin_notes = os.path.join(self.note_dir, 'Page99.txt')
# Test adding first line
cmds = [('pubs note Page99 -a aaa')]
self.execute_cmds(cmds)
note_lines = ['aaa']
self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines))
# Test adding additional line
cmds = [('pubs note Page99 -a bbb')]
self.execute_cmds(cmds)
note_lines.append('bbb')
self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines))
# https://github.com/pubs/pubs/pull/201#discussion_r307499310
# Test multiword line.
# * Pass the command split into a command and its args to
# execute_cmdsplit, which is called by execute_cmds:
#cmds = [('pubs note Page99 -a "xxx yyy"')]
cmd_split = ['pubs', 'note', 'Page99', '-a', 'xxx yyy']
self.execute_cmdsplit(cmd_split, expected_out=None, expected_err=None)
note_lines.append('xxx yyy')
self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines))
# # https://github.com/pubs/pubs/pull/201#discussion_r305274071
# # Test adding Chinese characters
# cmds = [('pubs note Page99 -a \347\350\346\345')]
# self.execute_cmds(cmds)
# note_lines.append('\347\350\346\345')
# self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines))
# # Test adding Japanese character
# cmds = [('pubs note Page99 -a ソ')]
# self.execute_cmds(cmds)
# note_lines.append('ソ')
# self.assertFileContentEqual(fin_notes, self._get_note_content(note_lines))
@staticmethod
def _get_note_content(note_lines):
"""Given a list of note lines, return full note file content"""
return '{lines}\n'.format(lines='\n'.join(note_lines))
if __name__ == '__main__':
unittest.main(verbosity=2)

@ -97,14 +97,6 @@ class CommandTestCase(fake_env.TestFakeFs):
3. the expected output on stdout, verified with assertEqual. 3. the expected output on stdout, verified with assertEqual.
4. the expected output on stderr, verified with assertEqual. 4. the expected output on stderr, verified with assertEqual.
""" """
def normalize(s):
s = color.undye(s)
try:
s = s.decode('utf-8')
except AttributeError:
pass
return s
try: try:
outs = [] outs = []
for cmd in cmds: for cmd in cmds:
@ -126,15 +118,8 @@ class CommandTestCase(fake_env.TestFakeFs):
input.as_global() input.as_global()
try: try:
if capture_output: if capture_output:
capture_wrap = fake_env.capture(pubs_cmd.execute, actual_out = self.execute_cmdsplit(
verbose=PRINT_OUTPUT) actual_cmd.split(), expected_out, expected_err)
_, stdout, stderr = capture_wrap(actual_cmd.split())
actual_out = normalize(stdout)
actual_err = normalize(stderr)
if expected_out is not None:
self.assertEqual(p3.u_maybe(actual_out), p3.u_maybe(expected_out))
if expected_err is not None:
self.assertEqual(p3.u_maybe(actual_err), p3.u_maybe(expected_err))
outs.append(color.undye(actual_out)) outs.append(color.undye(actual_out))
else: else:
pubs_cmd.execute(actual_cmd.split()) pubs_cmd.execute(actual_cmd.split())
@ -150,6 +135,29 @@ class CommandTestCase(fake_env.TestFakeFs):
else: else:
raise FakeSystemExit(*exc.args).with_traceback(tb) raise FakeSystemExit(*exc.args).with_traceback(tb)
@staticmethod
def normalize(s):
"""Remove color from a string, adjusting for a decode method needed in Python2"""
s = color.undye(s)
try:
s = s.decode('utf-8')
except AttributeError:
pass
return s
def execute_cmdsplit(self, actual_cmdlist, expected_out, expected_err):
"""Run a single command, which has been split into a list containing cmd and args"""
capture_wrap = fake_env.capture(pubs_cmd.execute,
verbose=PRINT_OUTPUT)
_, stdout, stderr = capture_wrap(actual_cmdlist)
actual_out = self.normalize(stdout)
actual_err = self.normalize(stderr)
if expected_out is not None:
self.assertEqual(p3.u_maybe(actual_out), p3.u_maybe(expected_out))
if expected_err is not None:
self.assertEqual(p3.u_maybe(actual_err), p3.u_maybe(expected_err))
return actual_out
def tearDown(self): def tearDown(self):
pass pass

Loading…
Cancel
Save