Mercurial > libervia-backend
view tools/memory.py @ 19:f2a745ca0fbc
refactoring: using xml params part III (parameters import)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 07 Nov 2009 21:23:28 +0100 |
parents | 6928e3cb73a8 |
children | fc8c202cda87 |
line wrap: on
line source
#!/usr/bin/python # -*- coding: utf-8 -*- """ SAT: a jabber client Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org) 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 3 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, see <http://www.gnu.org/licenses/>. """ from __future__ import with_statement import os.path import time import pickle from xml.dom import minidom from logging import debug, info, error import pdb const_SAVEFILE_PARAM=os.path.expanduser("~/.sat_param.save") const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save") class Param(): """This class manage parameter with xml""" ### TODO: add desciption in params #TODO: mettre Watched dans un plugin default_xml = """ <params> <category name="Connection"> <param name="JabberID" value="goffi@necton2.int/TestScript" type="string" /> <param name="Password" value="toto" type="password" /> <param name="Server" value="necton2.int" type="string" /> </category> <category name="Misc"> <param name="Watched" value="test@Jabber.goffi.int" type="string" /> </category> </params> """ def load_default_params(self): self.dom = minidom.parseString(Param.default_xml) def __init__(self): debug("Parameters init") self.load_default_params() def __get_unique_node(self, parent, tag, name): """return node with given tag, create a new one if the node doesn't exist @param parent: parent of nodes to check (e.g. documentElement) @param tag: tag to check (e.g. "category") @param name: name to check (e.g. "JID") @return: node if it exist or None """ for node in parent.childNodes: if node.nodeName == tag and node.getAttribute("name") == name: #the node already exists return node #the node is new return None def import_params(self, parent, xml): """import xml in parameters, do nothing if the param already exist @param parent: parent class (usefull for callbacks) @param xml: parameters in xml form""" src_dom = minidom.parseString(xml) def import_node(tgt_parent, src_parent): print "import_node [%s, %s]" % (tgt_parent.nodeName, src_parent.nodeName) for child in src_parent.childNodes: if child.nodeName == '#text': continue node = self.__get_unique_node(tgt_parent, child.nodeName, child.getAttribute("name")) if not node: #The node is new tgt_parent.appendChild(child) else: import_node(node, child) import_node(self.dom.documentElement, src_dom.documentElement) def getParamV(self, name, category): """Helper method to get the value in the good type getParamV stands for GetParamValue""" node = self.__getParamNode(name, category) if not node: error("Requested param [%s] in category [%s] doesn't exist !" , name, category) return None return node.getAttribute("value") def getParams(self): """Return the whole params XML""" return self.dom.toxml() def getParamsForCategory(self, category): """Return node's xml for selected category""" for node in self.dom.documentElement.childNodes: if node.nodeName == "category" and node.getAttribute("name") == category: return node.toxml() return "<category />" def __getParamNode(self, name, category): for node in self.dom.documentElement.childNodes: if node.nodeName == "category" and node.getAttribute("name") == category: params = node.getElementsByTagName("param") for param in params: if param.getAttribute("name") == name: return param return None def getParamsCategories(self): """return the categories availables""" categories=[] for cat in self.dom.getElementsByTagName("category"): categories.append(cat.getAttribute("name")) return categories def setParam(self, name, value, category): node = self.__getParamNode(name, category) if not node: return #TODO: throw an error node.setAttribute("value", value) class Memory: """This class manage all persistent informations""" def __init__(self): info ("Memory manager init") self.contact={} self.presenceStatus={} self.params=Param() self.history={} self.disco={} #XXX: maybe best in a separate class self.features={} self.load() def load(self): """Load parameters and all memory things from file/db""" #first parameters if os.path.exists(const_SAVEFILE_PARAM): try: with open(const_SAVEFILE_PARAM, 'r') as params_pickle: self.params=pickle.load(params_pickle) debug("params loaded") except: error ("Can't load params !") self.params.load_default_params() else: error ("No params, using default parameters") self.params.load_default_params() #history if os.path.exists(const_SAVEFILE_HISTORY): try: with open(const_SAVEFILE_HISTORY, 'r') as history_pickle: self.history=pickle.load(history_pickle) debug("history loaded") except: error ("Can't load history !") def save(self): """Save parameters and all memory things to file/db""" with open(const_SAVEFILE_PARAM, 'w') as params_pickle: pickle.dump(self.params, params_pickle) with open(const_SAVEFILE_HISTORY, 'w') as history_pickle: pickle.dump(self.history, history_pickle) def addToHistory(self, me_jid, from_jid, to_jid, type, message): me_short=me_jid.userhost() from_short=from_jid.userhost() to_short=to_jid.userhost() if from_jid==me_jid: key=to_short else: key=from_short if not self.history.has_key(me_short): self.history[me_short]={} if not self.history[me_short].has_key(key): self.history[me_short][key]={} self.history[me_short][key][int(time.time())] = (from_short, message) def getHistory(self, from_jid, to_jid, size): ret={} if not self.history.has_key(from_jid): error("source JID not found !") #TODO: throw an error here return {} if not self.history[from_jid].has_key(to_jid): error("dest JID not found !") #TODO: throw an error here return {} stamps=self.history[from_jid][to_jid].keys() stamps.sort() for stamp in stamps[-size:]: ret[stamp]=self.history[from_jid][to_jid][stamp] return ret def addContact(self, JID, attributes, groups): debug("Memory addContact: %s",JID) assert(isinstance(attributes,dict)) assert(isinstance(groups,set)) self.contact[JID]=[attributes, groups] def addPresenceStatus(self, jid, type, show, status, priority): self.presenceStatus[jid]=[type, show, status, priority] def getContacts(self): debug ("Memory getContact OK (%s)", self.contact) ret=[] for contact in self.contact: ret.append([contact] + [self.contact[contact][0]] + [self.contact[contact][1]]) #very ugly I know ! return ret def getPresenceStatus(self): status=[] for contact, contactStatus in self.presenceStatus.items(): status.append([contact]+contactStatus) debug ("Memory getPresenceStatus (%s)", status) return status def getParamV(self, name, category): return self.params.getParamV(name, category) def getParams(self): return self.params.getParams() def getParamsForCategory(self, category): return self.params.getParamsForCategory(category) def getParamsCategories(self): return self.params.getParamsCategories() def setParam(self, name, value, category): return self.params.setParam(name, value, category) def import_params(self, parent, xml): return self.params.import_params(parent, xml)