dibbler/write_logfile.py

38 lines
1.0 KiB
Python
Raw Normal View History

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import db
from helpers import *
# Writes a log of all transactions to a text file.
#
# Usage:
# ./write_logfile.py filename
# or (writing to stdout):
# ./write_logfile.py
def write_log(f):
session = Session()
line_format = '%s|%s|%s|%s|%s|%s\n'
transaction_list = session.query(Transaction).all()
for transaction in transaction_list:
if transaction.purchase:
2018-08-19 16:38:45 +02:00
products = ', '.join([ent.product.name for ent in transaction.purchase.entries])
description = ''
else:
products = ''
description = transaction.description
line = line_format % ('purchase', transaction.time, products,
transaction.user.name, transaction.amount, transaction.description)
f.write(line.encode('utf8'))
session.close()
if len(sys.argv) < 2:
write_log(sys.stdout)
else:
filename = sys.argv[1]
2018-08-19 16:38:45 +02:00
print('Writing log to ' + filename)
with open(filename, 'w') as f:
write_log(f)