diff --git a/accounting/__init__.py b/accounting/__init__.py index 3e7d414..35910a9 100644 --- a/accounting/__init__.py +++ b/accounting/__init__.py @@ -1,3 +1,4 @@ +import sys import subprocess import logging import time @@ -118,7 +119,10 @@ class Ledger: accounts = [] for line in output.split(b'\n'): - name, balance = line.decode('utf8').split('|') + try: + name, balance = line.decode('utf8').split('|') + except ValueError: + continue accounts.append(Account(name=name, balance=balance)) @@ -195,12 +199,14 @@ class Account: self=self) -def main(): +def main(argv=None): + if argv is None: + argv = sys.argv + logging.basicConfig(level=logging.DEBUG) ledger = Ledger(ledger_file='non-profit-test-data.ledger') print(ledger.bal()) print(ledger.reg()) if __name__ == '__main__': - logging.basicConfig(level=logging.DEBUG) - main() + sys.exit(main()) diff --git a/accounting/config.py b/accounting/config.py new file mode 100644 index 0000000..f6b115f --- /dev/null +++ b/accounting/config.py @@ -0,0 +1,6 @@ +import os + +LEDGER_FILE = os.environ.get('LEDGER_FILE', None) +DEBUG = bool(int(os.environ.get('DEBUG', 0))) +PORT = int(os.environ.get('PORT', 5000)) +HOST = os.environ.get('HOST', '127.0.0.1') diff --git a/accounting/web.py b/accounting/web.py new file mode 100644 index 0000000..4bc2ed7 --- /dev/null +++ b/accounting/web.py @@ -0,0 +1,59 @@ +import logging + +from flask import Flask, g, jsonify, json + +from accounting import Ledger, Account, Posting, Transaction + + +logging.basicConfig(level=logging.DEBUG) +app = Flask('accounting') +app.config.from_pyfile('config.py') + +ledger = Ledger(ledger_file=app.config['LEDGER_FILE']) + +class AccountingEncoder(json.JSONEncoder): + def default(self, o): + if isinstance(o, Account): + return dict( + name=o.name, + balance=o.balance + ) + elif isinstance(o, Transaction): + return dict( + date=o.date, + payee=o.payee, + postings=o.postings + ) + elif isinstance(o, Posting): + return dict( + account=o.account, + amount=o.amount, + symbol=o.symbol + ) + + return json.JSONEncoder.default(self, o) + +app.json_encoder = AccountingEncoder + +@app.route('/') +def index(): + return 'Hello World!' + +@app.route('/balance') +def balance_report(): + report_data = ledger.bal() + + return jsonify(balance_report=report_data) + +@app.route('/register') +def register_report(): + report_data = ledger.reg() + + return jsonify(register_report=report_data) + + +def main(): + app.run(debug=True) + +if __name__ == '__main__': + main() diff --git a/bin/serve b/bin/serve new file mode 100755 index 0000000..81818ad --- /dev/null +++ b/bin/serve @@ -0,0 +1,9 @@ +#! /usr/bin/env python3 +import sys + +sys.path.append('./') + +from accounting.web import main + +if __name__ == '__main__': + sys.exit(main())