2019-06-25 06:32:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-06-16 10:11:23 +00:00
|
|
|
from jumpserver.const import DYNAMIC
|
|
|
|
from werkzeug.local import Local, LocalProxy
|
2019-06-25 06:32:25 +00:00
|
|
|
|
|
|
|
thread_local = Local()
|
|
|
|
|
|
|
|
|
|
|
|
def _find(attr):
|
|
|
|
return getattr(thread_local, attr, None)
|
2020-06-16 10:11:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class _Settings:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def get_dynamic_cfg_from_thread_local():
|
|
|
|
KEY = 'dynamic_config'
|
|
|
|
|
|
|
|
try:
|
|
|
|
cfg = getattr(thread_local, KEY)
|
|
|
|
except AttributeError:
|
|
|
|
cfg = _Settings()
|
|
|
|
setattr(thread_local, KEY, cfg)
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
|
|
|
|
|
|
|
|
class DynamicDefaultLocalProxy(LocalProxy):
|
|
|
|
def __getattr__(self, item):
|
|
|
|
try:
|
|
|
|
value = super().__getattr__(item)
|
|
|
|
except AttributeError:
|
|
|
|
value = getattr(DYNAMIC, item)()
|
|
|
|
setattr(self, item, value)
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL_DYNAMIC_SETTINGS = DynamicDefaultLocalProxy(get_dynamic_cfg_from_thread_local)
|