From 6add7fcac82f9b807e71fe84d16c673682d82327 Mon Sep 17 00:00:00 2001 From: Yujun Zhang Date: Tue, 14 Feb 2017 15:20:16 +0800 Subject: Add test for yaml_file loader Change-Id: I997ceeed17bd35889f6b0f3465bbe75df256b71e Signed-off-by: Yujun Zhang --- qtip/base/error.py | 11 ++++++----- qtip/loader/plan.py | 1 - qtip/loader/yaml_file.py | 19 +++++++------------ 3 files changed, 13 insertions(+), 18 deletions(-) (limited to 'qtip') diff --git a/qtip/base/error.py b/qtip/base/error.py index 01a7f7a6..a055aa8d 100644 --- a/qtip/base/error.py +++ b/qtip/base/error.py @@ -8,22 +8,23 @@ ############################################################################## -class QtipError(Exception): +class BaseError(Exception): pass -class InvalidFormat(QtipError): - def __init__(self, filename): +class InvalidContent(BaseError): + def __init__(self, filename, excinfo=None): self.filename = filename + self.excinfo = excinfo -class NotFound(QtipError): +class NotFound(BaseError): def __init__(self, module, package='qtip'): self.package = package self.module = module -class ToBeDoneError(QtipError): +class ToBeDoneError(BaseError): """something still to be done""" def __init__(self, method, module): self.method = method diff --git a/qtip/loader/plan.py b/qtip/loader/plan.py index 6f1764e2..9b5546e0 100644 --- a/qtip/loader/plan.py +++ b/qtip/loader/plan.py @@ -26,7 +26,6 @@ class Plan(YamlFileLoader): self.qpis = [QPISpec(qpi, paths=paths) for qpi in self.content[PlanProp.QPIS]] - self.info = self.content[PlanProp.INFO] _config = self.content[PlanProp.CONFIG] # TODO(yujunz) create collector by name diff --git a/qtip/loader/yaml_file.py b/qtip/loader/yaml_file.py index f1cd4614..ccaee8db 100644 --- a/qtip/loader/yaml_file.py +++ b/qtip/loader/yaml_file.py @@ -7,11 +7,10 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from collections import defaultdict from os import path import yaml -from qtip.base.error import InvalidFormat +from qtip.base.error import InvalidContent from qtip.base.constant import BaseProp from qtip.loader.file import FileLoader @@ -21,13 +20,9 @@ class YamlFileLoader(FileLoader): def __init__(self, name, paths=None): super(YamlFileLoader, self).__init__(name, paths) - content = defaultdict(lambda: None) - - try: - content.update(yaml.safe_load(file(self._abspath))) - except yaml.YAMLError: - # TODO(yujunz) log yaml error - raise InvalidFormat(self._abspath) - - self.name = content[BaseProp.NAME] or path.splitext(name)[0] - self.content = content + with open(self._abspath, 'r') as stream: + content = yaml.safe_load(stream) + if not isinstance(content, dict): + raise InvalidContent(self._abspath) + self.content = content + self.name = content.get(BaseProp.NAME, path.splitext(name)[0]) -- cgit 1.2.3-korg