summaryrefslogtreecommitdiffstats
path: root/qtip/loader/yaml_file.py
blob: ccaee8dbf484bc176c120a15c8d4691efb0b010b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
##############################################################################
# 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 os import path
import yaml

from qtip.base.error import InvalidContent
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)
        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])