blob: 0d1175adbcf2ae525607ae6b799f65e483740908 [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()
19
20 def _read(self):
21 fp = file(self._path)
22 self._data = yaml.safe_load(fp)
23 self._name = self._data.get('name', self._path)
24 fp.close()
25
26 name = property(lambda self: self._name)
27
28 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):
36 return '<{0} {1}, {2}>'.format(self.__class__.__name__, self._name,
37 self._data.keys())