Skip to content

Commit 186067a

Browse files
committed
parsing options from environment
1 parent b929cb4 commit 186067a

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

proxy_py/_settings.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
from checkers.google_com_checker import GoogleComChecker
22

3+
import string
4+
import os
5+
import ast
6+
37

48
# enable to get more information in logs
59
DEBUG = False
610

11+
712
"""
813
Database settings (do not try to change after creation of the database)
914
"""
@@ -19,6 +24,7 @@
1924
DB_MAX_DOMAIN_LENGTH = 128
2025
DB_AUTH_DATA_MAX_LENGTH = 64
2126

27+
2228
"""
2329
Fetcher settings
2430
"""
@@ -67,6 +73,7 @@
6773
GoogleComChecker,
6874
]
6975

76+
7077
"""
7178
Server settings
7279
"""
@@ -81,13 +88,20 @@
8188

8289
PROXY_PROVIDER_SERVER_API_CONFIG_FETCH_CONFIG = {
8390
'fields': [
84-
'address', 'protocol', 'auth_data', 'domain', 'port', 'last_check_time', 'next_check_time', 'number_of_bad_checks',
85-
'bad_proxy', 'uptime', 'response_time', 'white_ipv4', 'white_ipv6', 'city', 'country_code', 'region'
91+
'address', 'protocol', 'auth_data', 'domain', 'port',
92+
'last_check_time', 'next_check_time',
93+
'number_of_bad_checks', 'bad_proxy', 'uptime',
94+
'response_time', 'white_ipv4', 'white_ipv6',
95+
'city', 'country_code', 'region'
8696
],
8797
'filter_fields': [
88-
'last_check_time', 'protocol', 'number_of_bad_checks', 'bad_proxy', 'uptime', 'response_time'
98+
'last_check_time', 'protocol', 'number_of_bad_checks', 'bad_proxy',
99+
'uptime', 'response_time'
100+
],
101+
'order_by_fields': [
102+
'last_check_time', 'number_of_bad_checks', 'uptime', 'response_time',
103+
'country_code'
89104
],
90-
'order_by_fields': ['last_check_time', 'number_of_bad_checks', 'uptime', 'response_time', 'country_code'],
91105
'default_order_by_fields': ['response_time', ],
92106
}
93107

@@ -102,3 +116,33 @@
102116
}
103117

104118
TEMPLATES_PATH = "server/templates"
119+
120+
121+
"""
122+
Loading from the environment
123+
"""
124+
125+
126+
def load_settings_from_environment():
127+
for key, val in globals().items():
128+
# filter only variables with capital letters or digits or undescore
129+
rest = "".join([
130+
ch for ch in key
131+
if ch not in string.ascii_uppercase and ch not in string.digits and ch != '_'
132+
])
133+
if len(rest) > 0:
134+
continue
135+
136+
env_key = "PROXY_PY_" + key
137+
if env_key in os.environ:
138+
env_value = os.environ[env_key]
139+
try:
140+
globals()[key] = ast.literal_eval(env_value)
141+
except:
142+
raise Exception(
143+
f"An error happened during parsing environment value. " +
144+
f"Key = {env_key}, Value = {env_value}"
145+
)
146+
147+
148+
load_settings_from_environment()

0 commit comments

Comments
 (0)