-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbank_list_update.py
72 lines (54 loc) · 1.93 KB
/
bank_list_update.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
"""
Helper script to update README.md to reflect banks listed in bank2ynab.conf
"""
import logging
import re
# configure our logger
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
def get_banks(file):
"""
:param file: filename for config file
:return banks: list of bank names
"""
logging.info("Reading bank configuration file ({})...".format(file))
with open(file, "r") as f:
file_contents = f.read()
bracket_regex = re.compile("\n\[(.*)\]\n")
banks = bracket_regex.findall(file_contents)
return banks[1:]
def edit_readme(file, start, end, banks):
"""
:param file: filename for readme file
:param start: string indicating start of section to replace
:param end: string indicating end of section to replace
:param banks: list of banks
"""
logging.info("Formatting bank list...")
bank_list_text = start + "\n"
for bank in banks:
bank_list_text += "1. {}\n".format(bank)
bank_list_text += end
with open(file, "r") as f:
file_contents = f.read()
logging.info("Writing new list to {}".format(file))
with open(file, "w") as f:
list_regex = re.compile("({})(.*)({})".format(start, end), re.DOTALL)
file_contents = list_regex.sub(bank_list_text, file_contents)
f.write(file_contents)
def run(read_file, write_file, start, end):
"""
:param read_file: filename for config file
:param write_file: filename for readme file
:param start: string indicating start of section to replace
:param end: string indicating end of section to replace
"""
banks = get_banks(read_file)
if banks != []:
edit_readme(write_file, start, end, banks)
logging.info("Done.")
# Variables
config_file = "bank2ynab.conf"
readme_file = "README.md"
start_token = "<!--AUTO BANK UPDATE START-->"
end_token = "<!--AUTO BANK UPDATE END-->"
run(config_file, readme_file, start_token, end_token)