@@ -102,24 +102,48 @@ class Ledger:
p.stdin.write(command + b'\n')
p.stdin.flush()
output = self.read_until_prompt(p)
self.ledger_process.send_signal(subprocess.signal.SIGTERM)
_log.debug('Waiting for ledger to shut down')
self.ledger_process.wait()
self.ledger_process = None
return output
def add_transaction(self, transaction):
transaction_template = ('\n{date} {t.payee}\n'
'{postings}')
posting_template = (' {account} {p.amount.symbol}'
' {p.amount.amount}\n')
output = b''
output += transaction_template.format(
date=transaction.date.strftime('%Y-%m-%d'),
t=transaction,
postings=''.join([posting_template.format(
p=p,
account=p.account + ' ' * (
80 - (len(p.account) + len(p.amount.symbol) +
len(p.amount.amount) + 1 + 2)
)) for p in transaction.postings])).encode('utf8')
with open(self.ledger_file, 'ab') as f:
f.write(output)
_log.debug('written to file: %s', output)
def bal(self):
output = self.send_command('xml')
if output is None:
raise RuntimeError('bal call returned no output')
accounts = []
xml = ElementTree.fromstring(output.decode('utf8'))
accounts = self._recurse_accounts(xml.find('./accounts'))