forked from Programvareverkstedet/dibbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_logfile.py
executable file
·37 lines (33 loc) · 1.03 KB
/
write_logfile.py
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
#!/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:
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]
print('Writing log to ' + filename)
with open(filename, 'w') as f:
write_log(f)