Upload settings_utils.py with huggingface_hub
Browse files- settings_utils.py +44 -0
settings_utils.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
|
4 |
+
class Settings:
|
5 |
+
_instance = None
|
6 |
+
_settings = {}
|
7 |
+
|
8 |
+
def __new__(cls):
|
9 |
+
if cls._instance is None:
|
10 |
+
cls._instance = super().__new__(cls)
|
11 |
+
return cls._instance
|
12 |
+
|
13 |
+
def __setattr__(self, key, value):
|
14 |
+
if key.endswith("_key") or key in {"_instance", "_settings"}:
|
15 |
+
raise AttributeError(f"Modifying '{key}' is not allowed.")
|
16 |
+
|
17 |
+
self._settings[key] = value
|
18 |
+
|
19 |
+
def __getattr__(self, key):
|
20 |
+
if key.endswith("_key"):
|
21 |
+
actual_key = key[:-4] # Remove the "_key" suffix
|
22 |
+
return self.environment_variable_key_name(actual_key)
|
23 |
+
|
24 |
+
env_value = os.getenv(self.environment_variable_key_name(key))
|
25 |
+
|
26 |
+
if env_value is not None:
|
27 |
+
return env_value
|
28 |
+
|
29 |
+
if key in self._settings:
|
30 |
+
return self._settings[key]
|
31 |
+
|
32 |
+
raise AttributeError(f"'{key}' not found")
|
33 |
+
|
34 |
+
def environment_variable_key_name(self, key):
|
35 |
+
return "UNITXT_" + key.upper()
|
36 |
+
|
37 |
+
def get_all_environment_variables(self):
|
38 |
+
return [
|
39 |
+
self.environment_variable_key_name(key) for key in self._settings.keys()
|
40 |
+
]
|
41 |
+
|
42 |
+
|
43 |
+
def get_settings():
|
44 |
+
return Settings()
|