python flask 配置檔案的原始碼分析

2022-05-14 16:10:45 字數 1314 閱讀 7118

settings.py

')

原始碼分析:

class

config(dict):

def from_pyfile(self, filename, silent=false):

#找到檔案

filename =os.path.join(self.root_path, filename)

#建立了乙個模組,裡面沒有東西

d = types.moduletype('

config')

d.__file__ =filename

try:

#開啟檔案,獲取所有內容

#再將配置檔案裡的所有值封裝到上一步建立的模組中

with open(filename, mode='rb'

) as config_file:

exec(compile(config_file.read(), filename, '

exec

'), d.__dict__)#

執行from_object方法

self.from_object(d)

return

true

deffrom_object(self, obj):

#for key in

dir(obj):

ifkey.isupper():

self[key] = getattr(obj, key)

方式三:

os.environ['

flaks-settings

'] = '

settings.py''

flaks-settings

')原始碼分析:

class config(dict):

def from_envvar(self, variable_name, silent=false):

#獲取settings檔案

rv = os.environ.get(variable_name)

#發現還是執行了from_pyfile方法

return self.from_pyfile(rv, silent=silent)

settings.devconfig

module_name, obj_name = import_name.rsplit('.', 1)

try:

module = __import__(module_name, none, none, [obj_name])

#獲取模組中的類並返回

try:

return getattr(module, obj_name)

drbd配置檔案 drbd配置檔案

drbd配置檔案 vim usr local drbd etc drbd.d global common.conf global usage count yes 是否參加drbd使用者統計 common protocol c 使用drbd的第三種同步協議 disk 使用dpod功能保證在數 on i...

Python flask日誌配置

def init log log name 初始化日誌 return logging.basicconfig level logging.info 除錯debug級 開發環境 file log handler rotatingfilehandler logs log format path of c...

Spring配置檔案載入外部配置檔案

有時,應用程式可能需要從不同的位置 例如 檔案系統 classpath或者url 讀取外部資源 例如 文字檔案 xml檔案 屬性檔案或或者影象檔案 通常,為了從不同位置載入資源,需要和不同的api打交道。spring的資源載入器提供了乙個統一的getresource 方法,使用這個方法可以通過資源路...