Added simple web serving of ledger data.
This commit is contained in:
parent
7fad5e4cee
commit
f8f51f7489
4 changed files with 84 additions and 4 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
@ -118,7 +119,10 @@ class Ledger:
|
||||||
accounts = []
|
accounts = []
|
||||||
|
|
||||||
for line in output.split(b'\n'):
|
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))
|
accounts.append(Account(name=name, balance=balance))
|
||||||
|
|
||||||
|
@ -195,12 +199,14 @@ class Account:
|
||||||
self=self)
|
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')
|
ledger = Ledger(ledger_file='non-profit-test-data.ledger')
|
||||||
print(ledger.bal())
|
print(ledger.bal())
|
||||||
print(ledger.reg())
|
print(ledger.reg())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
sys.exit(main())
|
||||||
main()
|
|
||||||
|
|
6
accounting/config.py
Normal file
6
accounting/config.py
Normal file
|
@ -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')
|
59
accounting/web.py
Normal file
59
accounting/web.py
Normal file
|
@ -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()
|
9
bin/serve
Executable file
9
bin/serve
Executable file
|
@ -0,0 +1,9 @@
|
||||||
|
#! /usr/bin/env python3
|
||||||
|
import sys
|
||||||
|
|
||||||
|
sys.path.append('./')
|
||||||
|
|
||||||
|
from accounting.web import main
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
Loading…
Reference in a new issue