-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.py
More file actions
192 lines (151 loc) · 5.17 KB
/
config.py
File metadata and controls
192 lines (151 loc) · 5.17 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
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
185
186
187
188
189
190
191
192
"""
Utilities providing configuration to the SDK
"""
import json
from pathlib import Path
from typing import Any, Callable, Dict
from urllib.parse import urlparse
import boto3 as boto
from pydantic import Extra, Field, validator
from pydantic_settings import BaseSettings
CREDENTIALS_FILE = "credentials"
CONFIG_FILE = "config"
DATABRICKS_CONFIG_FILE = "databrickscfg"
def json_config_settings_source(path: str) -> Callable[[BaseSettings], Dict[str, Any]]:
def source(settings: BaseSettings) -> Dict[str, Any]:
config_path = _get_config_dir().joinpath(path)
if config_path.exists():
with open(config_path) as fobj:
return json.load(fobj)
return {}
return source
class APIKey(BaseSettings):
id: str = Field(..., alias="api_key_id", env="SYNC_API_KEY_ID")
secret: str = Field(..., alias="api_key_secret", env="SYNC_API_KEY_SECRET")
class Config:
@classmethod
def customise_sources(cls, init_settings, env_settings, file_secret_settings):
return (
init_settings,
env_settings,
json_config_settings_source(CREDENTIALS_FILE),
)
class Configuration(BaseSettings):
api_url: str = Field("https://api.synccomputing.com", env="SYNC_API_URL")
class Config:
extra = Extra.ignore
@classmethod
def customise_sources(cls, init_settings, env_settings, file_secret_settings):
return (
init_settings,
env_settings,
json_config_settings_source(CONFIG_FILE),
)
class DatabricksConf(BaseSettings):
host: str = Field(..., env="DATABRICKS_HOST")
token: str = Field(..., env="DATABRICKS_TOKEN")
aws_region_name: str = Field(
boto.client("s3").meta.region_name, env="DATABRICKS_AWS_REGION"
)
@validator("host")
def validate_host(cls, host):
if host:
parsed_host = urlparse(host)
return f"https://{parsed_host.netloc}"
class Config:
@classmethod
def customise_sources(cls, init_settings, env_settings, file_secret_settings):
return (
init_settings,
env_settings,
json_config_settings_source(DATABRICKS_CONFIG_FILE),
)
def init(api_key: APIKey, config: Configuration, db_config: DatabricksConf = None):
"""Initializes configuration files. Currently only Linux-based systems are supported.
:param api_key: API key
:type api_key: APIKey
:param config: configuration
:type config: Configuration
:param db_config: Databricks configuration, defaults to None
:type db_config: DatabricksConf, optional
"""
config_dir = _get_config_dir()
config_dir.mkdir(exist_ok=True)
credentials_path = config_dir.joinpath(CREDENTIALS_FILE)
with open(credentials_path, "w") as credentials_out:
credentials_out.write(api_key.json(by_alias=True, indent=2))
global _api_key
_api_key = api_key
config_path = config_dir.joinpath(CONFIG_FILE)
with open(config_path, "w") as config_out:
config_out.write(config.json(exclude_none=True, indent=2))
global _config
_config = config
if db_config:
db_config_path = config_dir.joinpath(DATABRICKS_CONFIG_FILE)
with open(db_config_path, "w") as db_config_out:
db_config_out.write(db_config.json(exclude_none=True, indent=2))
global _db_config
_db_config = db_config
def get_api_key() -> APIKey:
"""Returns API key from configuration
:return: API key
:rtype: APIKey
"""
global _api_key
if _api_key is None:
try:
_api_key = APIKey()
except ValueError:
pass
return _api_key
def set_api_key(api_key: APIKey):
global _api_key
if _api_key is not None:
raise RuntimeError(
"Sync API key/secret has already been set and the library does not support resetting "
"credentials"
)
_api_key = api_key
def get_config() -> Configuration:
"""Gets configuration
:return: configuration
:rtype: Configuration
"""
global _config
if _config is None:
_config = Configuration()
return _config
def get_databricks_config() -> DatabricksConf:
global _db_config
if _db_config is None:
try:
_db_config = DatabricksConf()
except ValueError:
pass
return _db_config
def set_databricks_config(db_config: DatabricksConf):
global _db_config
if _db_config is not None:
raise RuntimeError(
"Databricks config has already been set and the library does not support resetting "
"credentials"
)
_db_config = db_config
CONFIG: Configuration
_config = None
API_KEY: APIKey
_api_key = None
DB_CONFIG: DatabricksConf
_db_config = None
def __getattr__(name):
if name == "CONFIG":
return get_config()
elif name == "API_KEY":
return get_api_key()
elif name == "DB_CONFIG":
return get_databricks_config()
else:
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
def _get_config_dir() -> Path:
return Path("~/.sync").expanduser()