1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2008 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
24
def __init__(self,applet_name,applet_uid):
26
self._default_client = awn.Config(applet_name,None)
27
self._instance_client = awn.Config(applet_name,applet_uid)
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
#print 'getting '+string
49
if self._instance_client.exists('DEFAULT', string):
50
#print string+' exists in instance'
51
if self._registered[string] in ['string','str',str]:
52
self._values[string] = self._instance_client.get_string('DEFAULT', string)
53
elif self._registered[string] in ['integer','int',int]:
54
self._values[string] = self._instance_client.get_int('DEFAULT', string)
55
elif self._registered[string] in ['float',float]:
56
self._values[string] = self._instance_client.get_float('DEFAULT', string)
57
elif self._registered[string] in ['boolean','bool',bool]:
58
self._values[string] = self._instance_client.get_bool('DEFAULT', string)
59
elif self._registered[string] in ['list-string','list-str',['string'],['str'],[str]]:
60
self._values[string] = self._instance_client.get_list('DEFAULT', string,awn.CONFIG_LIST_STRING)
61
elif self._registered[string] in ['list-integer','list-int',['integer'],['int'],[int]]:
62
self._values[string] = intify(self._instance_client.get_list('DEFAULT', string,awn.CONFIG_LIST_STRING))
63
elif self._registered[string] in ['list-boolean','list-bool',['boolean'],['bool'],[bool]]:
64
self._values[string] = self._instance_client.get_list('DEFAULT', string,awn.CONFIG_LIST_BOOL)
66
else:#Error when registering the key (setting the type)
67
#print "KEY TYPE ERROR for %s" % string
68
return "KEY TYPE ERROR for %s" % string
69
#print 'Got '+string+' from instance'
71
#print string+' not in instance'
72
if self._registered[string] in ['string','str',str]:
73
self._values[string] = self._default_client.get_string('DEFAULT', string)
74
elif self._registered[string] in ['integer','int',int]:
75
self._values[string] = self._default_client.get_int('DEFAULT', string)
76
elif self._registered[string] in ['float',float]:
77
self._values[string] = self._default_client.get_float('DEFAULT', string)
78
elif self._registered[string] in ['boolean','bool',bool]:
79
self._values[string] = self._default_client.get_bool('DEFAULT', string)
80
elif self._registered[string] in ['list-string','list-str',['string'],['str'],[str]]:
81
self._values[string] = self._default_client.get_list('DEFAULT', string,awn.CONFIG_LIST_STRING)
82
elif self._registered[string] in ['list-integer','list-int',['integer'],['int'],[int]]:
83
self._values[string] = intify(self._default_client.get_list('DEFAULT', string,awn.CONFIG_LIST_STRING))
84
elif self._registered[string] in ['list-boolean','list-bool',['boolean'],['bool'],[bool]]:
85
self._values[string] = self._default_client.get_list('DEFAULT', string,awn.CONFIG_LIST_BOOL)
86
else:#Error when registering the key (setting the type)
87
#print "KEY TYPE ERROR for %s" % string
88
return "KEY TYPE ERROR for %s" % string
89
#print 'Got '+string+' from default'
91
#Set the functions to call for when <string> is changed as an empty list
92
self._connects[string] = []
95
return self._values[string]
97
#A function to call self.get -- in case someone messes up
98
def get_value(self,string):
101
#A function to set the value of a key -- assumes that <string> has already been registered and the <value> is the
102
#same type as when registered
103
def set(self,string,value):
104
#print 'setting '+string+' to '+str(value)
105
#Set the AwnCC value first
106
if self._registered[string] in ['string','str',str]:
107
self._instance_client.set_string('DEFAULT', string, value)
108
elif self._registered[string] in ['integer','int',int]:
109
self._instance_client.set_int('DEFAULT', string, value)
110
elif self._registered[string] in ['float',float]:
111
self._instance_client.set_float('DEFAULT', string, value)
112
elif self._registered[string] in ['boolean','bool',bool]:
113
self._instance_client.set_bool('DEFAULT', string, value)
114
elif self._registered[string] in ['list-string','list-str',['string'],['str'],[str]]:
115
self._instance_client.set_list('DEFAULT', string, awn.CONFIG_LIST_STRING, value)
116
elif self._registered[string] in ['list-integer','list-int',['integer'],['int'],[int]]:
117
self._instance_client.set_list('DEFAULT', string, awn.CONFIG_LIST_STRING, stringify(value))
118
elif self._registered[string] in ['list-boolean','list-bool',['boolean'],['bool'],[bool]]:
119
self._instance_client.set_list('DEFAULT', string, awn.CONFIG_LIST_BOOL, value)
121
#print "KEY TYPE ERROR for %s" % self._registered[string]
122
return "KEY TYPE ERROR for %s" % self._registered[string]
123
#print 'set '+string+' to '+str(value)
125
#Set the value (internally)
126
self._values[string] = value
128
#Last, go through any functions set to call for when <string> is changed
129
for x in self._connects[string]:
130
x[0](string,value,*x[1],**x[2])
132
#A function to call set.set -- in case someone messes up
133
def set_value(self,string,value):
134
self.set(string,value)
136
#A function to set a function to be called when one of <strings> is changed
137
#<arg> is an optional parameter which will be passed to the function, if called
138
#This assumes that each of <strings> has been registered
139
def connect(self,strings,function,*args1,**args2):
140
if type(strings)==type(''):#<strings> is a single string
141
self._connects[strings].append([function,args1,args2])
142
else:#Assume that <strings> is a list of strings
144
self._connects[x].append([function,args1,args2])
147
def disconnect(self, strings, function):
148
if type(strings) == str:
149
for func in self._connects[strings]:
150
if func[0] == function:
151
self._connects[strings].remove(func)
154
for string in strings:
155
for func in self._connects[strings]:
156
if func[0] == function:
157
self._connects[strings].remove(func)
159
#In case the user wants to get a value via <settingsinstance>[<key>]
160
def __getitem__(self,key):
163
#In case the user wants to set a value via <settingsinstance>[<key>] = ...
164
def __setitem__(self,key,value):
165
return self.set(key,value)
167
#list of integers -> list of equivalent strings
168
def stringify(list1):
171
list2.append(str(item))
174
#list of strings -> list of equivalent integers
178
list2.append(int(float(item)))