tiramisu/tiramisu/storage/dictionary/value.py

93 lines
2.9 KiB
Python
Raw Normal View History

2013-08-14 23:06:31 +02:00
# -*- coding: utf-8 -*-
"default plugin for value: set it in a simple dictionary"
2014-07-06 15:31:57 +02:00
# Copyright (C) 2013-2014 Team tiramisu (see AUTHORS for all contributors)
2013-08-14 23:06:31 +02:00
#
2013-09-22 22:33:09 +02:00
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
2013-08-14 23:06:31 +02:00
#
2013-09-22 22:33:09 +02:00
# 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 Lesser General Public License for more
# details.
2013-08-14 23:06:31 +02:00
#
2013-09-22 22:33:09 +02:00
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-08-14 23:06:31 +02:00
# ____________________________________________________________
2015-07-24 17:54:10 +02:00
from copy import copy
2013-09-22 20:57:52 +02:00
from ..util import Cache
2013-08-14 23:06:31 +02:00
2013-08-20 09:47:12 +02:00
class Values(Cache):
__slots__ = ('_values', '_informations', '__weakref__')
2013-08-14 23:06:31 +02:00
2013-08-20 22:45:11 +02:00
def __init__(self, storage):
2013-08-14 23:06:31 +02:00
"""init plugin means create values storage
"""
self._values = {}
self._informations = {}
2013-08-14 23:06:31 +02:00
# should init cache too
2013-08-27 09:46:52 +02:00
super(Values, self).__init__(storage)
2013-08-14 23:06:31 +02:00
# value
def setvalue(self, path, value, owner):
"""set value for a path
2013-08-14 23:06:31 +02:00
a specified value must be associated to an owner
"""
self._values[path] = (owner, value)
2013-08-14 23:06:31 +02:00
def getvalue(self, path):
"""get value for a path
2013-08-14 23:06:31 +02:00
return: only value, not the owner
"""
return self._values[path][1]
2013-08-14 23:06:31 +02:00
def hasvalue(self, path):
"""if path has a value
2013-08-14 23:06:31 +02:00
return: boolean
"""
return path in self._values
2013-08-14 23:06:31 +02:00
def resetvalue(self, path):
2013-08-14 23:06:31 +02:00
"""remove value means delete value in storage
"""
del(self._values[path])
2013-08-14 23:06:31 +02:00
2013-08-20 09:47:12 +02:00
def get_modified_values(self):
2013-08-14 23:06:31 +02:00
"""return all values in a dictionary
example: {'path1': (owner, 'value1'), 'path2': (owner, 'value2')}
2013-08-14 23:06:31 +02:00
"""
2015-07-24 17:54:10 +02:00
return copy(self._values)
2013-08-14 23:06:31 +02:00
# owner
def setowner(self, path, owner):
"""change owner for a path
2013-08-14 23:06:31 +02:00
"""
self._values[path] = (owner, self._values[path][1])
2013-08-14 23:06:31 +02:00
def getowner(self, path, default):
"""get owner for a path
2013-08-14 23:06:31 +02:00
return: owner object
"""
return self._values.get(path, (default, None))[0]
def set_information(self, key, value):
"""updates the information's attribute
(which is a dictionary)
:param key: information's key (ex: "help", "doc"
:param value: information's value (ex: "the help string")
"""
self._informations[key] = value
def get_information(self, key):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
if key in self._informations:
return self._informations[key]
2014-06-19 23:22:39 +02:00
else: # pragma: optional cover
raise ValueError("not found")