On Thu, Feb 8, 2018 at 4:50 PM, Stefano Zacchiroli <z...@upsilon.cc> wrote:

> On Thu, Feb 08, 2018 at 12:08:40PM +0800, Oon-Ee Ng wrote:
> > Also I use fava's plugin capabilities to commit every time an edit
> > happens.
>
> Can you tell us more about this? It's the first time I hear about it,
> and it looks interesting to me. Also, given "git commit + git push" is
> probably a standard need for many people editing via Fava, would a
> "official" plugin for doing this be welcome?
>

Sorry, haven't been on for a couple of weeks. The plugin is dead simple,
and ONLY does commit, as handling push/pull would necessitate manual
intervention on conflicts, so I do that using a bash script instead.

The proper name is 'fava extension', and on any recent version of fava you
can find documentation about it on the Help page. It seems there's a recent
fava-plugins project to collect these[1].

My plugin looks like this:-

import os
import subprocess
from fava.ext import FavaExtensionBase
'''
Example hook for fava.

The hook add_transaction_git_commit will automatically add all files with
the
beancount suffix in fava's root folder and commit them with a message based
on
the transaction's narration, payee, and date.

To limit external dependencies, this uses the subprocess module (2.4+).
Please
read the documentation for that[1], especially if you're considering using
shell=True (probably a bad idea).

[1] - https://docs.python.org/2/library/subprocess.html
'''

class AutoCommit(FavaExtensionBase):
    def _run(self, args):
        cwd = os.path.dirname(self.ledger.beancount_file_path)
        subprocess.call(args, cwd=cwd, stdout=subprocess.DEVNULL)

    def after_write_source(self, path, _):
        message = 'autocommit: file saved'
        self._run(["git", "add", path])
        self._run(["git", "commit", "-m", message])

    def after_insert_metadata(self, *_):
        message = 'autocommit: metadata added'
        self._run(["git", "commit", "-am", message])

    def after_insert_entry(self, entry):
        message = 'autocommit:'
        if entry.narration:
            message += '"{}"'.format(entry.narration)
        if entry.payee:
            if message:
                message += ' paid to "{}"'.format(entry.payee)
            else:
                message = 'Payment to "{}"'.format(entry.payee)
        if message:
            message += ' on {}'.format(entry.date)
        else:
            message += 'entry on {}'.format(entry.date)
        self._run(["git", "commit", "-am", message])

I'm also planning a PR to extend fava's extensions capabilities[2] such
that they can modify the transactions themselves, but haven't had time to
get round to it (yet).

[1] - https://github.com/beancount/fava-plugins
[2] - https://github.com/beancount/fava/issues/564

-- 
You received this message because you are subscribed to the Google Groups 
"Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to beancount+unsubscr...@googlegroups.com.
To post to this group, send email to beancount@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beancount/CAGQ70esxYhtkjkNgU5%2BOBjjh5%3Dgidn9xWGjKFuu4LoG_5ce1Dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to