-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
159 lines (152 loc) · 6.54 KB
/
utils.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import calendar
from gspread import service_account_from_dict
from gspread.spreadsheet import Spreadsheet, Worksheet
import logging
from pathlib import Path
import toml
month_labels = {m: calendar.month_abbr[m] for m in range(1,13)}
def get_transaction_tab_presets(
config_file_path: str = ".streamlit/secrets.toml"
) -> (dict[str, dict[str, str|bool|int]] | None):
"""
Gets transaction presets for the Add Transaction tab.
"""
try:
presets: list[dict[str, str|bool|int]] = toml.loads(
Path(config_file_path).read_text(encoding="utf-8")
)['expense_tracker']['transaction_tab']['presets']
if len(presets) > 0:
def get_key(p_dict):
key = ""
if 'memo' in p_dict:
key = f"{p_dict['memo']}"
elif 'category' in p_dict:
key = f"{p_dict['category']}"
if 'owner' in p_dict:
return f"{key} ({p_dict['owner']})"
return key
return {get_key(p): p for p in presets}
else:
return None
except (FileNotFoundError, toml.decoder.TomlDecodeError, KeyError):
logging.info("No config specified, continuing without presets")
return None
def get_transaction_tab_shared_default(
config_file_path: str = ".streamlit/secrets.toml",
fallback_val: bool = False
) -> bool:
"""
Gets transaction tab default for shared field.
"""
try:
secrets_config: list[dict[str, str]] = toml.loads(
Path(config_file_path).read_text(encoding="utf-8")
)
if 'expense_tracker' in secrets_config and \
'transaction_tab' in secrets_config['expense_tracker'] and \
'defaults' in secrets_config['expense_tracker']['transaction_tab'] and \
'shared' in secrets_config['expense_tracker']['transaction_tab']['defaults']:
return secrets_config['expense_tracker']['transaction_tab']['defaults']['shared']
else:
return fallback_val
except (FileNotFoundError, toml.decoder.TomlDecodeError, KeyError):
logging.info("No config file found")
return fallback_val
def get_worksheet(
config_file_path: str = ".streamlit/secrets.toml"
) -> str | None:
"""
Gets worksheet value from secrets config file.
This is needed to specify a worksheet for service accounts.
"""
try:
secrets_config: list[dict[str, str]] = toml.loads(
Path(config_file_path).read_text(encoding="utf-8")
)
if 'connections' in secrets_config and \
'gsheets' in secrets_config['connections'] and \
'worksheet' in secrets_config['connections']['gsheets'] and \
'type' in secrets_config['connections']['gsheets'] and \
secrets_config['connections']['gsheets']['type'] == 'service_account':
return secrets_config['connections']['gsheets']['worksheet']
else:
return None
except (FileNotFoundError, toml.decoder.TomlDecodeError, KeyError):
logging.info("No config file found, continuing without owner color map")
return None
def get_spreadsheet_client(config_file_path: str = ".streamlit/secrets.toml") -> Spreadsheet | None:
"""
Get spreadsheet client using service account
"""
try:
secrets_config: list[dict[str, str]] = toml.loads(
Path(config_file_path).read_text(encoding="utf-8")
)
if 'connections' in secrets_config and \
'gsheets' in secrets_config['connections'] and \
'type' in secrets_config['connections']['gsheets'] and \
secrets_config['connections']['gsheets']['type'] == 'service_account':
config = secrets_config['connections']['gsheets']
client = service_account_from_dict(config)
spreadsheet = client.open_by_url(config['spreadsheet'])
return spreadsheet
else:
return None
except (FileNotFoundError, toml.decoder.TomlDecodeError, KeyError):
logging.info("No config file found")
return None
def get_worksheet_client(config_file_path: str = ".streamlit/secrets.toml",
check_write_perms: bool = True) -> Worksheet | None:
"""
Get worksheet client using service account.
"""
try:
secrets_config: list[dict[str, str]] = toml.loads(
Path(config_file_path).read_text(encoding="utf-8")
)
if 'connections' in secrets_config and \
'gsheets' in secrets_config['connections']:
config = secrets_config['connections']['gsheets']
spreadsheet = get_spreadsheet_client()
if spreadsheet:
if check_write_perms:
permissions = [ p for p in spreadsheet.list_permissions() \
if p['emailAddress'] == config['client_email'] ]
if len(permissions) > 0:
for p in permissions:
if p['role'] != 'writer' and \
p['deleted'] != False and \
p['pendingOwner'] != False:
return None
worksheet = spreadsheet.worksheet(config['worksheet'])
return worksheet
except (FileNotFoundError, toml.decoder.TomlDecodeError, KeyError):
logging.info("No config file found")
return None
def get_google_sheet_titles_and_url(
config_file_path: str = ".streamlit/secrets.toml",
default_title: str | None = "Google Sheet"
) -> tuple[str, str, str] | None:
"""
Get spreadsheet & worsksheet titles and URL from secrets config file.
"""
try:
secrets_config: list[dict[str, str]] = toml.loads(
Path(config_file_path).read_text(encoding="utf-8")
)
if 'connections' in secrets_config and \
'gsheets' in secrets_config['connections'] and \
'spreadsheet' in secrets_config['connections']['gsheets']:
config = secrets_config['connections']['gsheets']
url = config['spreadsheet']
spreadsheet = get_spreadsheet_client()
if spreadsheet:
s_title = spreadsheet.title
worksheet = spreadsheet.worksheet(config['worksheet'])
w_title = worksheet.title
return s_title, w_title, url
elif default_title:
return default_title, '', url
except (FileNotFoundError, toml.decoder.TomlDecodeError, KeyError):
logging.info("No config file found")
return None