-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
89 lines (70 loc) · 2.31 KB
/
settings.py
File metadata and controls
89 lines (70 loc) · 2.31 KB
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
import os
from dotenv import load_dotenv, find_dotenv
import json
from pathlib import Path
import copy
DEFAULT_APP_DIR = Path.home() / ".plaid-cli-python"
DEFAULT_APP_DIR.mkdir(parents=True, exist_ok=True)
DEFAULT_CONFIG_PATH = DEFAULT_APP_DIR / ".env"
DEFAULT_CONFIG = {
"PORT": 8080,
"PLAID_ENV": "sandbox",
"PLAID_CLIENT_ID": None,
"PLAID_SECRET": None,
"PLAID_SANDBOX_REDIRECT_URI": None,
"PLAID_API_VERSION": "2020-09-14",
}
DEFAULT_DATA = {"links": []}
# load .env values into environment variables
# use fallback config path if necessary
env_path = find_dotenv()
if not env_path and DEFAULT_CONFIG_PATH.is_file():
env_path = DEFAULT_CONFIG_PATH
load_dotenv(env_path)
def load_config() -> dict:
config = copy.deepcopy(DEFAULT_CONFIG)
for key in config:
if (val := os.getenv(key)) is not None:
config[key] = val
return config
def __merge(source, destination):
for key, value in source.items():
if isinstance(value, dict):
# get node or create one
node = destination.setdefault(key, {})
__merge(value, node)
else:
destination[key] = value
return destination
def load_json_file(config_path: Path, default_json: dict) -> dict:
config = default_json.copy()
if config_path.is_file():
with open(config_path) as f:
config = __merge(json.load(f), config)
return config
def write_json_file(config_path: Path, content: dict):
config_path.parent.mkdir(parents=True, exist_ok=True)
config_path.write_text(json.dumps(content, indent=True, sort_keys=True))
def save_data(data: dict, path: Path = None):
if not path:
path = DEFAULT_APP_DIR / "data.json"
write_json_file(path, data)
def load_data(path: Path = None) -> dict:
if not path:
path = DEFAULT_APP_DIR / "data.json"
return load_json_file(path, DEFAULT_DATA)
def get_link_data(data: dict, token_or_alias: str) -> str:
links = data["links"]
matching_link = next(
(
l
for l in links
if l["access_token"] == token_or_alias
or l.get("alias", None) == token_or_alias
),
None,
)
if matching_link:
return matching_link
else:
raise ValueError(f"Token or alias does not exist for {token_or_alias}.")