annotate tools/memory.py @ 4:3c1cefd41186

added sat executable added twistd.log to .hgignore
author Goffi <goffi@goffi.org>
date Mon, 19 Oct 2009 22:53:21 +0200
parents c4bc297b82f0
children bd9e9997d540
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
goffi@necton2
parents:
diff changeset
1 #!/usr/bin/python
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
goffi@necton2
parents:
diff changeset
4 """
goffi@necton2
parents:
diff changeset
5 SAT: a jabber client
goffi@necton2
parents:
diff changeset
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
goffi@necton2
parents:
diff changeset
7
goffi@necton2
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
goffi@necton2
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
goffi@necton2
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
goffi@necton2
parents:
diff changeset
11 (at your option) any later version.
goffi@necton2
parents:
diff changeset
12
goffi@necton2
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
goffi@necton2
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
goffi@necton2
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
goffi@necton2
parents:
diff changeset
16 GNU General Public License for more details.
goffi@necton2
parents:
diff changeset
17
goffi@necton2
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
goffi@necton2
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
goffi@necton2
parents:
diff changeset
20 """
goffi@necton2
parents:
diff changeset
21
goffi@necton2
parents:
diff changeset
22 from __future__ import with_statement
goffi@necton2
parents:
diff changeset
23
goffi@necton2
parents:
diff changeset
24 import os.path
goffi@necton2
parents:
diff changeset
25 import time
goffi@necton2
parents:
diff changeset
26 import pickle
goffi@necton2
parents:
diff changeset
27 from logging import debug, info, error
goffi@necton2
parents:
diff changeset
28
goffi@necton2
parents:
diff changeset
29 const_SAVEFILE_PARAM=os.path.expanduser("~/.sat_param.save")
goffi@necton2
parents:
diff changeset
30 const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save")
goffi@necton2
parents:
diff changeset
31
goffi@necton2
parents:
diff changeset
32 class Memory:
goffi@necton2
parents:
diff changeset
33 """This class manage all persistent informations"""
goffi@necton2
parents:
diff changeset
34
goffi@necton2
parents:
diff changeset
35 def __init__(self):
goffi@necton2
parents:
diff changeset
36 info ("Memory manager init")
goffi@necton2
parents:
diff changeset
37 self.contact={}
goffi@necton2
parents:
diff changeset
38 self.presenceStatus={}
goffi@necton2
parents:
diff changeset
39 self.params={}
goffi@necton2
parents:
diff changeset
40 self.history={}
goffi@necton2
parents:
diff changeset
41 self.disco={} #XXX: maybe best in a separate class
goffi@necton2
parents:
diff changeset
42 self.features={}
goffi@necton2
parents:
diff changeset
43 self.load()
goffi@necton2
parents:
diff changeset
44
goffi@necton2
parents:
diff changeset
45 def load_default_params(self):
goffi@necton2
parents:
diff changeset
46 """Load default value, in case of no save file or error."""
goffi@necton2
parents:
diff changeset
47
goffi@necton2
parents:
diff changeset
48 self.createParam("JabberID", "goffi@jabber.goffi.int/TestScript", "string", "Connection")
goffi@necton2
parents:
diff changeset
49 self.createParam("Password", "toto", "password", "Connection")
goffi@necton2
parents:
diff changeset
50 self.createParam("Server", "jabber.goffi.int", "string", "Connection")
goffi@necton2
parents:
diff changeset
51 self.createParam("IP", "192.168.0.2", "string", "File Transfert")
goffi@necton2
parents:
diff changeset
52 self.createParam("Port", "28915", "string", "File Transfert")
goffi@necton2
parents:
diff changeset
53 self.createParam("Watched", "test@jabber.goffi.int", "string", "Misc") #TODO: a mettre dans un plugin
goffi@necton2
parents:
diff changeset
54
goffi@necton2
parents:
diff changeset
55 def load(self):
goffi@necton2
parents:
diff changeset
56 """Load parameters and all memory things from file/db"""
goffi@necton2
parents:
diff changeset
57
goffi@necton2
parents:
diff changeset
58 #first parameters
goffi@necton2
parents:
diff changeset
59 if os.path.exists(const_SAVEFILE_PARAM):
goffi@necton2
parents:
diff changeset
60 try:
goffi@necton2
parents:
diff changeset
61 with open(const_SAVEFILE_PARAM, 'r') as params_pickle:
goffi@necton2
parents:
diff changeset
62 self.params=pickle.load(params_pickle)
goffi@necton2
parents:
diff changeset
63 debug("params loaded")
goffi@necton2
parents:
diff changeset
64 except:
goffi@necton2
parents:
diff changeset
65 error ("Can't load params !")
goffi@necton2
parents:
diff changeset
66 self.load_default_params()
goffi@necton2
parents:
diff changeset
67 else:
goffi@necton2
parents:
diff changeset
68 self.load_default_params()
goffi@necton2
parents:
diff changeset
69
goffi@necton2
parents:
diff changeset
70 #history
goffi@necton2
parents:
diff changeset
71 if os.path.exists(const_SAVEFILE_HISTORY):
goffi@necton2
parents:
diff changeset
72 try:
goffi@necton2
parents:
diff changeset
73 with open(const_SAVEFILE_HISTORY, 'r') as history_pickle:
goffi@necton2
parents:
diff changeset
74 self.history=pickle.load(history_pickle)
goffi@necton2
parents:
diff changeset
75 debug("history loaded")
goffi@necton2
parents:
diff changeset
76 except:
goffi@necton2
parents:
diff changeset
77 error ("Can't load history !")
goffi@necton2
parents:
diff changeset
78
goffi@necton2
parents:
diff changeset
79
goffi@necton2
parents:
diff changeset
80 def save(self):
goffi@necton2
parents:
diff changeset
81 """Save parameters and all memory things to file/db"""
goffi@necton2
parents:
diff changeset
82 with open(const_SAVEFILE_PARAM, 'w') as params_pickle:
goffi@necton2
parents:
diff changeset
83 pickle.dump(self.params, params_pickle)
goffi@necton2
parents:
diff changeset
84 with open(const_SAVEFILE_HISTORY, 'w') as history_pickle:
goffi@necton2
parents:
diff changeset
85 pickle.dump(self.history, history_pickle)
goffi@necton2
parents:
diff changeset
86
goffi@necton2
parents:
diff changeset
87 def addToHistory(self, me_jid, from_jid, to_jid, type, message):
goffi@necton2
parents:
diff changeset
88 me_short=me_jid.userhost()
goffi@necton2
parents:
diff changeset
89 from_short=from_jid.userhost()
goffi@necton2
parents:
diff changeset
90 to_short=to_jid.userhost()
goffi@necton2
parents:
diff changeset
91
goffi@necton2
parents:
diff changeset
92 if from_jid==me_jid:
goffi@necton2
parents:
diff changeset
93 key=to_short
goffi@necton2
parents:
diff changeset
94 else:
goffi@necton2
parents:
diff changeset
95 key=from_short
goffi@necton2
parents:
diff changeset
96
goffi@necton2
parents:
diff changeset
97 if not self.history.has_key(me_short):
goffi@necton2
parents:
diff changeset
98 self.history[me_short]={}
goffi@necton2
parents:
diff changeset
99 if not self.history[me_short].has_key(key):
goffi@necton2
parents:
diff changeset
100 self.history[me_short][key]={}
goffi@necton2
parents:
diff changeset
101
goffi@necton2
parents:
diff changeset
102 self.history[me_short][key][int(time.time())] = (from_short, message)
goffi@necton2
parents:
diff changeset
103
goffi@necton2
parents:
diff changeset
104 def getHistory(self, from_jid, to_jid, size):
goffi@necton2
parents:
diff changeset
105 ret={}
goffi@necton2
parents:
diff changeset
106 if not self.history.has_key(from_jid):
goffi@necton2
parents:
diff changeset
107 error("source JID not found !")
goffi@necton2
parents:
diff changeset
108 #TODO: throw an error here
goffi@necton2
parents:
diff changeset
109 return {}
goffi@necton2
parents:
diff changeset
110 if not self.history[from_jid].has_key(to_jid):
goffi@necton2
parents:
diff changeset
111 error("dest JID not found !")
goffi@necton2
parents:
diff changeset
112 #TODO: throw an error here
goffi@necton2
parents:
diff changeset
113 return {}
goffi@necton2
parents:
diff changeset
114 stamps=self.history[from_jid][to_jid].keys()
goffi@necton2
parents:
diff changeset
115 stamps.sort()
goffi@necton2
parents:
diff changeset
116 for stamp in stamps[-size:]:
goffi@necton2
parents:
diff changeset
117 ret[stamp]=self.history[from_jid][to_jid][stamp]
goffi@necton2
parents:
diff changeset
118
goffi@necton2
parents:
diff changeset
119 return ret
goffi@necton2
parents:
diff changeset
120
goffi@necton2
parents:
diff changeset
121 def addContact(self, JID, attributes, groups):
goffi@necton2
parents:
diff changeset
122 debug("Memory addContact: %s",JID)
goffi@necton2
parents:
diff changeset
123 assert(isinstance(attributes,dict))
goffi@necton2
parents:
diff changeset
124 assert(isinstance(groups,list))
goffi@necton2
parents:
diff changeset
125 self.contact[JID]=[attributes, groups]
goffi@necton2
parents:
diff changeset
126
goffi@necton2
parents:
diff changeset
127 def addPresenceStatus(self, jid, type, show, status, priority):
goffi@necton2
parents:
diff changeset
128 self.presenceStatus[jid]=[type, show, status, priority]
goffi@necton2
parents:
diff changeset
129
goffi@necton2
parents:
diff changeset
130 def getContacts(self):
goffi@necton2
parents:
diff changeset
131 debug ("Memory getContact OK (%s)", self.contact)
goffi@necton2
parents:
diff changeset
132 ret=[]
goffi@necton2
parents:
diff changeset
133 for contact in self.contact:
goffi@necton2
parents:
diff changeset
134 ret.append([contact] + [self.contact[contact][0]] + [self.contact[contact][1]]) #very ugly I know !
goffi@necton2
parents:
diff changeset
135 return ret
goffi@necton2
parents:
diff changeset
136
goffi@necton2
parents:
diff changeset
137 def getPresenceStatus(self):
goffi@necton2
parents:
diff changeset
138 status=[]
goffi@necton2
parents:
diff changeset
139 for contact, contactStatus in self.presenceStatus.items():
goffi@necton2
parents:
diff changeset
140 status.append([contact]+contactStatus)
goffi@necton2
parents:
diff changeset
141 debug ("Memory getPresenceStatus (%s)", status)
goffi@necton2
parents:
diff changeset
142 return status
goffi@necton2
parents:
diff changeset
143
goffi@necton2
parents:
diff changeset
144 def getParamV(self, name, namespace):
goffi@necton2
parents:
diff changeset
145 """Helper method to get the value in the good type
goffi@necton2
parents:
diff changeset
146 getParamV stands for GetParamValue"""
goffi@necton2
parents:
diff changeset
147 if not self.params.has_key(namespace) or not self.params[namespace].has_key(name):
goffi@necton2
parents:
diff changeset
148 error("Requested param %s in namespace %s doesn't exist !" , name, namespace)
goffi@necton2
parents:
diff changeset
149 return None
goffi@necton2
parents:
diff changeset
150 return self.params[namespace][name][0]
goffi@necton2
parents:
diff changeset
151
goffi@necton2
parents:
diff changeset
152
goffi@necton2
parents:
diff changeset
153 def getParam(self, name, namespace):
goffi@necton2
parents:
diff changeset
154 if self.params.has_key(namespace) and self.params[namespace].has_key(name):
goffi@necton2
parents:
diff changeset
155 return self.params[namespace][name]
goffi@necton2
parents:
diff changeset
156 return ["",""]
goffi@necton2
parents:
diff changeset
157
goffi@necton2
parents:
diff changeset
158 def getParams(self, namespace):
goffi@necton2
parents:
diff changeset
159 if self.params.has_key(namespace):
goffi@necton2
parents:
diff changeset
160 ret=[]
goffi@necton2
parents:
diff changeset
161 for name in self.params[namespace]:
goffi@necton2
parents:
diff changeset
162 ret.append([name] + self.params[namespace][name])
goffi@necton2
parents:
diff changeset
163 return ret
goffi@necton2
parents:
diff changeset
164 return [[]]
goffi@necton2
parents:
diff changeset
165
goffi@necton2
parents:
diff changeset
166 def getParamsCategories(self):
goffi@necton2
parents:
diff changeset
167 """return the namespaces availables"""
goffi@necton2
parents:
diff changeset
168 return self.params.keys()
goffi@necton2
parents:
diff changeset
169
goffi@necton2
parents:
diff changeset
170 def setParam(self, name, value, namespace):
goffi@necton2
parents:
diff changeset
171 if not self.params.has_key(namespace) or not self.params[namespace].has_key(name):
goffi@necton2
parents:
diff changeset
172 return #TODO: throw an error
goffi@necton2
parents:
diff changeset
173 self.params[namespace][name][0]=value;
goffi@necton2
parents:
diff changeset
174
goffi@necton2
parents:
diff changeset
175 def createParam (self, name, value, type, namespace):
goffi@necton2
parents:
diff changeset
176 ### TODO: add desciption in params
goffi@necton2
parents:
diff changeset
177 if not self.params.has_key(namespace):
goffi@necton2
parents:
diff changeset
178 self.params[namespace]={}
goffi@necton2
parents:
diff changeset
179 self.params[namespace][name]=[value,type];
goffi@necton2
parents:
diff changeset
180
goffi@necton2
parents:
diff changeset
181 def registerFeature(self, feature, callback=None):
goffi@necton2
parents:
diff changeset
182 self.features[feature]=callback
goffi@necton2
parents:
diff changeset
183