-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathconfiguration.py
More file actions
201 lines (164 loc) · 5.93 KB
/
Copy pathconfiguration.py
File metadata and controls
201 lines (164 loc) · 5.93 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
193
194
195
196
197
198
199
200
201
from __future__ import annotations
import logging
import os
import re
import time
import warnings
from typing import Optional
from conductor.shared.configuration.settings.authentication_settings import (
AuthenticationSettings,
)
class Configuration:
AUTH_TOKEN = None
def __init__(
self,
base_url: Optional[str] = None,
debug: bool = False,
authentication_settings: AuthenticationSettings = None,
server_api_url: Optional[str] = None,
auth_token_ttl_min: int = 45,
polling_interval: Optional[float] = None,
domain: Optional[str] = None,
polling_interval_seconds: Optional[float] = None,
):
if server_api_url is not None:
self.host = server_api_url
elif base_url is not None:
if re.search(r'/api(/|$)', base_url):
warnings.warn(
"'base_url' been passed with '/api' path. Consider using 'server_api_url' instead"
)
else:
base_url += "/api"
self.host = base_url
else:
self.host = os.getenv("CONDUCTOR_SERVER_URL")
if self.host is None or self.host == "":
self.host = "http://localhost:8080/api"
self.temp_folder_path = None
self.__ui_host = os.getenv("CONDUCTOR_UI_SERVER_URL")
if self.__ui_host is None:
self.__ui_host = self.host.replace("8080/api", "5001")
if authentication_settings is not None:
self.authentication_settings = authentication_settings
else:
key = os.getenv("CONDUCTOR_AUTH_KEY")
secret = os.getenv("CONDUCTOR_AUTH_SECRET")
if key is not None and secret is not None:
self.authentication_settings = AuthenticationSettings(key_id=key, key_secret=secret)
else:
self.authentication_settings = None
# Debug switch
self.debug = debug
# Log format
self.logger_format = "%(asctime)s %(name)-12s %(levelname)-8s %(message)s"
self.is_logger_config_applied = False
# SSL/TLS verification
# Set this to false to skip verifying SSL certificate when calling API
# from https server.
self.verify_ssl = True
# Set this to customize the certificate file to verify the peer.
self.ssl_ca_cert = None
# client certificate file
self.cert_file = None
# client key file
self.key_file = None
# Set this to True/False to enable/disable SSL hostname verification.
self.assert_hostname = None
# Proxy URL
self.proxy = None
# Safe chars for path_param
self.safe_chars_for_path_param = ""
# Provide an alterative to requests.Session() for HTTP connection.
self.http_connection = None
# not updated yet
self.token_update_time = 0
self.auth_token_ttl_msec = auth_token_ttl_min * 60 * 1000
# Worker properties
self.polling_interval = polling_interval or self._get_env_float(
"CONDUCTOR_WORKER_POLL_INTERVAL", 100
)
self.domain = domain or os.getenv("CONDUCTOR_WORKER_DOMAIN", "default_domain")
self.polling_interval_seconds = polling_interval_seconds or self._get_env_float(
"CONDUCTOR_WORKER_POLL_INTERVAL_SECONDS", 0
)
@property
def debug(self):
"""Debug status
:param value: The debug status, True or False.
:type: bool
"""
return self.__debug
@debug.setter
def debug(self, value):
"""Debug status
:param value: The debug status, True or False.
:type: bool
"""
self.__debug = value
if self.__debug:
self.__log_level = logging.DEBUG
else:
self.__log_level = logging.INFO
@property
def logger_format(self):
"""The logger format.
The logger_formatter will be updated when sets logger_format.
:param value: The format string.
:type: str
"""
return self.__logger_format
@logger_format.setter
def logger_format(self, value):
"""The logger format.
The logger_formatter will be updated when sets logger_format.
:param value: The format string.
:type: str
"""
self.__logger_format = value
@property
def log_level(self):
"""The log level.
The log_level will be updated when sets logger_format.
:param value: The format string.
:type: str
"""
return self.__log_level
@property
def ui_host(self):
"""
The log_level will be updated when sets logger_format.
:param value: The format string.
:type: str
"""
return self.__ui_host
def apply_logging_config(self, log_format: Optional[str] = None, level=None):
if self.is_logger_config_applied:
return
if log_format is None:
log_format = self.logger_format
if level is None:
level = self.__log_level
logging.basicConfig(format=log_format, level=level)
self.is_logger_config_applied = True
@staticmethod
def get_logging_formatted_name(name):
return f"[pid:{os.getpid()}] {name}"
def update_token(self, token: str) -> None:
self.AUTH_TOKEN = token
self.token_update_time = round(time.time() * 1000)
def _get_env_float(self, env_var: str, default: float) -> float:
"""Get float value from environment variable with default fallback."""
try:
value = os.getenv(env_var)
if value is not None:
return float(value)
except (ValueError, TypeError):
pass
return default
def get_poll_interval_seconds(self):
return self.polling_interval_seconds
def get_poll_interval(self):
return self.polling_interval
def get_domain(self):
return self.domain