From b411bd16d8297ff03a55b80dd8d0ff6d91b0991c Mon Sep 17 00:00:00 2001 From: "Amlesh Sivanantham (zamlz)" Date: Thu, 28 Feb 2019 09:35:57 -0800 Subject: [PATCH] Setup PaperEvent class. Git plugin only listens to this class --- pubs/events.py | 44 ++++++++++++++--------------- pubs/plugs/git/git.py | 64 +++++++------------------------------------ pubs/repo.py | 21 ++++++++------ 3 files changed, 44 insertions(+), 85 deletions(-) diff --git a/pubs/events.py b/pubs/events.py index 24666e0..3dfbc75 100644 --- a/pubs/events.py +++ b/pubs/events.py @@ -25,39 +25,37 @@ class Event(object): return wrap -class RemoveEvent(Event): +class PaperEvent(Event): + _format = "Unknown modification of paper {citekey}." + def __init__(self, citekey): self.citekey = citekey + @property + def description(self): + return self._format.format(citekey=self.citekey) -class RenameEvent(Event): - def __init__(self, paper, old_citekey): - self.paper = paper - self.old_citekey = old_citekey - -class AddEvent(Event): - def __init__(self, citekey): - self.citekey = citekey +class AddEvent(PaperEvent): + _format = "Added paper {citekey}." -class EditEvent(Event): - def __init__(self, citekey): - self.citekey = citekey +class RemoveEvent(PaperEvent): + _format = "Removes paper {citekey}." -class TagEvent(Event): - def __init__(self, citekey): - self.citekey = citekey +class ModifyEvent(PaperEvent): + _format = "Modifies paper {citekey}." -class DocEvent(Event): - """possible actions: add, remove""" - def __init__(self, citekey, action): - self.citekey - self.action = action +class RenameEvent(PaperEvent): + _format = "Renames paper {old_citekey} to {citekey}." + def __init__(self, paper, old_citekey): + super(RenameEvent, self).__init__(paper.citekey) + self.paper = paper + self.old_citekey = old_citekey -class NoteEvent(Event): - def __init__(self, citekey, action): - self.citekey + @property + def description(self): + return self._format.format(citekey=self.citekey, old_citekey=self.old_citekey) diff --git a/pubs/plugs/git/git.py b/pubs/plugs/git/git.py index 8a8377a..82692c4 100644 --- a/pubs/plugs/git/git.py +++ b/pubs/plugs/git/git.py @@ -27,62 +27,18 @@ class GitPlugin(PapersPlugin): subprocess.call('git -C {} {}'.format(self.pubsdir, cmd), shell=True) -@RenameEvent.listen() -def git_rename(RenameEventInstance): - new_key = RenameEventInstance.paper.citekey - old_key = RenameEventInstance.old_citekey - - # Stage the changes and commit - git = GitPlugin.get_instance() - git.shell("add \*/{}.\*".format(old_key)) - git.shell("add \*/{}.\*".format(new_key)) - git.shell('commit -m "Renamed citekey {} to {}"'.format(old_key, new_key)) - - -@RemoveEvent.listen() -def git_remove(RemoveEventInstance): - citekey = RemoveEventInstance.citekey - - # Stage the changes and commit - git = GitPlugin.get_instance() - git.shell("add \*/{}.\*".format(citekey)) - git.shell('commit -m "Removed files for {}"'.format(citekey)) +@PaperEvent.listen() +def git_commit_event(PaperEventInstance): + citekey = PaperEventInstance.citekey - -@AddEvent.listen() -def git_add(AddEventInstance): - citekey = AddEventInstance.citekey + if isinstance(PaperEventInstance, RenameEvent): + old_citekey = RenameEventInstance.old_citekey + else: + old_citekey = None # Stage the changes and commit git = GitPlugin.get_instance() + if old_citekey: + git.shell("add \*/{}.\*".format(old_citekey)) git.shell("add \*/{}.\*".format(citekey)) - git.shell('commit -m "Added files for {}"'.format(citekey)) - - -@EditEvent.listen() -def git_edit(EditEventInstance): - pass - - -@TagEvent.listen() -def git_tag(TagEventInstance): - pass - - -@DocEvent.listen() -def git_doc(DocEventInstance): - citekey = DocEventInstance.citekey - - # Stage the changes and commit - git = GitPlugin.get_instance() - git.shell("add \*/{}.\*".format(citekey)) - if DocEventInstance.action == 'add': - git.shell('commit -m "Added document for {}"'.format(citekey)) - elif DocEventInstance.action == 'remove': - git.shell('commit -m "Removed document for {}"'.format(citekey)) - - -@NoteEvent.listen() -def git_note(NoteEventInstance): - pass - + git.shell('commit -m "{}"'.format(PaperEventInstance.description)) diff --git a/pubs/repo.py b/pubs/repo.py index eedfff4..69ca1d7 100644 --- a/pubs/repo.py +++ b/pubs/repo.py @@ -102,8 +102,10 @@ class Repository(object): def remove_paper(self, citekey, remove_doc=True, event=True): """ Remove a paper. Is silent if nothing needs to be done.""" + if event: + events.RemoveEvent(citekey).send() if remove_doc: - self.remove_doc(citekey, detach_only=True, event=False) + self.remove_doc(citekey, detach_only=True) try: self.databroker.remove_note(citekey, self.conf['main']['note_extension'], silent=True) @@ -113,10 +115,8 @@ class Repository(object): pass self.citekeys.remove(citekey) self.databroker.remove(citekey) - if event: - events.RemoveEvent(citekey).send() - def remove_doc(self, citekey, detach_only=False, event=True): + def remove_doc(self, citekey, detach_only=False): """ Remove a doc. Is silent if nothing needs to be done.""" try: metadata = self.databroker.pull_metadata(citekey) @@ -126,8 +126,6 @@ class Repository(object): p = self.pull_paper(citekey) p.docpath = None self.push_paper(p, overwrite=True, event=False) - if event: - events.DocEvent(citekey, 'remove').send() except IOError: # FIXME: if IOError is about being unable to # remove the file, we need to issue an error.I @@ -194,8 +192,15 @@ class Repository(object): p.docpath = docfile self.push_paper(p, overwrite=True, event=False) - def unique_citekey(self, base_key): - """Create a unique citekey for a given basekey.""" + def unique_citekey(self, base_key, bibentry): + """Create a unique citekey for a given base key. + + :param base_key: the base key in question. + :param bibentry: the bib entry to possibly generate the citekey. + """ + if not bibstruct.valid_citekey(base_key): + base_key = bibstruct.generate_citekey(bibentry) + # TODO: check that the generated citekey does not have a slash too. for n in itertools.count(): if not base_key + _base27(n) in self.citekeys: return base_key + _base27(n)