diff options
author | Yujun Zhang <zhang.yujunz@zte.com.cn> | 2017-01-19 16:58:46 +0800 |
---|---|---|
committer | Yujun Zhang <zhang.yujunz@zte.com.cn> | 2017-01-29 20:34:56 +0800 |
commit | f2d021c72b38845954755bab54aa13b4b2aad725 (patch) | |
tree | 384b0aa7772facb1e8f836a4741f8151ef119901 /qtip/loader/yaml_file.py | |
parent | a42b9a6ac576b11403955ff38df6cc58cf02abb7 (diff) |
Refactor loader classes
- rename BaseLoader to YamlFileLoader as base class of QTIP specs loader
- create an abstract BaseLoader
- create FileLoader for logfile collector
Change-Id: I0c992cd847fc0dce4fdd73a13c1cdbc406c84532
Signed-off-by: Yujun Zhang <zhang.yujunz@zte.com.cn>
Diffstat (limited to 'qtip/loader/yaml_file.py')
-rw-r--r-- | qtip/loader/yaml_file.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/qtip/loader/yaml_file.py b/qtip/loader/yaml_file.py new file mode 100644 index 00000000..f1cd4614 --- /dev/null +++ b/qtip/loader/yaml_file.py @@ -0,0 +1,33 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp 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 +############################################################################## + +from collections import defaultdict +from os import path +import yaml + +from qtip.base.error import InvalidFormat +from qtip.base.constant import BaseProp +from qtip.loader.file import FileLoader + + +class YamlFileLoader(FileLoader): + """load content from yaml file""" + + 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 |