Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_should_raise_exception(self):
with pytest.raises(FileFormatNotSupportedError) as excinfo:
load_config_file("tests/data/api_wrong_format_include.yaml")
assert (
str(excinfo.value)
== "The format .txt is not supported. Supported formats: '.yaml', '.yml', '.json'. "
"File path: 'tests/data/include.txt'."
def file_format_not_supported(*args, **kwargs):
raise FileFormatNotSupportedError(".txt", "foo/api.txt")
def scan():
spec_path = settings["spec_path"]
try:
api_spec = load_config_file(spec_path)
except FileNotFoundError as e:
error_message = f"Could not find API spec file: {spec_path}. {str(e)}"
logger.error(error_message)
raise SystemExit(ExitCode.USAGE_ERROR)
except EmptyConfigFileError as e:
error_message = f"API spec file is empty. {str(e)}"
logger.error(error_message)
raise SystemExit(ExitCode.USAGE_ERROR)
except (yaml.YAMLError, FileFormatNotSupportedError) as e:
logger.error(e)
raise SystemExit(ExitCode.USAGE_ERROR)
try:
if API_KEY not in api_spec:
raise MissingMandatoryKeyError({API_KEY}, ROOT_SCOPE)
root_node = EndpointNode(api_spec[API_KEY])
results = root_node.run()
except (
InvalidKeyError,
MissingMandatoryKeyError,
KeyError,
InvalidPythonCodeError,
) as e:
def construct_include(loader: Loader, node: yaml.Node) -> Any:
"""Include file referenced at node."""
relative_path = os.path.join(loader._root, loader.construct_scalar(node))
full_path = os.path.abspath(relative_path)
extension = os.path.splitext(full_path)[1].lstrip(".")
with open(full_path, "r") as f:
if extension in ("yaml", "yml"):
return yaml.load(f, Loader)
elif extension in ("json",):
return json.load(f)
else:
raise FileFormatNotSupportedError(f".{extension}", relative_path)
def __init__(self, file_format, file_path, *args):
message = f"The format {file_format} is not supported. Supported formats: '.yaml', '.yml', '.json'. File path: '{file_path}'."
super(FileFormatNotSupportedError, self).__init__(message, *args)
def load_config_file(file_path):
extension = os.path.splitext(file_path)[-1]
if extension not in (".yaml", ".yml", ".json"):
raise FileFormatNotSupportedError(extension, file_path)
with open(file_path, "r") as stream:
logger.info(f"Loading file {file_path}")
data = yaml.load(stream, Loader)
if not data:
raise EmptyConfigFileError(file_path)
return data