martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame^] | 1 | # |
| 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 | # |
| 9 | import datatypes |
| 10 | import yaml |
| 11 | |
| 12 | class 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()) |