view tools/memory.py @ 24:61124cb82fb7

Updated README and licenses (for images), added installation instructions.
author Goffi <goffi@goffi.org>
date Tue, 01 Dec 2009 07:55:46 +0100
parents bb72c29f3432
children a544b376b6f0
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
from twisted.internet import defer

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" />
        <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>
    </params>
    """

    def load_default_params(self):
        self.dom = minidom.parseString(Param.default_xml)
    
    def __init__(self, host):
        debug("Parameters init")
        self.host = host
        self.load_default_params()
        host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)

    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 importParams(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):
            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)
        if not node:
            error("Requested param [%s] in category [%s] doesn't exist !" , name, category)
            return
        if node.getAttribute('default_cb') == 'yes':
            del node.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"):
        """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")
           
           @return: attribute"""
        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(attr)

    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
        type = node.getAttribute("type")
        if type=="button":
            print "clique",node.toxml()
        else:
            node.setAttribute("value", value)
            self.host.bridge.paramUpdate(name, value, category)

class Memory:
    """This class manage all persistent informations"""

    def __init__(self, host):
        info ("Memory manager init")
        self.contact={}
        self.presenceStatus={}
        self.params=Param(host)
        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:
                #gof: FIXME FIXME FIXME: sauver l'xml et non plus le pickle !!!!!!!
                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 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, parent, xml):
        return self.params.importParams(parent, xml)
    
    def setDefault(self, name, category, callback, errback=None):
        return self.params.setDefault(name, category, callback, errback)