-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.py
83 lines (61 loc) · 2.36 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
"""
Mergin Media Sync - a tool to sync media files from Mergin projects to other storage backends
Copyright (C) 2021 Lutra Consulting
License: MIT
"""
import pathlib
from dynaconf import Dynaconf
config = Dynaconf(
envvar_prefix=False,
settings_files=["config.yaml"],
)
class ConfigError(Exception):
pass
def validate_config(config):
"""Validate config - make sure values are consistent"""
if not (
config.mergin.username and config.mergin.password and config.mergin.project_name
):
raise ConfigError("Config error: Incorrect mergin settings")
if config.driver not in ["local", "minio"]:
raise ConfigError("Config error: Unsupported driver")
if config.operation_mode not in ["move", "copy"]:
raise ConfigError("Config error: Unsupported operation mode")
if config.driver == "local" and not config.local.dest:
raise ConfigError("Config error: Incorrect Local driver settings")
if config.driver == "minio" and not (
config.minio.endpoint
and config.minio.access_key
and config.minio.secret_key
and config.minio.bucket
):
raise ConfigError("Config error: Incorrect MinIO driver settings")
if not (config.allowed_extensions and len(config.allowed_extensions)):
raise ConfigError("Config error: Allowed extensions can not be empty")
if "references" not in config:
config.update({"references": []})
if config.references is None:
config.update({"references": []})
if not isinstance(config.references, list):
raise ConfigError(
"Config error: Incorrect reference settings. Needs to be list of references."
)
for ref in config.references:
if not all(
hasattr(ref, attr)
for attr in ["file", "table", "local_path_column", "driver_path_column"]
):
raise ConfigError("Config error: Incorrect media reference settings")
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.")