django/conf/__init__.py/class_Settings

摘自如题文件内容,如有侵权,请联系 QQ 929054468 删除
class Settings:
def __init__(self, settings_module):
# update this dict from global settings (but only for ALL_CAPS settings)
for setting in dir(global_settings):
if setting.isupper():
setattr(self, setting, getattr(global_settings, setting))

将本地模块 global_settings 中的属性值导入到实例对象中

# store the settings module in case someone later cares
self.SETTINGS_MODULE = settings_module

mod = importlib.import_module(self.SETTINGS_MODULE)

利用 importlib模块导入新增的模块 SETTINGS_MODULE
        tuple_settings = (
"INSTALLED_APPS",
"TEMPLATE_DIRS",
"LOCALE_PATHS",
)
self._explicit_settings = set()
for setting in dir(mod):
if setting.isupper():
setting_value = getattr(mod, setting)

if (setting in tuple_settings and
not isinstance(setting_value, (list, tuple))):
raise ImproperlyConfigured("The %s setting must be a list or a tuple. " % setting)
setattr(self, setting, setting_value)
self._explicit_settings.add(setting)

将新增的模块属性值导入到实例对象中

if not self.SECRET_KEY:
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

检测新导入的属性值中有没有 SECRET_KEY 属性值,且不为空
        if self.is_overridden('DEFAULT_CONTENT_TYPE'):
warnings.warn('The DEFAULT_CONTENT_TYPE setting is deprecated.', RemovedInDjango30Warning)

检查属性名 DEFAULT_CONTENT_TYPE 是否在新导入的模块属性中,否则进行warn提醒
        if hasattr(time, 'tzset') and self.TIME_ZONE:
# When we can, attempt to validate the timezone. If we can't find
# this file, no check happens and it's harmless.
zoneinfo_root = '/usr/share/zoneinfo'
if (os.path.exists(zoneinfo_root) and not
os.path.exists(os.path.join(zoneinfo_root, *(self.TIME_ZONE.split('/'))))):
raise ValueError("Incorrect timezone setting: %s" % self.TIME_ZONE)
# Move the time zone info into os.environ. See ticket #2315 for why
# we don't do this unconditionally (breaks Windows).
os.environ['TZ'] = self.TIME_ZONE
time.tzset()

设置时区,首先会检查 time 模块是否有 tzset 方法,和对象导入的模块是否有TIME_ZONE属性,根据TIME_ZONE在linux服务器的 /usr/share/zoneinfo 查找对应的时区,如果对应的时区文件不存在,则抛出异常,
然后重新设置时区

def is_overridden(self, setting):
return setting in self._explicit_settings

def __repr__(self):
return '<%(cls)s "%(settings_module)s">' % {
'cls': self.__class__.__name__,
'settings_module': self.SETTINGS_MODULE,
}

猜你喜欢

转载自www.cnblogs.com/jiajiahui/p/9728982.html