From 5a6e90440e386dd478084a6c0946fdb88eb8c5a8 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 11 May 2023 18:15:46 -0400 Subject: [PATCH] Replace yaml.load with yaml.safe_load (#276) --- cyanobyte/codegen.py | 6 +++--- cyanobyte/validator.py | 22 +++++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cyanobyte/codegen.py b/cyanobyte/codegen.py index e1329b2..5e4eebb 100644 --- a/cyanobyte/codegen.py +++ b/cyanobyte/codegen.py @@ -23,7 +23,7 @@ import json import click -from yaml import load +from yaml import safe_load try: from yaml import CLoader as Loader except ImportError: @@ -129,7 +129,7 @@ def generate_source_file(template, peripheral, opts, template_ext, """ # Open peripheral file with open(peripheral, "r") as peripheral_file: - peripheral_data = load(peripheral_file, Loader=Loader) + peripheral_data = safe_load(peripheral_file) # Add additional metadata to the spec data peripheral_data["version"] = _VERSION peripheral_data["fileName"] = peripheral @@ -139,7 +139,7 @@ def generate_source_file(template, peripheral, opts, template_ext, options_file = open(opts, "r") except: options_file = pkg_resources.open_text('cyanobyte-templates', opts) - options_data = load(options_file, Loader=Loader) + options_data = safe_load(options_file) peripheral_data["options"] = options_data # Load imports diff --git a/cyanobyte/validator.py b/cyanobyte/validator.py index 9224972..7987cda 100644 --- a/cyanobyte/validator.py +++ b/cyanobyte/validator.py @@ -6,9 +6,9 @@ import sys import json -import click import os import os.path as path +import click import yaml from yaml.constructor import ConstructorError try: @@ -31,13 +31,16 @@ def no_duplicates_constructor(loader, node, deep=False): key = loader.construct_object(key_node, deep=deep) value = loader.construct_object(value_node, deep=deep) if key in mapping: - raise ConstructorError("while constructing a mapping", node.start_mark, - "found duplicate key (%s)" % key, key_node.start_mark) + raise ConstructorError("while constructing a mapping", + node.start_mark, + "found duplicate key (%s)" % key, + key_node.start_mark) mapping[key] = value return loader.construct_mapping(node, deep) -yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, no_duplicates_constructor, Loader=Loader) +yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, + no_duplicates_constructor, Loader=Loader) def cyanobyte_validate(input_files): """ @@ -50,7 +53,8 @@ def cyanobyte_validate(input_files): path = "cyanobyte-spec/cyanobyte.schema.json" try: import pkg_resources - path = pkg_resources.resource_filename('cyanobyte-spec', 'cyanobyte.schema.json') + path = pkg_resources.resource_filename('cyanobyte-spec', + 'cyanobyte.schema.json') except:pass with open(path, "r") as schema_json: @@ -61,7 +65,7 @@ def cyanobyte_validate(input_files): for input_file in input_files: with open(input_file, "r") as document_yaml: try: - document_dict = yaml.load(document_yaml, Loader=Loader) + document_dict = yaml.safe_load(document_yaml) validate(instance=document_dict, schema=schema) print('✓ ' + input_file) except (ConstructorError, ValidationError) as err: @@ -70,9 +74,9 @@ def cyanobyte_validate(input_files): # Dump all errors here print('') - for e in errors: - print(e.input_file + ':') - print(e.err) + for err in errors: + print(err.input_file + ':') + print(err.err) def unittest(input_files):