From 148917c70c8257e9a6fa31a0cd56d06d03afbb2b Mon Sep 17 00:00:00 2001 From: Fabien Benureau Date: Tue, 12 Nov 2013 13:34:39 +0100 Subject: [PATCH] move_doc in docbroker + consequences + remove_cmd remove notes too --- papers/commands/note_cmd.py | 2 +- papers/databroker.py | 29 ++++++++++++++----------- papers/datacache.py | 24 ++++++++++----------- papers/filebroker.py | 43 +++++++++++++++++++++++++------------ papers/repo.py | 14 ++++++------ 5 files changed, 64 insertions(+), 48 deletions(-) diff --git a/papers/commands/note_cmd.py b/papers/commands/note_cmd.py index 073c46d..ad2a3eb 100644 --- a/papers/commands/note_cmd.py +++ b/papers/commands/note_cmd.py @@ -23,5 +23,5 @@ def command(args): ui.error("citekey {} not found".format(args.citekey)) ui.exit(1) - notepath = rp.databroker.real_notepath('notesdir://{}.txt'.format(args.citekey)) + notepath = rp.databroker.real_notepath(args.citekey) content.edit_file(config().edit_cmd, notepath, temporary=False) diff --git a/papers/databroker.py b/papers/databroker.py index 7ffa6ee..7614a43 100644 --- a/papers/databroker.py +++ b/papers/databroker.py @@ -56,26 +56,29 @@ class DataBroker(object): 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) + def real_docpath(self, docpath): + return self.docbroker.real_docpath(docpath) + + def add_doc(self, citekey, source_path, overwrite=False): + return self.docbroker.add_doc(citekey, source_path, overwrite=overwrite) def remove_doc(self, docpath, silent=True): return self.docbroker.remove_doc(docpath, silent=silent) - def real_docpath(self, docpath): - return self.docbroker.real_docpath(docpath) + def rename_doc(self, docpath, new_citekey): + return self.docbroker.rename_doc(docpath, new_citekey) # notesbroker - def in_notesdir(self, docpath): - return self.notebroker.in_docsdir(docpath) - - def copy_note(self, citekey, source_path, overwrite=False): - return self.notebroker.copy_doc(citekey, source_path, overwrite=overwrite) + def real_notepath(self, citekey): + notepath = 'notesdir://{}.txt'.format(citekey) + return self.notebroker.real_docpath(notepath) - def remove_note(self, docpath, silent=True): - return self.notebroker.remove_doc(docpath, silent=silent) + def remove_note(self, citekey, silent=True): + notepath = 'notesdir://{}.txt'.format(citekey) + return self.notebroker.remove_doc(notepath, 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 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 636d8e5..8dbec1a 100644 --- a/papers/datacache.py +++ b/papers/datacache.py @@ -65,28 +65,28 @@ class DataCache(object): def in_docsdir(self, docpath): return self.databroker.in_docsdir(docpath) + def real_docpath(self, docpath): + return self.databroker.real_docpath(docpath) + def copy_doc(self, citekey, source_path, overwrite=False): - return self.databroker.copy_doc(citekey, source_path, overwrite=overwrite) + return self.databroker.add_doc(citekey, source_path, overwrite=overwrite) def remove_doc(self, docpath, silent=True): return self.databroker.remove_doc(docpath, silent=silent) - def real_docpath(self, docpath): - return self.databroker.real_docpath(docpath) + def rename_doc(self, docpath, new_citekey): + return self.databroker.rename_doc(docpath, new_citekey) # notesbroker - def in_notesdir(self, docpath): - return self.databroker.in_notesdir(docpath) - - def copy_note(self, citekey, source_path, overwrite=False): - return self.databroker.copy_note(citekey, source_path, overwrite=overwrite) + def real_notepath(self, citekey): + return self.databroker.real_notepath(citekey) - def remove_note(self, docpath, silent=True): - return self.databroker.remove_note(docpath, silent=silent) + def remove_note(self, citekey, silent=True): + return self.databroker.remove_note(citekey, silent=True) - def real_notepath(self, docpath): - return self.databroker.real_notepath(docpath) + def rename_note(self, old_citekey, new_citekey): + return self.databroker.rename_note(old_citekey, new_citekey) # class ChangeTracker(object): diff --git a/papers/filebroker.py b/papers/filebroker.py index c721950..a723ab9 100644 --- a/papers/filebroker.py +++ b/papers/filebroker.py @@ -110,7 +110,7 @@ class DocBroker(object): * 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. + * move_doc only applies from inside to inside the docsdir """ def __init__(self, directory, scheme='docsdir', subdir='doc'): @@ -129,8 +129,21 @@ class DocBroker(object): # def doc_exists(self, citekey, ext='.txt'): # return check_file(os.path.join(self.docdir, citekey + ext), fail=False) - def copy_doc(self, citekey, source_path, overwrite=False): - """ Copy a document to the pubsdir/doc, and return the location + def real_docpath(self, docpath): + """Return the full path + Essentially transform pubsdir://doc/{citekey}.{ext} to /path/to/pubsdir/doc/{citekey}.{ext}. + Return absoluted paths of regular ones otherwise. + """ + if self.in_docsdir(docpath): + parsed = urlparse.urlparse(docpath) + 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)) + + def add_doc(self, citekey, source_path, overwrite=False): + """ Add a document to the docsdir, and return its location. The document will be named {citekey}.{ext}. The location will be docsdir://{citekey}.{ext}. @@ -162,15 +175,17 @@ class DocBroker(object): if check_file(filepath): os.remove(filepath) - def real_docpath(self, docpath): - """Return the full path - Essentially transform pubsdir://doc/{citekey}.{ext} to /path/to/pubsdir/doc/{citekey}.{ext}. - Return absoluted paths of regular ones otherwise. + def rename_doc(self, docpath, new_citekey): + """ Move a document inside the docsdir + + :raise IOError: if docpath doesn't point to a file + if new_citekey doc exists already. + :raise ValueError: if docpath is not in docsdir(). + + if an exception is raised, the files on disk haven't changed. """ - if self.in_docsdir(docpath): - parsed = urlparse.urlparse(docpath) - 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)) + if not self.in_docsdir(docpath): + raise ValueError('cannot rename an external file ({}).'.format(docpath)) + + new_notepath = self.add_doc(new_citekey, docpath) + self.remove_doc(docpath) \ No newline at end of file diff --git a/papers/repo.py b/papers/repo.py index 21b7a2c..df474ba 100644 --- a/papers/repo.py +++ b/papers/repo.py @@ -87,6 +87,7 @@ class Repository(object): metadata = self.databroker.pull_metadata(citekey) docpath = metadata.get('docfile') self.databroker.remove_doc(docpath, silent=True) + self.databroker.remove_note(citekey, silent=True) except IOError: pass # FXME: if IOError is about being unable to # remove the file, we need to issue an error.I @@ -103,6 +104,7 @@ class Repository(object): # check if new_citekey does not exists if self.databroker.exists(new_citekey, both=False): raise IOError("can't rename paper to {}, conflicting files exists".format(new_citekey)) + # modify bibdata (__delitem__ not implementd by pybtex) new_bibdata = BibliographyData() new_bibdata.entries[new_citekey] = paper.bibdata.entries[old_citekey] @@ -110,17 +112,13 @@ class Repository(object): # move doc file if necessary 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 + paper.docpath = self.databroker.rename_doc(paper.docpath, new_citekey) + # move note file if necessary try: - old_notepath = 'notesdir://{}.txt'.format(old_citekey) - new_notepath = self.databroker.copy_note(new_citekey, old_notepath) - self.databroker.remove_notei(old_notepath) + self.databroker.rename_note(old_citekey, new_citekey) except IOError: - import traceback - traceback.print_exc() + pass # push_paper to new_citekey paper.citekey = new_citekey