95 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"
# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors)
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ____________________________________________________________
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
"""
return self._values
# 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]
else:
raise ValueError("not found")