From 1ad64d7859cdc5822db16c4f2cdbc6124f0ecaa8 Mon Sep 17 00:00:00 2001 From: Fabien Benureau Date: Mon, 11 Nov 2013 04:07:42 +0100 Subject: [PATCH] notes cmd --- papers/commands/__init__.py | 1 + papers/commands/note_cmd.py | 27 +++++++++++++++++++++++++++ papers/databroker.py | 18 ++++++++++++++---- papers/datacache.py | 15 ++++++++++++--- papers/filebroker.py | 30 ++++++++++++++++-------------- papers/papers_cmd.py | 1 + papers/repo.py | 2 +- tests/test_databroker.py | 4 ++-- tests/test_filebroker.py | 6 +++--- tests/test_usecase.py | 4 ++-- 10 files changed, 79 insertions(+), 29 deletions(-) create mode 100644 papers/commands/note_cmd.py diff --git a/papers/commands/__init__.py b/papers/commands/__init__.py index f832ba3..bc524c7 100644 --- a/papers/commands/__init__.py +++ b/papers/commands/__init__.py @@ -8,6 +8,7 @@ import list_cmd import attach_cmd import open_cmd import tag_cmd +import note_cmd # bulk import export_cmd import import_cmd diff --git a/papers/commands/note_cmd.py b/papers/commands/note_cmd.py new file mode 100644 index 0000000..073c46d --- /dev/null +++ b/papers/commands/note_cmd.py @@ -0,0 +1,27 @@ +from .. import repo +from .. import content +from ..configs import config +from ..uis import get_ui + +def parser(subparsers): + parser = subparsers.add_parser('note', + help='edit the note attached to a paper') + parser.add_argument('citekey', + help='citekey of the paper') + return parser + + +def command(args): + """ + """ + + ui = get_ui() + + + rp = repo.Repository(config()) + if not rp.databroker.exists(args.citekey): + ui.error("citekey {} not found".format(args.citekey)) + ui.exit(1) + + notepath = rp.databroker.real_notepath('notesdir://{}.txt'.format(args.citekey)) + content.edit_file(config().edit_cmd, notepath, temporary=False) diff --git a/papers/databroker.py b/papers/databroker.py index 5679d53..e5c5680 100644 --- a/papers/databroker.py +++ b/papers/databroker.py @@ -12,7 +12,8 @@ class DataBroker(object): def __init__(self, directory, create=False): self.filebroker = filebroker.FileBroker(directory, create=create) self.endecoder = endecoder.EnDecoder() - self.docbroker = filebroker.DocBroker(directory) + self.docbroker = filebroker.DocBroker(directory, scheme='docsdir', subdir='doc') + self.notebroker = filebroker.DocBroker(directory, scheme='notesdir', subdir='notes') # filebroker+endecoder @@ -52,8 +53,8 @@ class DataBroker(object): # docbroker - def is_pubsdir_doc(self, docpath): - return self.docbroker.is_pubsdir_doc(docpath) + def in_docsdir(self, docpath): + return self.docbroker.in_docsdir(docpath) def copy_doc(self, citekey, source_path, overwrite=False): return self.docbroker.copy_doc(citekey, source_path, overwrite=overwrite) @@ -62,4 +63,13 @@ class DataBroker(object): return self.docbroker.remove_doc(docpath, silent=silent) def real_docpath(self, docpath): - return self.docbroker.real_docpath(docpath) \ No newline at end of file + return self.docbroker.real_docpath(docpath) + + + # notesbroker + + def in_notesdir(self, docpath): + return self.notebroker.in_docsdir(docpath) + + def real_notepath(self, docpath): + return self.notebroker.real_docpath(docpath) \ No newline at end of file diff --git a/papers/datacache.py b/papers/datacache.py index 9d7a1f6..ef6646d 100644 --- a/papers/datacache.py +++ b/papers/datacache.py @@ -60,10 +60,10 @@ class DataCache(object): """Will return None if bibdata_raw can't be decoded""" return self.databroker.verify(bibdata_raw) - # docbroker + # docbroker - def is_pubsdir_doc(self, docpath): - return self.databroker.is_pubsdir_doc(docpath) + def in_docsdir(self, docpath): + return self.databroker.in_docsdir(docpath) def copy_doc(self, citekey, source_path, overwrite=False): return self.databroker.copy_doc(citekey, source_path, overwrite=overwrite) @@ -74,6 +74,15 @@ class DataCache(object): def real_docpath(self, docpath): return self.databroker.real_docpath(docpath) + # notesbroker + + def in_notesdir(self, docpath): + return self.databroker.in_notesdir(docpath) + + def real_notepath(self, docpath): + return self.databroker.real_notepath(docpath) + + # class ChangeTracker(object): # def __init__(self, cache, directory): diff --git a/papers/filebroker.py b/papers/filebroker.py index aa43e74..1b257ce 100644 --- a/papers/filebroker.py +++ b/papers/filebroker.py @@ -107,38 +107,37 @@ class DocBroker(object): * only one document can be attached to a paper (might change in the future) * this document can be anything, the content is never processed. - * these document have an adress of the type "pubsdir://doc/citekey.pdf" + * these document have an adress of the type "docsdir://citekey.pdf" + * docsdir:// correspond to /path/to/pubsdir/doc (configurable) * document outside of the repository will not be removed. * deliberately, there is no move_doc method. """ - def __init__(self, directory): - self.docdir = os.path.join(directory, 'doc') + def __init__(self, directory, scheme='docsdir', subdir='doc'): + self.scheme = scheme + self.docdir = os.path.join(directory, subdir) if not check_directory(self.docdir, fail = False): os.mkdir(self.docdir) - def is_pubsdir_doc(self, docpath): + def in_docsdir(self, docpath): try: parsed = urlparse.urlparse(docpath) except Exception: return False - if parsed.scheme == 'pubsdir': - assert parsed.netloc == 'doc' - assert parsed.path[0] == '/' - return parsed.scheme == 'pubsdir' + return parsed.scheme == self.scheme def copy_doc(self, citekey, source_path, overwrite=False): """ Copy a document to the pubsdir/doc, and return the location The document will be named {citekey}.{ext}. - The location will be pubsdir://doc/{citekey}.{ext}. + The location will be docsdir://{citekey}.{ext}. :param overwrite: will overwrite existing file. :return: the above location """ full_source_path = self.real_docpath(source_path) check_file(full_source_path) - target_path = 'pubsdir://' + os.path.join('doc', citekey + os.path.splitext(source_path)[-1]) + target_path = '{}://{}'.format(self.scheme, citekey + os.path.splitext(source_path)[-1]) full_target_path = self.real_docpath(target_path) if not overwrite and check_file(full_target_path, fail=False): raise IOError('{} file exists.'.format(full_target_path)) @@ -147,11 +146,11 @@ class DocBroker(object): return target_path def remove_doc(self, docpath, silent=True): - """ Will remove only file hosted in pubsdir://doc/ + """ Will remove only file hosted in docsdir:// :raise ValueError: for other paths, unless :param silent: is True """ - if not self.is_pubsdir_doc(docpath): + if not self.in_docsdir(docpath): if not silent: raise ValueError(('the file to be removed {} is set as external. ' 'you should remove it manually.').format(docpath)) @@ -165,7 +164,10 @@ class DocBroker(object): Essentially transform pubsdir://doc/{citekey}.{ext} to /path/to/pubsdir/doc/{citekey}.{ext}. Return absoluted paths of regular ones otherwise. """ - if self.is_pubsdir_doc(docpath): + if self.in_docsdir(docpath): parsed = urlparse.urlparse(docpath) - docpath = os.path.join(self.docdir, parsed.path[1:]) + if parsed.path == '': + docpath = os.path.join(self.docdir, parsed.netloc) + else: + docpath = os.path.join(self.docdir, parsed.netloc, parsed.path[1:]) return os.path.normpath(os.path.abspath(docpath)) diff --git a/papers/papers_cmd.py b/papers/papers_cmd.py index a308f9d..0433fc5 100644 --- a/papers/papers_cmd.py +++ b/papers/papers_cmd.py @@ -22,6 +22,7 @@ CORE_CMDS = collections.OrderedDict([ ('attach', commands.attach_cmd), ('open', commands.open_cmd), ('tag', commands.tag_cmd), + ('note', commands.note_cmd), ('export', commands.export_cmd), ('import', commands.import_cmd), diff --git a/papers/repo.py b/papers/repo.py index e717433..7469d66 100644 --- a/papers/repo.py +++ b/papers/repo.py @@ -109,7 +109,7 @@ class Repository(object): paper.bibdata = new_bibdata # move doc file if necessary - if self.databroker.is_pubsdir_doc(paper.docpath): + if self.databroker.in_docsdir(paper.docpath): new_docpath = self.databroker.copy_doc(new_citekey, paper.docpath) self.databroker.remove_doc(paper.docpath) paper.docpath = new_docpath diff --git a/tests/test_databroker.py b/tests/test_databroker.py index 255585b..7e4971a 100644 --- a/tests/test_databroker.py +++ b/tests/test_databroker.py @@ -67,8 +67,8 @@ class TestDataBroker(TestFakeFs): with self.assertRaises(IOError): db.pull_metadata('citekey') - db.copy_doc('Larry99', 'pubsdir://doc/Page99.pdf') + db.copy_doc('Larry99', 'docsdir://Page99.pdf') self.assertTrue(content.check_file('repo/doc/Page99.pdf', fail=False)) self.assertTrue(content.check_file('repo/doc/Larry99.pdf', fail=False)) - db.remove_doc('pubsdir://doc/Page99.pdf') + db.remove_doc('docsdir://Page99.pdf') diff --git a/tests/test_filebroker.py b/tests/test_filebroker.py index 2691dad..32833a8 100644 --- a/tests/test_filebroker.py +++ b/tests/test_filebroker.py @@ -101,9 +101,9 @@ class TestDocBroker(TestFakeFs): docpath = docb.copy_doc('Page99', 'data/pagerank.pdf') self.assertTrue(content.check_file(os.path.join('tmpdir', 'doc/Page99.pdf'))) - self.assertTrue(docb.is_pubsdir_doc(docpath)) - self.assertEqual(docpath, 'pubsdir://doc/Page99.pdf') - docb.remove_doc('pubsdir://doc/Page99.pdf') + self.assertTrue(docb.in_docsdir(docpath)) + self.assertEqual(docpath, 'docsdir://Page99.pdf') + docb.remove_doc('docsdir://Page99.pdf') self.assertFalse(content.check_file(os.path.join('tmpdir', 'doc/Page99.pdf'), fail=False)) with self.assertRaises(IOError): diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 5688304..b6c7c77 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -100,13 +100,13 @@ class TestInit(CommandTestCase): pubsdir = os.path.expanduser('~/papers_test2') papers_cmd.execute('papers init -p {}'.format(pubsdir).split()) self.assertEqual(set(self.fs['os'].listdir(pubsdir)), - {'bib', 'doc', 'meta'}) + {'bib', 'doc', 'meta', 'notes'}) def test_init2(self): pubsdir = os.path.expanduser('~/.papers') papers_cmd.execute('papers init'.split()) self.assertEqual(set(self.fs['os'].listdir(pubsdir)), - {'bib', 'doc', 'meta'}) + {'bib', 'doc', 'meta', 'notes'}) class TestAdd(DataCommandTestCase):