summaryrefslogtreecommitdiff
path: root/emacs.py
blob: 3398ec9c50b12fee5c8b9cbfc485cea6cf7365a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sys

from ledger import *

def emacs_date (seconds):
    return "(%d %d %d)" % (seconds / 65536, seconds % 65536, 0)

class EmacsFormatTransactions (TransactionHandler):
    last_entry = None
    output     = None

    def __init__ (self):
	self.last_entry = None

	if config.output_file:
	    self.output = open (config.output_file, "w")
	else:
	    self.output = sys.stdout

	TransactionHandler.__init__ (self)

    def __del__ (self):
	if config.output_file:
	    self.output.close ()

    def flush (self):
	self.output.write ("))\n")
	self.output.flush ()

    def write_entry (self, entry):
	self.output.write("%s %s %s %s\n" %
			  (emacs_date (entry.date),
			   (entry.state and "t") or "nil",
			   (entry.code and "\"%s\"" % entry.code) or "nil",
			   (entry.payee and "\"%s\"" % entry.payee) or "nil"))

    def __call__ (self, xact):
	if not transaction_has_xdata (xact) or \
	   not transaction_xdata (xact).dflags & TRANSACTION_DISPLAYED:
	    if self.last_entry is None:
		self.output.write("((")
		self.write_entry (xact.entry)
	    elif xact.entry != self.last_entry:
		self.output.write(")\n (")
		self.write_entry (xact.entry)
	    else:
		self.output.write("\n")
	    self.output.write("  (\"%s\" \"%s\"%s%s)" %
			      (xact.account.fullname (), xact.amount,
			       (xact.cost and " \"%s\"" % xact.cost) or "",
			       (xact.note and " \"%s\"" % xact.note) or ""))
	    self.last_entry = xact.entry
	    transaction_xdata (xact).dflags |= TRANSACTION_DISPLAYED