gaupol/attrdict.py¶
Source code for module gaupol.attrdict from file gaupol/attrdict.py.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | # -*- coding: utf-8 -*-
# Copyright (C) 2006 Osmo Salomaa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Observable dictionary with attribute access to keys."""
import aeidon
__all__ = ("AttributeDictionary",)
class AttributeDictionary(aeidon.Observable):
"""
Observable dictionary with attribute access to keys.
:class:`AttributeDictionary` is initialized from a root dictionary,
which is kept in sync with attribute values. This allows convenient
attribute access to dictionary keys and notifications of changes
via the :class:`aeidon.Observable` interface.
"""
def __init__(self, root):
"""
Initialize an :class:`AttributeDictionary` instance.
All subdictionaries of `root` are initialized as instances of
:class:`AttributeDictionary` as well.
"""
aeidon.Observable.__init__(self)
self._root = root
self.update(root)
def add_attribute(self, name, value):
"""Add instance attribute and corresponding root dictionary key."""
self._root[name] = value
# In the case of dictionaries, set the original dictionary
# to the root dictionary, but instantiate an AttributeDictionary
# for use as the corresponding attribute.
if isinstance(value, dict):
value = AttributeDictionary(value)
setattr(self, name, value)
self.connect("notify::{}".format(name), self._on_notify, name)
def extend(self, root):
"""Add new values from another root dictionary."""
for name, value in root.items():
if hasattr(self, name):
if isinstance(value, dict):
getattr(self, name).extend(value)
else:
self.add_attribute(name, value)
def _on_notify(self, obj, value, name):
"""Synchronize changed attribute value with root dictionary."""
self._root[name] = value
def remove_attribute(self, name):
"""Remove instance attribute and corresponding root dictionary key."""
self.disconnect("notify::{}".format(name), self._on_notify)
delattr(self, name)
if name in self._root:
del self._root[name]
def update(self, root):
"""Update values from another root dictionary."""
for name, value in root.items():
if hasattr(self, name):
if isinstance(value, dict):
getattr(self, name).update(value)
else:
setattr(self, name, value)
else:
self.add_attribute(name, value)
|