1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2009 Sharkbaitbobby <sharkbaitbobby+awn@gmail.com>
4
# This library is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU Lesser General Public
6
# License as published by the Free Software Foundation; either
7
# version 2 of the License, or (at your option) any later version.
9
# This library is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
# Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public
15
# License along with this library; if not, write to the
16
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
# Boston, MA 02111-1307, USA.
19
#A class to sort of act like a daemon for the AwnConfigClient settings
22
from desktopagnostic.config import GROUP_DEFAULT
25
def __init__(self, applet):
27
self.client = awn.config_get_default_for_applet(applet)
28
self._values = {} #{key:value,key:value,etc:etc}
29
self._registered = {} #{key:type,key:type,etc:etc}
30
self._connects = {} #{key:[[func,arg],[func,arg]],key:[[func,arg],[func,arg]]}
32
#A function to add another value to the registered dictionary -- includes the type
33
def register(self,dictionary):
34
for string, type_default in dictionary.items():
35
try:#String has been registered -- no action necessary
36
self._registered[string]
37
except:#String has not been registered -- register it
38
self._registered[string] = type_default[0]
39
if self.get(string) is None:
40
self.set(string, type_default[1])
42
#A function to get the value of a key -- assumes that <string> has already been registered
44
try:#Has been fetched from AwnCC -- return the value
46
return self._values[string]
47
except:#Has not been fetched from AwnCC -- fetch it and return the value
48
self._values[string] = self.client.get_value(GROUP_DEFAULT, string)
50
#Set the functions to call for when <string> is changed as an empty list
51
self._connects[string] = []
54
return self._values[string]
56
#A function to call self.get -- in case someone messes up
57
def get_value(self,string):
60
#A function to set the value of a key -- assumes that <string> has already been registered and the <value> is the
61
#same type as when registered
62
def set(self,string,value):
63
#Set the AwnCC value first
64
self.client.set_value(GROUP_DEFAULT, string, value)
66
#Set the value (internally)
67
self._values[string] = value
69
#Last, go through any functions set to call for when <string> is changed
70
for x in self._connects[string]:
71
x[0](string,value,*x[1],**x[2])
73
#A function to call set.set -- in case someone messes up
74
def set_value(self,string,value):
75
self.set(string,value)
77
#A function to set a function to be called when one of <strings> is changed
78
#<arg> is an optional parameter which will be passed to the function, if called
79
#This assumes that each of <strings> has been registered
80
def connect(self,strings,function,*args1,**args2):
81
if type(strings)==type(''):#<strings> is a single string
82
self._connects[strings].append([function,args1,args2])
83
else:#Assume that <strings> is a list of strings
85
self._connects[x].append([function,args1,args2])
88
def disconnect(self, strings, function):
89
if type(strings) == str:
90
for func in self._connects[strings]:
91
if func[0] == function:
92
self._connects[strings].remove(func)
95
for string in strings:
96
for func in self._connects[strings]:
97
if func[0] == function:
98
self._connects[strings].remove(func)
100
#In case the user wants to get a value via <settingsinstance>[<key>]
101
def __getitem__(self,key):
104
#In case the user wants to set a value via <settingsinstance>[<key>] = ...
105
def __setitem__(self,key,value):
106
return self.set(key,value)