diff options
author | 2017-07-27 15:04:07 +0000 | |
---|---|---|
committer | 2017-07-27 15:04:07 +0000 | |
commit | 162c03ef301396072c1934e7e7e0c40a841b4fe2 (patch) | |
tree | 7a2a2781949252436450ff5832962785061a774a /app/utils/metadata_parser.py | |
parent | b88c78e3cf2bef22aa2f1c4d0bf305e303bc15f0 (diff) | |
parent | 7e83d0876ddb84a45e130eeba28bc40ef53c074b (diff) |
Merge "Calipso initial release for OPNFV"
Diffstat (limited to 'app/utils/metadata_parser.py')
-rw-r--r-- | app/utils/metadata_parser.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/app/utils/metadata_parser.py b/app/utils/metadata_parser.py new file mode 100644 index 0000000..1ed49ab --- /dev/null +++ b/app/utils/metadata_parser.py @@ -0,0 +1,83 @@ +############################################################################### +# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) # +# and others # +# # +# All rights reserved. This program and the accompanying materials # +# are made available under the terms of the Apache License, Version 2.0 # +# which accompanies this distribution, and is available at # +# http://www.apache.org/licenses/LICENSE-2.0 # +############################################################################### +import json +import os +from abc import abstractmethod, ABCMeta + +from utils.util import get_extension + + +class MetadataParser(metaclass=ABCMeta): + + def __init__(self): + super().__init__() + self.errors = [] + + @abstractmethod + def get_required_fields(self) -> list: + pass + + def validate_metadata(self, metadata: dict) -> bool: + if not isinstance(metadata, dict): + raise ValueError('metadata needs to be a valid dict') + + # make sure metadata json contains all fields we need + required_fields = self.get_required_fields() + if not all([field in metadata for field in required_fields]): + raise ValueError("Metadata json should contain " + "all the following fields: {}" + .format(', '.join(required_fields))) + return True + + @staticmethod + def _load_json_file(file_path: str): + with open(file_path) as data_file: + return json.load(data_file) + + def _parse_json_file(self, file_path: str): + metadata = self._load_json_file(file_path) + + # validate metadata correctness + if not self.validate_metadata(metadata): + return None + + return metadata + + @staticmethod + def check_metadata_file_ok(file_path: str): + extension = get_extension(file_path) + if extension != 'json': + raise ValueError("Extension '{}' is not supported. " + "Please provide a .json metadata file." + .format(extension)) + + if not os.path.isfile(file_path): + raise ValueError("Couldn't load metadata file. " + "Path '{}' doesn't exist or is not a file" + .format(file_path)) + + def parse_metadata_file(self, file_path: str) -> dict: + # reset errors in case same parser is used to read multiple inputs + self.errors = [] + self.check_metadata_file_ok(file_path) + + # Try to parse metadata file if it has one of the supported extensions + metadata = self._parse_json_file(file_path) + self.check_errors() + return metadata + + def check_errors(self): + if self.errors: + raise ValueError("Errors encountered during " + "metadata file parsing:\n{}" + .format("\n".join(self.errors))) + + def add_error(self, msg): + self.errors.append(msg) |