From fca29b2e8ff7e2b37a5a260672d666509a054007 Mon Sep 17 00:00:00 2001 From: Attila Farkas <ati@mail.muni.cz> Date: Mon, 18 Jan 2021 17:45:58 +0100 Subject: [PATCH] Add a simple yaml reader --- cyber_sandbox_creator/io/__init__.py | 0 cyber_sandbox_creator/io/reader.py | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 cyber_sandbox_creator/io/__init__.py create mode 100644 cyber_sandbox_creator/io/reader.py diff --git a/cyber_sandbox_creator/io/__init__.py b/cyber_sandbox_creator/io/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cyber_sandbox_creator/io/reader.py b/cyber_sandbox_creator/io/reader.py new file mode 100644 index 0000000..1620479 --- /dev/null +++ b/cyber_sandbox_creator/io/reader.py @@ -0,0 +1,19 @@ +import yaml + +from pathlib import Path +from typing import Union, List, Dict + + +class Reader: + """Static methods for reading from files""" + + @staticmethod + def open_yaml(file_path: Path) -> Union[List, Dict]: + """Open a yaml file and return its content""" + try: + with file_path.open() as input_file: + return yaml.safe_load(input_file) + except yaml.YAMLError: + raise IOError(f"Could not parse yaml file {file_path}.") + except IOError: + raise IOError(f"Could not open yaml file {file_path}.") -- GitLab