From 4ffd62fe9fd3e465ca26c552d3ee498b70820c1f Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Wed, 5 Jul 2017 15:58:28 -0400 Subject: [PATCH] [notes] Allow different file extension for note file (see #70). --- pubs/commands/note_cmd.py | 6 ++-- pubs/config/spec.py | 3 ++ pubs/databroker.py | 21 +++++++----- pubs/datacache.py | 12 +++---- pubs/repo.py | 6 ++-- tests/fake_env.py | 4 +++ tests/test_databroker.py | 1 - tests/test_usecase.py | 70 +++++++++++++++++++++++++++++++++++++-- 8 files changed, 99 insertions(+), 24 deletions(-) diff --git a/pubs/commands/note_cmd.py b/pubs/commands/note_cmd.py index 696f99d..ada34aa 100644 --- a/pubs/commands/note_cmd.py +++ b/pubs/commands/note_cmd.py @@ -1,5 +1,4 @@ from .. import repo -from .. import content from ..uis import get_ui from ..utils import resolve_citekey from ..completion import CiteKeyCompletion @@ -8,8 +7,7 @@ from ..completion import CiteKeyCompletion def parser(subparsers, conf): parser = subparsers.add_parser('note', 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) return parser @@ -19,6 +17,6 @@ def command(conf, args): ui = get_ui() rp = repo.Repository(conf) citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True) - notepath = rp.databroker.real_notepath(citekey) + notepath = rp.databroker.real_notepath(citekey, rp.conf['main']['note_extension']) ui.edit_file(notepath, temporary=False) rp.close() diff --git a/pubs/config/spec.py b/pubs/config/spec.py index 220ec53..3d56117 100644 --- a/pubs/config/spec.py +++ b/pubs/config/spec.py @@ -24,6 +24,9 @@ open_cmd = string(default=None) # variable $EDITOR. edit_cmd = string(default='') +# Which default extension to use when creating a note file. +note_extension = string(default='txt') + # If true debug mode is on which means exceptions are not catched and # the full python stack is printed. debug = boolean(default=False) diff --git a/pubs/databroker.py b/pubs/databroker.py index 4e5c0be..d4a5274 100644 --- a/pubs/databroker.py +++ b/pubs/databroker.py @@ -2,6 +2,7 @@ from . import filebroker from . import endecoder from .p3 import pickle + class DataBroker(object): """ DataBroker class @@ -96,14 +97,16 @@ class DataBroker(object): # notesbroker - def real_notepath(self, citekey): - notepath = 'notesdir://{}.txt'.format(citekey) - return self.notebroker.real_docpath(notepath) + def _notepath(self, citekey, extension): + return 'notesdir://{}.{}'.format(citekey, extension) + + def real_notepath(self, citekey, extension): + return self.notebroker.real_docpath(self._notepath(citekey, extension)) - def remove_note(self, citekey, silent=True): - notepath = 'notesdir://{}.txt'.format(citekey) - return self.notebroker.remove_doc(notepath, silent=silent) + def remove_note(self, citekey, extension, silent=True): + return self.notebroker.remove_doc(self._notepath(citekey, extension), + silent=silent) - def rename_note(self, old_citekey, new_citekey): - notepath = 'notesdir://{}.txt'.format(old_citekey) - return self.notebroker.rename_doc(notepath, new_citekey) + def rename_note(self, old_citekey, new_citekey, extension): + return self.notebroker.rename_doc( + self._notepath(old_citekey, extension), new_citekey) diff --git a/pubs/datacache.py b/pubs/datacache.py index 69f32ad..f9b4960 100644 --- a/pubs/datacache.py +++ b/pubs/datacache.py @@ -187,11 +187,11 @@ class DataCache(object): # notesbroker - def real_notepath(self, citekey): - return self.databroker.real_notepath(citekey) + def real_notepath(self, citekey, extension): + return self.databroker.real_notepath(citekey, extension) - def remove_note(self, citekey, silent=True): - return self.databroker.remove_note(citekey, silent=True) + def remove_note(self, citekey, extension, silent=True): + return self.databroker.remove_note(citekey, extension, silent=True) - def rename_note(self, old_citekey, new_citekey): - return self.databroker.rename_note(old_citekey, new_citekey) + def rename_note(self, old_citekey, new_citekey, extension): + return self.databroker.rename_note(old_citekey, new_citekey, extension) diff --git a/pubs/repo.py b/pubs/repo.py index 2860f79..32026d3 100644 --- a/pubs/repo.py +++ b/pubs/repo.py @@ -107,7 +107,8 @@ class Repository(object): if remove_doc: self.remove_doc(citekey, detach_only=True) try: - self.databroker.remove_note(citekey, silent=True) + self.databroker.remove_note(citekey, self.conf['main']['note_extension'], + silent=True) except IOError: pass # FIXME: if IOError is about being unable to # remove the file, we need to issue an error. @@ -157,7 +158,8 @@ class Repository(object): # move note file if necessary try: - self.databroker.rename_note(old_citekey, new_citekey) + self.databroker.rename_note(old_citekey, new_citekey, + self.conf['main']['note_extension']) except IOError: pass diff --git a/tests/fake_env.py b/tests/fake_env.py index 7169cdb..f420efc 100644 --- a/tests/fake_env.py +++ b/tests/fake_env.py @@ -68,9 +68,13 @@ class FakeInput(): for md in self.module_list: md.input = self md._editor_input = self + md._edit_file = self.input_to_file # if mdname.endswith('files'): # md.editor_input = self + def input_to_file(self, _, path_to_file, temporary=True): + content.write_file(path_to_file, self()) + def add_input(self, inp): self.inputs.append(inp) diff --git a/tests/test_databroker.py b/tests/test_databroker.py index 0723421..49c46a7 100644 --- a/tests/test_databroker.py +++ b/tests/test_databroker.py @@ -40,7 +40,6 @@ class TestDataBroker(fake_env.TestFakeFs): self.assertEqual(pulled[key], page99_bibentry['Page99'][key]) self.assertEqual(db.pull_bibentry('citekey1'), page99_bibentry) - def test_existing_data(self): ende = endecoder.EnDecoder() diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 56553d0..fd191f6 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -151,6 +151,10 @@ class DataCommandTestCase(CommandTestCase): # fake_env.copy_dir(self.fs, os.path.join(os.path.dirname(__file__), 'data'), 'data') # fake_env.copy_dir(self.fs, os.path.join(os.path.dirname(__file__), 'bibexamples'), 'bibexamples') + def assertFileContentEqual(self, path, expected_content): + self.assertTrue(os.path.isfile(path)) + self.assertEqual(content.get_content(path), expected_content) + # Actual tests @@ -391,6 +395,68 @@ class TestTag(DataCommandTestCase): self.execute_cmds(cmds) +class TestNote(DataCommandTestCase): + + def setUp(self): + super(TestNote, 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_edit(self): + cmds = [('pubs note Page99', ['xxx']), + ] + self.execute_cmds(cmds) + self.assertFileContentEqual(os.path.join(self.note_dir, 'Page99.txt'), + 'xxx') + + def test_note_edit_extension(self): + config = conf.load_conf() + config['main']['note_extension'] = 'md' + conf.save_conf(config) + cmds = [('pubs note Page99', ['xxx']), + ] + self.execute_cmds(cmds) + self.assertEqual(set(os.listdir(self.note_dir)), {'Page99.md'}) + self.assertFileContentEqual(os.path.join(self.note_dir, 'Page99.md'), + 'xxx') + + def test_rename_with_note(self): + config = conf.load_conf() + conf.save_conf(config) + cmds = [('pubs note Page99', ['xxx']), + 'pubs rename Page99 Page1999', + ] + self.execute_cmds(cmds) + self.assertEqual(set(os.listdir(self.note_dir)), {'Page1999.txt'}) + self.assertFileContentEqual(os.path.join(self.note_dir, 'Page1999.txt'), + 'xxx') + + def test_rename_with_note_extension(self): + config = conf.load_conf() + config['main']['note_extension'] = 'md' + conf.save_conf(config) + cmds = [('pubs note Page99', ['xxx']), + 'pubs rename Page99 Page1999', + ] + self.execute_cmds(cmds) + self.assertEqual(set(os.listdir(self.note_dir)), {'Page1999.md'}) + self.assertFileContentEqual(os.path.join(self.note_dir, 'Page1999.md'), + 'xxx') + + def test_remove_with_note_extension(self): + config = conf.load_conf() + config['main']['note_extension'] = 'md' + conf.save_conf(config) + cmds = [('pubs note Page99', ['xxx']), + ('pubs remove Page99', ['y']), + ] + self.execute_cmds(cmds) + self.assertEqual(os.listdir(self.note_dir), []) + + class TestUsecase(DataCommandTestCase): def test_first(self): @@ -502,14 +568,14 @@ class TestUsecase(DataCommandTestCase): ('pubs list', [], line2), ('pubs edit Page99', [bib3]), ('pubs list', [], line3), - ] + ] self.execute_cmds(cmds) def test_export(self): cmds = ['pubs init', ('pubs add', [str_fixtures.bibtex_external0]), 'pubs export Page99', - ] + ] outs = self.execute_cmds(cmds) self.assertEqual(endecoder.EnDecoder().decode_bibdata(outs[2]), fixtures.page_bibentry)