我正在寻找可以轻松从JSON文件返回配置对象的库,但不只是读取JSON文件并返回它 - 将继承应用于部分。 类似于旧的* .ini或Zend_Config_JSON(来自PHP Zend Framework)
示例:
{
"production":{
"phpSettings":{
"display_startup_errors": false,
"display_errors": false
},
"includePaths":{
"library": "APPLICATION_PATH/../library"
},
"bootstrap":{
"path": "APPLICATION_PATH/Bootstrap.php",
"class": "Bootstrap"
},
"appnamespace": "Application",,
"resources":{
"frontController":{
"controllerDirectory": "APPLICATION_PATH/controllers",
"moduleDirectory": "APPLICATION_PATH/modules",
"params":{
"displayExceptions": false
}
},
"modules":[],
"db":{
"adapter": "pdo_sqlite",
"params":{
"dbname": "APPLICATION_PATH/../data/db/application.db"
}
},
"layout":{
"layoutPath": "APPLICATION_PATH/layouts/scripts/"
}
}
},
"staging":{
"_extends": "production"
},
"testing":{
"_extends": "production",
"phpSettings":{
"display_startup_errors": true,
"display_errors": true
},
},
"development":{
"_extends": "production",
"resources":{
"frontController":{
"params":{
"displayExceptions": true
}
}
}
}
}
作为返回,我会有一个部分,它的部分有“_extend”属性,它将是扩展部分的顶部(合并/更新)的这一部分。
是否已经有一些库已经或者python社区更喜欢以不同的方式处理继承?
答案 0 :(得分:0)
请查看pyconfigini,这是在Zend_Config_Ini上建模的配置文件解析器
答案 1 :(得分:-1)
检查一下
cfg = JSONConfigParser()
cfg.read_string("""
[section]
number = 3.141592654
dictionary = {"key": "value"}
list = [1,
2,
3]
nested = {"list": [1,2,3]}
true = true
none = null
[DEFAULT]
# settings in the default section are inherited
# by all other sections.
default-setting = "default"
""")
# read a setting
cfg.get("section", "number")
# read a setting using index notation
cfg["section"]["true"]
# settings inherited from DEFAULT
cfg.get("section", "default-setting")
继承这里
# settings inherited from DEFAULT
cfg.get("section", "default-setting")