Python配置文件为带有inheritan的JSON

2024-07-03 06:22:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一个库,它可以很容易地从JSON文件返回配置对象,但不只是读取JSON文件并返回它—将继承应用到节。 类似于旧的*.ini或Zend\u Config\u 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
                }
            }
        }
    }
}

作为return,我将有一个节,it节有“_extend”属性,它将是一个this section在节的顶部(合并/更新)扩展。在

是某个库已经这样做了,还是python社区喜欢用不同的方式处理继承?在


Tags: 文件pathjsonfalsetruedbapplicationdisplay
2条回答

请看一下pyconfigini,它是基于Zend_config_Ini建模的配置文件解析器

看看这个

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")

在这里继承

^{pr2}$

参考号:https://pypi.python.org/pypi/json-config-parser/0.1.2

相关问题 更多 >