-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmembers.py
100 lines (73 loc) · 2.49 KB
/
members.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
from __future__ import print_function
import sys
import gnucashxml
import getopt
import csv
import re
DATE = 'Date'
MEMBERS = 'Members'
DONATING_MEMBERS = 'Donating members'
NAME = 'Name'
EMAIL = 'Email address'
ACCOUNT_BALANCE = 'Account balance'
MEMBERSHIP_TYPE = 'Membership type'
def main(argv):
try:
opts, args = getopt.getopt(argv, "a:b:c:")
except getopt.GetoptError:
print("argument error")
sys.exit(2)
filename = args[0]
book = gnucashxml.from_filename(filename)
active_members = []
active_member_accounts = book.find_account("Active Members")
for account in active_member_accounts.find_account("Full Members").children:
member = Member(account)
active_members.append(member)
for account in active_member_accounts.find_account("Student Members").children:
member = StudentMember(account)
active_members.append(member)
with open('members.csv', 'wb') as csvfile:
fieldnames = [
NAME,
EMAIL,
MEMBERSHIP_TYPE,
ACCOUNT_BALANCE,
]
writer = csv.DictWriter(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL, fieldnames=fieldnames)
writer.writeheader()
for member in active_members:
writer.writerow({
NAME: member.name(),
EMAIL: member.email(),
MEMBERSHIP_TYPE: member.type(),
ACCOUNT_BALANCE: member.effective_balance()
})
print(member.name(), "has a balance of", "$" + str(member.effective_balance()), " ", member.email())
if member.email() == None:
print("ERROR:", member.name(), "does not have an email address on record")
class Member(object):
monthy_dues = 40
membership_type = "Regular"
def __init__(self, account):
self.account = account
def type(self):
return self.membership_type
def name(self):
return self.account.name
def balance(self):
return sum((-1 * split.value) for split in self.account.splits)
def effective_balance(self):
return self.balance() - self.monthy_dues
def email(self):
email = re.search("[^@ ]+@[^@ ]+\.[^@ ]+", self.account.description)
if email is None:
return None
else:
return email.group()
class StudentMember(Member):
monthy_dues = 20
membership_type = "Student"
if __name__ == "__main__":
main(sys.argv[1:])