blob: 7570155885ca23a5ace5c7f129c063d19f99773d [file] [log] [blame]
martin f. krafftf37f0682013-06-14 16:36:20 +02001#
2# -*- coding: utf-8 -*-
3#
4# This file is part of reclass (http://github.com/madduck/reclass)
5#
6# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
7# Released under the terms of the Artistic Licence 2.0
8#
9import datatypes
10import yaml
11
12class YamlFile(object):
13
14 def __init__(self, path):
15 ''' Initialise a yamlfile object '''
16 self._path = path
17 self._data = dict()
18 self._read()
martin f. krafftf7858a82013-06-15 15:33:28 +020019 path = property(lambda self: self._path)
martin f. krafftf37f0682013-06-14 16:36:20 +020020
21 def _read(self):
22 fp = file(self._path)
martin f. krafft00e6a812013-06-15 15:40:41 +020023 data = yaml.safe_load(fp)
24 if data is not None:
25 self._data = data
martin f. krafftf37f0682013-06-14 16:36:20 +020026 fp.close()
27
martin f. krafftf37f0682013-06-14 16:36:20 +020028 def _get_entity(self):
29 classes = datatypes.Classes(self._data.get('classes', []))
30 parameters = datatypes.Parameters(self._data.get('parameters', {}))
31 applications = datatypes.Applications(self._data.get('applications', []))
32 return datatypes.Entity(classes, parameters, applications)
33 entity = property(lambda self: self._get_entity())
34
35 def __repr__(self):
martin f. krafftf7858a82013-06-15 15:33:28 +020036 return '<{0} {1}, {2}>'.format(self.__class__.__name__, self._path,
martin f. krafftf37f0682013-06-14 16:36:20 +020037 self._data.keys())