Setup PaperEvent class. Git plugin only listens to this class

main
Amlesh Sivanantham (zamlz) 6 years ago
parent 063d08183f
commit b411bd16d8
No known key found for this signature in database
GPG Key ID: 882C395C3B28902C

@ -25,39 +25,37 @@ class Event(object):
return wrap return wrap
class RemoveEvent(Event): class PaperEvent(Event):
_format = "Unknown modification of paper {citekey}."
def __init__(self, citekey): def __init__(self, citekey):
self.citekey = 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(PaperEvent):
class AddEvent(Event): _format = "Added paper {citekey}."
def __init__(self, citekey):
self.citekey = citekey
class EditEvent(Event): class RemoveEvent(PaperEvent):
def __init__(self, citekey): _format = "Removes paper {citekey}."
self.citekey = citekey
class TagEvent(Event): class ModifyEvent(PaperEvent):
def __init__(self, citekey): _format = "Modifies paper {citekey}."
self.citekey = citekey
class DocEvent(Event): class RenameEvent(PaperEvent):
"""possible actions: add, remove""" _format = "Renames paper {old_citekey} to {citekey}."
def __init__(self, citekey, action):
self.citekey
self.action = action
def __init__(self, paper, old_citekey):
super(RenameEvent, self).__init__(paper.citekey)
self.paper = paper
self.old_citekey = old_citekey
class NoteEvent(Event): @property
def __init__(self, citekey, action): def description(self):
self.citekey return self._format.format(citekey=self.citekey, old_citekey=self.old_citekey)

@ -27,62 +27,18 @@ class GitPlugin(PapersPlugin):
subprocess.call('git -C {} {}'.format(self.pubsdir, cmd), shell=True) subprocess.call('git -C {} {}'.format(self.pubsdir, cmd), shell=True)
@RenameEvent.listen() @PaperEvent.listen()
def git_rename(RenameEventInstance): def git_commit_event(PaperEventInstance):
new_key = RenameEventInstance.paper.citekey citekey = PaperEventInstance.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))
if isinstance(PaperEventInstance, RenameEvent):
@AddEvent.listen() old_citekey = RenameEventInstance.old_citekey
def git_add(AddEventInstance): else:
citekey = AddEventInstance.citekey old_citekey = None
# Stage the changes and commit # Stage the changes and commit
git = GitPlugin.get_instance() git = GitPlugin.get_instance()
if old_citekey:
git.shell("add \*/{}.\*".format(old_citekey))
git.shell("add \*/{}.\*".format(citekey)) git.shell("add \*/{}.\*".format(citekey))
git.shell('commit -m "Added files for {}"'.format(citekey)) git.shell('commit -m "{}"'.format(PaperEventInstance.description))
@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

@ -102,8 +102,10 @@ class Repository(object):
def remove_paper(self, citekey, remove_doc=True, event=True): def remove_paper(self, citekey, remove_doc=True, event=True):
""" Remove a paper. Is silent if nothing needs to be done.""" """ Remove a paper. Is silent if nothing needs to be done."""
if event:
events.RemoveEvent(citekey).send()
if remove_doc: if remove_doc:
self.remove_doc(citekey, detach_only=True, event=False) self.remove_doc(citekey, detach_only=True)
try: try:
self.databroker.remove_note(citekey, self.conf['main']['note_extension'], self.databroker.remove_note(citekey, self.conf['main']['note_extension'],
silent=True) silent=True)
@ -113,10 +115,8 @@ class Repository(object):
pass pass
self.citekeys.remove(citekey) self.citekeys.remove(citekey)
self.databroker.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.""" """ Remove a doc. Is silent if nothing needs to be done."""
try: try:
metadata = self.databroker.pull_metadata(citekey) metadata = self.databroker.pull_metadata(citekey)
@ -126,8 +126,6 @@ class Repository(object):
p = self.pull_paper(citekey) p = self.pull_paper(citekey)
p.docpath = None p.docpath = None
self.push_paper(p, overwrite=True, event=False) self.push_paper(p, overwrite=True, event=False)
if event:
events.DocEvent(citekey, 'remove').send()
except IOError: except IOError:
# FIXME: if IOError is about being unable to # FIXME: if IOError is about being unable to
# remove the file, we need to issue an error.I # remove the file, we need to issue an error.I
@ -194,8 +192,15 @@ class Repository(object):
p.docpath = docfile p.docpath = docfile
self.push_paper(p, overwrite=True, event=False) self.push_paper(p, overwrite=True, event=False)
def unique_citekey(self, base_key): def unique_citekey(self, base_key, bibentry):
"""Create a unique citekey for a given basekey.""" """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(): for n in itertools.count():
if not base_key + _base27(n) in self.citekeys: if not base_key + _base27(n) in self.citekeys:
return base_key + _base27(n) return base_key + _base27(n)

Loading…
Cancel
Save