-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathconfig.py
184 lines (152 loc) · 5.86 KB
/
config.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
Mergin Maps DB Sync - a tool for two-way synchronization between Mergin Maps and a PostGIS database
Copyright (C) 2022 Lutra Consulting
License: MIT
"""
import pathlib
import platform
import smtplib
import subprocess
import tempfile
from dynaconf import Dynaconf
import dynaconf
from smtp_functions import create_connection_and_log_user
config = Dynaconf(
envvar_prefix=False,
settings_files=[],
geodiff_exe="geodiff.exe" if platform.system() == "Windows" else "geodiff",
working_dir=(pathlib.Path(tempfile.gettempdir()) / "dbsync").as_posix(),
)
class ConfigError(Exception):
pass
def validate_config(config):
"""Validate config - make sure values are consistent"""
# validate that geodiff can be found, otherwise it does not make sense to run DB Sync
try:
subprocess.run(
[
config.geodiff_exe,
"help",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except FileNotFoundError:
raise ConfigError(
"Config error: Geodiff executable not found. Is it installed and available in `PATH` environment variable?"
)
if not (config.mergin.url and config.mergin.username and config.mergin.password):
raise ConfigError("Config error: Incorrect mergin settings")
if not (config.connections and len(config.connections)):
raise ConfigError("Config error: Connections list can not be empty")
if "init_from" not in config:
raise ConfigError("Config error: Missing parameter `init_from` in the configuration.")
if config.init_from not in [
"gpkg",
"db",
]:
raise ConfigError(
f"Config error: `init_from` parameter must be either `gpkg` or `db`. Current value is `{config.init_from}`."
)
for conn in config.connections:
for attr in [
"driver",
"conn_info",
"modified",
"base",
"mergin_project",
"sync_file",
]:
if not hasattr(
conn,
attr,
):
raise ConfigError(
f"Config error: Incorrect connection settings. Required parameter `{attr}` is missing."
)
if conn.driver != "postgres":
raise ConfigError("Config error: Only 'postgres' driver is currently supported.")
if "/" not in conn.mergin_project:
raise ConfigError(
"Config error: Name of the Mergin Maps project should be provided in the namespace/name format."
)
if "skip_tables" in conn:
if conn.skip_tables is None:
continue
elif isinstance(
conn.skip_tables,
str,
):
continue
elif not isinstance(
conn.skip_tables,
list,
):
raise ConfigError("Config error: Ignored tables parameter should be a list")
if "notification" in config:
settings = [
"smtp_server",
"email_sender",
"email_subject",
"email_recipients",
]
for setting in settings:
if setting not in config.notification:
raise ConfigError(f"Config error: `{setting}` is missing from `notification`.")
if not isinstance(config.notification.email_recipients, list):
raise ConfigError("Config error: `email_recipients` should be a list of addresses.")
if "use_ssl" in config.notification:
if not isinstance(config.notification.use_ssl, bool):
raise ConfigError("Config error: `use_ssl` must be set to either `true` or `false`.")
if "use_tls" in config.notification:
if not isinstance(config.notification.use_tls, bool):
raise ConfigError("Config error: `use_tls` must be set to either `true` or `false`.")
if "smtp_port" in config.notification:
if not isinstance(config.notification.smtp_port, int):
raise ConfigError("Config error: `smtp_port` must be set to an integer.")
if "minimal_email_interval" in config.notification:
if not isinstance(config.notification.minimal_email_interval, (int, float)):
raise ConfigError("Config error: `minimal_email_interval` must be set to a number.")
try:
smtp_conn = create_connection_and_log_user(config)
smtp_conn.quit()
except (OSError, smtplib.SMTPConnectError, smtplib.SMTPAuthenticationError) as e:
err = str(e)
if isinstance(e, (smtplib.SMTPConnectError, smtplib.SMTPAuthenticationError)):
err = str(e.smtp_error)
raise ConfigError(f"Config SMTP Error: {err}.")
def get_ignored_tables(
connection,
):
if "skip_tables" in connection:
if connection.skip_tables is None:
return []
elif isinstance(
connection.skip_tables,
str,
):
return [connection.skip_tables]
elif isinstance(
connection.skip_tables,
list,
):
if len(connection.skip_tables) < 1:
return []
elif isinstance(connection.skip_tables, dynaconf.vendor.box.box_list.BoxList):
return connection.skip_tables.to_list()
return connection.skip_tables
else:
return []
def update_config_path(
path_param: str,
) -> None:
config_file_path = pathlib.Path(path_param)
if config_file_path.exists():
print(f"Using config file: {path_param}")
user_file_config = Dynaconf(
envvar_prefix=False,
settings_files=[config_file_path],
)
config.update(user_file_config)
else:
raise IOError(f"Config file {config_file_path} does not exist.")