Package: rconf
WhatExtensible Python package to resolve references and apply patches inJSONandTOMLconfigurations.It uses Python’sjson moduleandtomllib moduleto decode,
handles local and remote reference URLs usingurllib,
and resolves document fragments specified through pointers,
optionally applying patches.References follow theJSON Reference draft.Reference patches are based onRFC 6902,
extended with shorthand notations, anassignand amergeoperation.JSON pointers followRFC 6901.TOML pointers areTOML keysextended with array indices.It can be used as a drop-in replacement forjson.loadandtomllib.load(rconf.load),json.loadsandtomllib.loads(rconf.loads),with an additionalrconf.loaduto load URLs and local paths
andrconf.loadcto load a Pythondict.
These functions all resolve references and apply patches.Whyrconfallows a developer tonot care about configuration location (URLs) andnot care about configuration format (decoders),and a user toreduce duplication by allowing URLs,reduce duplication by allowing references,reduce duplication by allowing patches andmix and match formats already in use.HowInstallationRConf ispip-installable:$pipinstallrconfLoad a URL (with fragment pointer)This snippet loads a document from a URL,
and resolves the pointer in its fragment:importjsonimportrconfconfig=rconf.loadu("https://github.com/manifest.json#/icons/0")print(json.dumps(config,indent=4)){"sizes":"114x114","src":"https://github.githubassets.com/apple-touch-icon-114x114.png"}or, with an explicit pointer,importjsonimportrconfconfig=rconf.loadu("https://raw.githubusercontent.com/pypa/build/main/pyproject.toml",ptr="project.description",)print(json.dumps(config,indent=4))"A simple, correct Python build frontend"Load and patch a stringThe string (config) contains a JSON document with a reference ($ref)
and a patch assignment for/name.importjsonimportrconfconfig=rconf.loads("""
{
    "github-icon": {
        "$ref": "https://github.com/manifest.json#/icons/0",
        "/name": "GitHub icon"
    }
}
""")print(json.dumps(config,indent=4)){"github-icon":{"sizes":"114x114","src":"https://github.githubassets.com/apple-touch-icon-114x114.png","name":"GitHub icon"}}Mix and patchFormats can be mixed,
like this JSON document referencing a TOML document,
patched with a$patcharray using both the full and shorthand notations.importjsonimportrconfconfig=rconf.loads("""
{
    "$ref": "data:application/toml;base64,W3Byb2plY3RdCnRpdGxlID0gIlByb2plY3QgdGl0bGUiCmRlc2NyaXB0aW9uID0gIlByb2plY3QgZGVzY3JpcHRpb24iCnJlYWRtZSA9ICJyZWFkbWUubWQiCg==",
    "$patch": [
        {"op": "move", "path": "/project/name", "from": "/project/title"},
        ["-", "/project/readme"],
        ["+", "/project/dynamic", ["version"]]
    ]
}
""")print(json.dumps(config,indent=4)){"project":{"description":"Project description","name":"Project title","dynamic":["version"]}}Command Line Interfacerconfis added as a CLI tool,
to translate JSON and TOML files with (or without) references and patches,
and works with URLs or file paths.$rconfdumphttps://github.com/manifest.json#/icons/0{
    "sizes": "114x114",
    "src": "https://github.githubassets.com/apple-touch-icon-114x114.png"
}rconf dumpbehavior can be modified with a configuration file.rconf configcan show or create an example.Those using bash/zsh can activate auto completion,
provided byargcomplete.$pipinstallrconf[sh]$activate-global-python-argcomplete--userDefinitionsDefinitions can be found on thedefinitionspage.UsageA more thorough description can be found on theusagepage,
with details in thereference.