Skip to content
Snippets Groups Projects
Commit 963f861f authored by Erik Reid's avatar Erik Reid
Browse files

added ..tojson directive

parent 7e8c1b36
No related branches found
No related tags found
No related merge requests found
...@@ -14,14 +14,43 @@ ...@@ -14,14 +14,43 @@
# import sys # import sys
# sys.path.insert(0, os.path.abspath('.')) # sys.path.insert(0, os.path.abspath('.'))
from importlib import import_module
from docutils.parsers.rst import Directive
from docutils import nodes
from sphinx import addnodes
import json
import os import os
import sys import sys
sys.path.insert(0, os.path.abspath( sys.path.insert(0, os.path.abspath(
os.path.join( os.path.join(
os.path.dirname(__file__), os.path.dirname(__file__),
'..', '..', 'inventory_provider'))) '..', '..', 'inventory_provider')))
class RenderAsJSON(Directive):
# cf. https://stackoverflow.com/a/59883833
required_arguments = 1
def run(self):
module_path, member_name = self.arguments[0].rsplit('.', 1)
member_data = getattr(import_module(module_path), member_name)
code = json.dumps(member_data, indent=2)
literal = nodes.literal_block(code, code)
literal['language'] = 'json'
return [
addnodes.desc_name(text=member_name),
addnodes.desc_content('', literal)
]
def setup(app):
app.add_directive('asjson', RenderAsJSON)
# -- Project information ----------------------------------------------------- # -- Project information -----------------------------------------------------
......
import json import json
import jsonschema import jsonschema
CONFIG_SCHEMA = { CONFIG_SCHEMA = {
...@@ -142,10 +141,14 @@ CONFIG_SCHEMA = { ...@@ -142,10 +141,14 @@ CONFIG_SCHEMA = {
def load(f): def load(f):
""" """
loads, validates and returns configuration parameters Loads, validates and returns configuration parameters.
Input is validated against this jsonschema:
.. asjson:: inventory_provider.config.CONFIG_SCHEMA
:param f: file-like object that produces the config file :param f: file-like object that produces the config file
:return: :return: a dict containing the parsed configuration parameters
""" """
config = json.loads(f.read()) config = json.loads(f.read())
jsonschema.validate(config, CONFIG_SCHEMA) jsonschema.validate(config, CONFIG_SCHEMA)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment