Mercurial > libervia-backend
view tools/memory.py @ 60:9764e027ecc0
SàT: multi-profile parameters, first draft
- new bridge methods getProfilesList and createProfile
- param xml now use two types: general and individual, general are common parameters, individual parameters are linked to profiles
- indidual xml are constructed from param xml and saved values
/!\ params values are not saved yet
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 28 Jan 2010 02:32:27 +1100 |
parents | a5b5fb5fc9fd |
children | 58d49fc19639 |
line wrap: on
line source
#!/usr/bin/python # -*- coding: utf-8 -*- """ SAT: a jabber client Copyright (C) 2009, 2010 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 from twisted.internet import defer from twisted.words.protocols.jabber import jid SAVEFILE_PARAM="/param" SAVEFILE_HISTORY="/history" SAVEFILE_PRIVATE="/private" #file used to store misc values (mainly for plugins) class Param(): """This class manage parameter with xml""" ### TODO: add desciption in params #TODO: mettre Watched dans un plugin default_xml = u""" <params> <general> </general> <individual> <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" /> <param name="NewAccount" value="Register new account" type="button" callback="registerNewAccount"/> </category> <category name="Misc"> <param name="Watched" value="test@Jabber.goffi.int" type="string" /> </category> </individual> </params> """ def load_default_params(self): self.dom = minidom.parseString(Param.default_xml.encode('utf-8')) def load(self, file): """Load parameters from file""" self.dom = minidom.parse(file) def save(self, file): """Save parameters to xml file""" with open(file, 'wb') as xml_file: self.dom.writexml(xml_file) def __init__(self, host): debug("Parameters init") self.host = host self.default_profile = None self.params = {'goffi':{}} #gof: self.params_gen = {} host.set_const('savefile_param', SAVEFILE_PARAM) host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB) def getProfilesList(self): return self.params.keys() def createProfile(self, name, default=False): """Create a new profile @param name: Name of the profile @param default: True if default value""" if self.params.has_key(name): info ('The profile name already exists') return 1 self.params[name]={} return 0 def getProfileName(self, profile_key): """return profile according to profile_key @param profile_key: profile name or key which can be @ALL@ for all profiles @DEFAULT@ for default profile @return: requested profile name or None if it doesn't exist""" if profile_key=='@DEFAULT@': if not self.params: return None info('No default profile, returning first one') #TODO: manage real default profile return self.params.keys()[0] #FIXME: gof: temporary, must use real default value, and fallback to first one if it doesn't exists if not self.params.has_key(profile_key): error ('Trying to access an unknown profile') return None return profile_key def __get_unique_node(self, parent, tag, name): """return node with given tag @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 importParams(self, xml): """import xml in parameters, do nothing if the param already exist @param xml: parameters in xml form""" src_dom = minidom.parseString(xml.encode('utf-8')) def import_node(tgt_parent, src_parent): 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 __default_ok(self, value, name, category): self.setParam(name, value, category) def __default_ko(self, failure, name, category): error ("Can't determine default value for [%s/%s]: %s" % (category, name, str(failure.value))) def setDefault(self, name, category, callback, errback=None): """Set default value of parameter 'default_cb' attibute of parameter must be set to 'yes' @param name: name of the parameter @param category: category of the parameter @param callback: must return a string with the value (use deferred if needed) @param errback: must manage the error with args failure, name, category """ #TODO: send signal param update if value changed node = self.__getParamNode(name, category, '@ALL@') if not node: error("Requested param [%s] in category [%s] doesn't exist !" , name, category) return if node[1].getAttribute('default_cb') == 'yes': del node[1].attributes['default_cb'] d = defer.maybeDeferred(callback) d.addCallback(self.__default_ok, name, category) d.addErrback(errback or self.__default_ko, name, category) def getParamA(self, name, category, attr="value", profile_key="@DEFAULT@"): """Helper method to get a specific attribute @param name: name of the parameter @param category: category of the parameter @param attr: name of the attribute (default: "value") @param profile: owner of the param (@ALL@ for everyone) @return: attribute""" node = self.__getParamNode(name, category) if not node: error("Requested param [%s] in category [%s] doesn't exist !" , name, category) return None if node[0] == 'general': value = self.__getParam(None, category, name, 'general') return value or node[1].getAttribute(attr) assert(node[0] == 'individual') profile = self.getProfileName(profile_key) if not profile: error('Requesting a param for an non-existant profile') return None return self.__getParam(profile, category, name) or node[1].getAttribute(attr) def __getParam(self, profile, category, name, type='individual'): """Return the param, or None if it doesn't exist @param profile: the profile name (not profile key, i.e. name and not something like @DEFAULT@) @param category: param category @param name: param name """ if type == 'general': if self.params_gen.has_key((category, name)): return self.params_gen[(category, name)] return None #This general param has the default value assert (type == 'individual') if not self.params.has_key(profile) or not self.params[profile].has_key((category, name)): return None return self.params[profile][(category, name)] def __constructProfileXml(self, profile): """Construct xml for asked profile, filling values when needed /!\ as noticed in doc, don't forget to unlink the minidom.Document @param profile: profile name (not key !) @return: minidom.Document of the profile xml (cf warning above) """ prof_xml = minidom.parseString('<params/>') for type_node in self.dom.documentElement.childNodes: if type_node.nodeName == 'general' or type_node.nodeName == 'individual': #we use all params, general and individual for cat_node in type_node.childNodes: if cat_node.nodeName == 'category': category = cat_node.getAttribute('name') cat_copy = cat_node.cloneNode(True) #we make a copy for the new xml params = cat_copy.getElementsByTagName("param") for param_node in params: name = param_node.getAttribute('name') profile_value = self.__getParam(profile, category, name, type_node.nodeName) if profile_value: #there is a value for this profile, we must change the default param_node.setAttribute('value', profile_value) prof_xml.documentElement.appendChild(cat_copy) return prof_xml def getParams(self, profile_key='@DEFAULT@'): """Construct xml for asked profile Take params xml as skeleton""" profile = self.getProfileName(profile_key) if not profile: error("Asking params for inexistant profile") return "" prof_xml = self.__constructProfileXml(profile) return_xml = prof_xml.toxml() prof_xml.unlink() return return_xml def getParamsForCategory(self, category, profile_key='@DEFAULT@'): """Return node's xml for selected category""" #TODO: manage category of general type (without existant profile) profile = self.getProfileName(profile_key) if not profile: error("Asking params for inexistant profile") return "" prof_xml = self.__constructProfileXml(profile) for node in prof_xml.getElementsByTagName("category"): if node.nodeName == "category" and node.getAttribute("name") == category: result = node.toxml() prof_xml.unlink() return result prof_xml.unlink() return "<category />" def __getParamNode(self, name, category, type="@ALL@"): #FIXME: is type useful ? """Return a node from the param_xml @param name: name of the node @param category: category of the node @type: keyword for search: @ALL@ search everywhere @GENERAL@ only search in general type @INDIVIDUAL@ only search in individual type @return: a tuple with the node type and the the node, or None if not found""" for type_node in self.dom.documentElement.childNodes: if ( ((type == "@ALL@" or type == "@GENERAL@") and type_node.nodeName == 'general') or ( (type == "@ALL@" or type == "@INDIVIDUAL@") and type_node.nodeName == 'individual') ): for node in type_node.getElementsByTagName('category'): if node.getAttribute("name") == category: params = node.getElementsByTagName("param") for param in params: if param.getAttribute("name") == name: return (type_node.nodeName, 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, profile_key='@DEFAULT@'): """Set a parameter, return None if the parameter is not in param xml""" node = self.__getParamNode(name, category, '@ALL@') if not node: error('Requesting an unknown parameter (%s/%s)' % (category, name)) return if node[0] == 'general': self.params_gen[(category, name)] = value return assert (node[0] == 'individual') profile = self.getProfileName(profile_key) if not profile: error('Trying to set parameter for an unknown profile') return #TODO: throw an error type = node.getAttribute("type") if type=="button": print "clique",node.toxml() else: self.params[profile][(category, name)] = value self.host.bridge.paramUpdate(name, value, category) #TODO: add profile in signal class Memory: """This class manage all persistent informations""" def __init__(self, host): info ("Memory manager init") self.host = host self.contacts={} self.presenceStatus={} self.subscriptions={} self.params=Param(host) self.history={} #used to store chat history (key: short jid) self.private={} #used to store private value self.disco={} #XXX: maybe best in a separate class self.features={} host.set_const('savefile_history', SAVEFILE_HISTORY) host.set_const('savefile_private', SAVEFILE_PRIVATE) self.load() def load(self): """Load parameters and all memory things from file/db""" param_file = os.path.expanduser(self.host.get_const('local_dir')+ self.host.get_const('savefile_param')) history_file = os.path.expanduser(self.host.get_const('local_dir')+ self.host.get_const('savefile_history')) private_file = os.path.expanduser(self.host.get_const('local_dir')+ self.host.get_const('savefile_private')) #parameters if os.path.exists(param_file): try: self.params.load(param_file) 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(history_file): try: with open(history_file, 'r') as history_pickle: self.history=pickle.load(history_pickle) debug("history loaded") except: error ("Can't load history !") #private if os.path.exists(private_file): try: with open(private_file, 'r') as private_pickle: self.private=pickle.load(private_pickle) debug("private values loaded") except: error ("Can't load private values !") def save(self): """Save parameters and all memory things to file/db""" #TODO: need to encrypt files (at least passwords !) and set permissions param_file = os.path.expanduser(self.host.get_const('local_dir')+ self.host.get_const('savefile_param')) history_file = os.path.expanduser(self.host.get_const('local_dir')+ self.host.get_const('savefile_history')) private_file = os.path.expanduser(self.host.get_const('local_dir')+ self.host.get_const('savefile_private')) self.params.save(param_file) debug("params saved") with open(history_file, 'w') as history_pickle: pickle.dump(self.history, history_pickle) debug("history saved") with open(private_file, 'w') as private_pickle: pickle.dump(self.private, private_pickle) debug("private values saved") def getProfilesList(self): return self.params.getProfilesList() def createProfile(self, name, default=False): """Create a new profile @param name: Profile name @param default: True if default profile (replace previous default) """ return self.params.createProfile(name, default) 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 setPrivate(self, key, value): """Save a misc private value (mainly useful for plugins)""" self.private[key] = value def getPrivate(self, key): """return a private value @param key: name of wanted value @return: value or None if value don't exist""" if self.private.has_key(key): return self.private[key] return None def addContact(self, contact_jid, attributes, groups): debug("Memory addContact: %s",contact_jid.userhost()) assert(isinstance(attributes,dict)) assert(isinstance(groups,set)) self.contacts[contact_jid.userhost()]=[attributes, groups] def delContact(self, contact_jid): debug("Memory delContact: %s",contact_jid.userhost()) if self.contacts.has_key(contact_jid.userhost()): del self.contacts[contact_jid.userhost()] def getContact(self, contact_jid): if self.contacts.has_key(contact_jid.userhost()): self.contacts[contact_jid.userhost()] else: return None def getContacts(self): debug ("Memory getContact OK (%s)", self.contacts) ret=[] for contact in self.contacts: attr, groups = self.contacts[contact] ret.append([contact, attr, groups ]) return ret def addPresenceStatus(self, contact_jid, show, priority, statuses): if not self.presenceStatus.has_key(contact_jid.userhost()): self.presenceStatus[contact_jid.userhost()] = {} resource = jid.parse(contact_jid.full())[2] or '' self.presenceStatus[contact_jid.userhost()][resource] = (show, priority, statuses) def addWaitingSub(self, type, contact_jid): """Called when a subcription request is received""" self.subscriptions[contact_jid] = type def delWaitingSub(self, contact_jid): """Called when a subcription request is finished""" if self.subscriptions.has_key(contact_jid): del self.subscriptions[contact_jid] def getWaitingSub(self): """Called to get a list of currently waiting subscription requests""" return self.subscriptions def getPresenceStatus(self): debug ("Memory getPresenceStatus (%s)", self.presenceStatus) return self.presenceStatus def getParamA(self, name, category, attr="value"): return self.params.getParamA(name, category, attr) 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 importParams(self, xml): return self.params.importParams(xml) def setDefault(self, name, category, callback, errback=None): return self.params.setDefault(name, category, callback, errback)