Commit 5c6674f6 authored by Jakub Kicinski's avatar Jakub Kicinski

tools: ynl: load jsonschema on demand

The CLI script tries to validate jsonschema by default.
It's seems better to validate too many times than too few.
However, when copying the scripts to random servers having
to install jsonschema is tedious. Load jsonschema via
importlib, and let the user opt out.
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 8dfec0a8
...@@ -13,6 +13,7 @@ def main(): ...@@ -13,6 +13,7 @@ def main():
parser = argparse.ArgumentParser(description='YNL CLI sample') parser = argparse.ArgumentParser(description='YNL CLI sample')
parser.add_argument('--spec', dest='spec', type=str, required=True) parser.add_argument('--spec', dest='spec', type=str, required=True)
parser.add_argument('--schema', dest='schema', type=str) parser.add_argument('--schema', dest='schema', type=str)
parser.add_argument('--no-schema', action='store_true')
parser.add_argument('--json', dest='json_text', type=str) parser.add_argument('--json', dest='json_text', type=str)
parser.add_argument('--do', dest='do', type=str) parser.add_argument('--do', dest='do', type=str)
parser.add_argument('--dump', dest='dump', type=str) parser.add_argument('--dump', dest='dump', type=str)
...@@ -20,6 +21,9 @@ def main(): ...@@ -20,6 +21,9 @@ def main():
parser.add_argument('--subscribe', dest='ntf', type=str) parser.add_argument('--subscribe', dest='ntf', type=str)
args = parser.parse_args() args = parser.parse_args()
if args.no_schema:
args.schema = ''
attrs = {} attrs = {}
if args.json_text: if args.json_text:
attrs = json.loads(args.json_text) attrs = json.loads(args.json_text)
......
# SPDX-License-Identifier: BSD-3-Clause # SPDX-License-Identifier: BSD-3-Clause
import collections import collections
import jsonschema import importlib
import os import os
import traceback import traceback
import yaml import yaml
# To be loaded dynamically as needed
jsonschema = None
class SpecElement: class SpecElement:
"""Netlink spec element. """Netlink spec element.
...@@ -197,9 +201,14 @@ class SpecFamily(SpecElement): ...@@ -197,9 +201,14 @@ class SpecFamily(SpecElement):
if schema_path is None: if schema_path is None:
schema_path = os.path.dirname(os.path.dirname(spec_path)) + f'/{self.proto}.yaml' schema_path = os.path.dirname(os.path.dirname(spec_path)) + f'/{self.proto}.yaml'
if schema_path: if schema_path:
global jsonschema
with open(schema_path, "r") as stream: with open(schema_path, "r") as stream:
schema = yaml.safe_load(stream) schema = yaml.safe_load(stream)
if jsonschema is None:
jsonschema = importlib.import_module("jsonschema")
jsonschema.validate(self.yaml, schema) jsonschema.validate(self.yaml, schema)
self.attr_sets = collections.OrderedDict() self.attr_sets = collections.OrderedDict()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment