[notes] Allow different file extension for note file (see #70).
This commit is contained in:
parent
cd63575034
commit
4ffd62fe9f
@ -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()
|
||||
|
@ -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)
|
||||
|
@ -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 remove_note(self, citekey, silent=True):
|
||||
notepath = 'notesdir://{}.txt'.format(citekey)
|
||||
return self.notebroker.remove_doc(notepath, silent=silent)
|
||||
def real_notepath(self, citekey, extension):
|
||||
return self.notebroker.real_docpath(self._notepath(citekey, extension))
|
||||
|
||||
def rename_note(self, old_citekey, new_citekey):
|
||||
notepath = 'notesdir://{}.txt'.format(old_citekey)
|
||||
return self.notebroker.rename_doc(notepath, new_citekey)
|
||||
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, extension):
|
||||
return self.notebroker.rename_doc(
|
||||
self._notepath(old_citekey, extension), new_citekey)
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user