# HG changeset patch # User Emmanuel Gil Peyrot # Date 1358528135 -3600 # Node ID e5a875a3311b86d8adeb0b14a86123b3d9096281 # Parent 65821b3fa7ab133e09223bdb70fcebea7914e00e Fix pep8 support in src/memory. diff -r 65821b3fa7ab -r e5a875a3311b src/memory/memory.py --- a/src/memory/memory.py Fri Jan 18 17:55:35 2013 +0100 +++ b/src/memory/memory.py Fri Jan 18 17:55:35 2013 +0100 @@ -33,8 +33,8 @@ from sat.memory.persistent import PersistentDict from sat.core import exceptions -SAVEFILE_PARAM_XML="/param" #xml parameters template -SAVEFILE_DATABASE="/sat.db" +SAVEFILE_PARAM_XML = "/param" # xml parameters template +SAVEFILE_DATABASE = "/sat.db" class Params(object): @@ -61,18 +61,20 @@ - """ % {'category_connection': _("Connection"), - 'label_NewAccount': _("Register new account"), - 'label_autoconnect': _('Connect on frontend startup'), - 'label_autodisconnect': _('Disconnect on frontend closure'), - 'category_misc': _("Misc") - } + """ % { + 'category_connection': _("Connection"), + 'label_NewAccount': _("Register new account"), + 'label_autoconnect': _('Connect on frontend startup'), + 'label_autodisconnect': _('Disconnect on frontend closure'), + 'category_misc': _("Misc") + } def load_default_params(self): self.dom = minidom.parseString(Params.default_xml.encode('utf-8')) def _mergeParams(self, source_node, dest_node): """Look for every node in source_node and recursively copy them to dest if they don't exists""" + def getNodesMap(children): ret = {} for child in children: @@ -109,9 +111,9 @@ @param profile: profile to load (*must exist*) @param cache: if not None, will be used to store the value, as a short time cache @return: deferred triggered once params are loaded""" - if cache == None: + if cache is None: self.params[profile] = {} - return self.storage.loadIndParams(self.params[profile] if cache==None else cache, profile) + return self.storage.loadIndParams(self.params[profile] if cache is None else cache, profile) def purgeProfile(self, profile): """Remove cache data of a profile @@ -121,9 +123,9 @@ except KeyError: error(_("Trying to purge cache of a profile not in memory: [%s]") % profile) - def save_xml(self, file): + def save_xml(self, filename): """Save parameters template to xml file""" - with open(file, 'wb') as xml_file: + with open(filename, 'wb') as xml_file: xml_file.write(self.dom.toxml('utf-8')) def __init__(self, host, storage): @@ -141,7 +143,7 @@ @param profile: profile of the profile""" #FIXME: must be asynchronous and call the callback once the profile actually exists if self.storage.hasProfile(profile): - info (_('The profile [%s] already exists') % (profile,)) + info(_('The profile [%s] already exists') % (profile, )) return True if not self.host.trigger.point("ProfileCreation", profile): return False @@ -157,13 +159,12 @@ - CANCELED: profile creation canceled """ if self.storage.hasProfile(profile): - info (_('The profile name already exists')) + info(_('The profile name already exists')) return defer.fail("CONFLICT") if not self.host.trigger.point("ProfileCreation", profile): return defer.fail("CANCEL") return self.storage.createProfile(profile) - def deleteProfile(self, profile): """Delete an existing profile @param profile: name of the profile""" @@ -183,14 +184,14 @@ @ALL@ for all profiles @DEFAULT@ for default profile @return: requested profile name or None if it doesn't exist""" - if profile_key=='@DEFAULT@': + if profile_key == '@DEFAULT@': default = self.host.memory.memory_data.get('Profile_default') if not default: - info(_('No default profile, returning first one')) #TODO: manage real default profile + info(_('No default profile, returning first one')) # TODO: manage real default profile default = self.host.memory.memory_data['Profile_default'] = self.storage.getProfilesList()[0] - return default #FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists + return default # FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists if not self.storage.hasProfile(profile_key): - info (_('Trying to access an unknown profile')) + info(_('Trying to access an unknown profile')) return "" return profile_key @@ -218,7 +219,7 @@ if child.nodeName == '#text': continue node = self.__get_unique_node(tgt_parent, child.nodeName, child.getAttribute("name")) - if not node: #The node is new + if not node: # The node is new tgt_parent.appendChild(child) else: import_node(node, child) @@ -227,10 +228,10 @@ def __default_ok(self, value, name, category): #FIXME: gof: will not work with individual parameters - self.setParam(name, value, category) #FIXME: better to set param xml value ??? + self.setParam(name, value, category) # FIXME: better to set param xml value ??? def __default_ko(self, failure, name, category): - error (_("Can't determine default value for [%(category)s/%(name)s]: %(reason)s") % {'category':category, 'name':name, 'reason':str(failure.value)}) + error(_("Can't determine default value for [%(category)s/%(name)s]: %(reason)s") % {'category': category, 'name': name, 'reason': str(failure.value)}) def setDefault(self, name, category, callback, errback=None): """Set default value of parameter @@ -241,9 +242,9 @@ @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@') + node = self.__getParamNode(name, category, '@ALL@') if not node: - error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category}) + error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category}) return if node[1].getAttribute('default_cb') == 'yes': del node[1].attributes['default_cb'] @@ -257,15 +258,15 @@ @param attr: name of the attribute to get (e.g.: 'value' or 'type') @param value: user defined value""" if attr == 'value': - value_to_use = value if value!=None else node.getAttribute(attr) #we use value (user defined) if it exist, else we use node's default value + value_to_use = value if value is not None else node.getAttribute(attr) # we use value (user defined) if it exist, else we use node's default value if node.getAttribute('type') == 'bool': - return value_to_use.lower() not in ('false','0') + return value_to_use.lower() not in ('false', '0') return value_to_use return node.getAttribute(attr) def __type_to_string(self, result): """ convert result to string, according to its type """ - if isinstance(result,bool): + if isinstance(result, bool): return "true" if result else "false" return result @@ -284,14 +285,14 @@ #FIXME: looks really dirty and buggy, need to be reviewed/refactored node = self.__getParamNode(name, category) if not node: - error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category}) + error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category}) raise exceptions.NotFound if node[0] == 'general': value = self.__getParam(None, category, name, 'general') return self.__getAttr(node[1], attr, value) - assert(node[0] == 'individual') + assert node[0] == 'individual' profile = self.getProfileName(profile_key) if not profile: @@ -319,14 +320,14 @@ @param profile: owner of the param (@ALL@ for everyone)""" node = self.__getParamNode(name, category) if not node: - error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category}) + error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category}) return None if node[0] == 'general': value = self.__getParam(None, category, name, 'general') return defer.succeed(self.__getAttr(node[1], attr, value)) - assert(node[0] == 'individual') + assert node[0] == 'individual' profile = self.getProfileName(profile_key) if not profile: @@ -353,16 +354,16 @@ @return: param value or None if it doesn't exist """ if _type == 'general': - if self.params_gen.has_key((category, name)): + if (category, name) in self.params_gen: return self.params_gen[(category, name)] - return None #This general param has the default value + return None # This general param has the default value assert (_type == 'individual') - if self.params.has_key(profile): - cache = self.params[profile] # if profile is in main cache, we use it, - # ignoring the temporary cache - elif cache == None: #else we use the temporary cache if it exists, or raise an exception + if profile in self.params: + cache = self.params[profile] # if profile is in main cache, we use it, + # ignoring the temporary cache + elif cache is None: # else we use the temporary cache if it exists, or raise an exception raise exceptions.ProfileNotInCacheError - if not cache.has_key((category, name)): + if (category, name) not in cache: return None return cache[(category, name)] @@ -372,21 +373,22 @@ @param profile: profile name (not key !) @return: a deferred that fire a minidom.Document of the profile xml (cf warning above) """ - def constructProfile(ignore,profile_cache): + + def constructProfile(ignore, profile_cache): prof_xml = minidom.parseString('') cache = {} 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 + 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') - if not cache.has_key(category): - cache[category] = dest_cat = cat_node.cloneNode(True) #we make a copy for the new xml + if category not in cache: + cache[category] = dest_cat = cat_node.cloneNode(True) # we make a copy for the new xml new_node = True else: dest_cat = cache[category] - new_node = False #It's not a new node, we will merge information + new_node = False # It's not a new node, we will merge information params = cat_node.getElementsByTagName("param") dest_params = {} for node in dest_cat.childNodes: @@ -402,14 +404,13 @@ dest_cat.appendChild(dest_params[name]) profile_value = self.__getParam(profile, category, name, type_node.nodeName, cache=profile_cache) - if profile_value!=None: #there is a value for this profile, we must change the default + if profile_value is not None: # there is a value for this profile, we must change the default dest_params[name].setAttribute('value', profile_value) if new_node: prof_xml.documentElement.appendChild(dest_cat) return prof_xml - - if self.params.has_key(profile): + if profile in self.params: d = defer.succeed(None) profile_cache = self.params[profile] else: @@ -426,7 +427,7 @@ error(_("Asking params for inexistant profile")) return "" d = self.getParams(profile) - return d.addCallback(lambda param_xml:paramsXml2xmlUI(param_xml)) + return d.addCallback(lambda param_xml: paramsXml2xmlUI(param_xml)) def getParams(self, profile_key): """Construct xml for asked profile @@ -464,7 +465,7 @@ d = self.__constructProfileXml(profile) return d.addCallback(returnCategoryXml) - def __getParamNode(self, name, category, _type="@ALL@"): #FIXME: is _type useful ? + 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 @@ -475,8 +476,8 @@ @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') ): + 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") @@ -487,7 +488,7 @@ def getParamsCategories(self): """return the categories availables""" - categories=[] + categories = [] for cat in self.dom.getElementsByTagName("category"): name = cat.getAttribute("name") if name not in categories: @@ -497,15 +498,15 @@ def setParam(self, name, value, category, profile_key='@NONE@'): """Set a parameter, return None if the parameter is not in param xml""" #TODO: use different behaviour depending of the data type (e.g. password encrypted) - if profile_key!="@NONE@": + if profile_key != "@NONE@": profile = self.getProfileName(profile_key) if not profile: error(_('Trying to set parameter for an unknown profile')) - return #TODO: throw an error + return # TODO: throw an error node = self.__getParamNode(name, category, '@ALL@') if not node: - error(_('Requesting an unknown parameter (%(category)s/%(name)s)') % {'category':category, 'name':name}) + error(_('Requesting an unknown parameter (%(category)s/%(name)s)') % {'category': category, 'name': name}) return if node[0] == 'general': @@ -520,35 +521,36 @@ assert (profile_key != "@NONE@") _type = node[1].getAttribute("type") - if _type=="button": - print "clique",node.toxml() + if _type == "button": + print "clique", node.toxml() else: - if self.host.isConnected(profile): #key can not exists if profile is not connected + if self.host.isConnected(profile): # key can not exists if profile is not connected self.params[profile][(category, name)] = value self.host.bridge.paramUpdate(name, value, category, profile) self.storage.setIndParam(category, name, value, profile) + class Memory(object): """This class manage all persistent informations""" def __init__(self, host): - info (_("Memory manager init")) + info(_("Memory manager init")) self.initialized = defer.Deferred() self.host = host - self.entitiesCache={} #XXX: keep presence/last resource/other data in cache + self.entitiesCache = {} # XXX: keep presence/last resource/other data in cache # /!\ an entity is not necessarily in roster - self.subscriptions={} - self.server_features={} #used to store discovery's informations - self.server_identities={} + self.subscriptions = {} + self.server_features = {} # used to store discovery's informations + self.server_identities = {} self.config = self.parseMainConf() host.set_const('savefile_database', SAVEFILE_DATABASE) - database_file = os.path.expanduser(self.getConfig('','local_dir')+ - self.host.get_const('savefile_database')) + database_file = os.path.expanduser(self.getConfig('', 'local_dir') + + self.host.get_const('savefile_database')) self.storage = SqliteStorage(database_file) PersistentDict.storage = self.storage - self.params=Params(host, self.storage) + self.params = Params(host, self.storage) self.loadFiles() - d = self.storage.initialized.addCallback(lambda ignore:self.load()) + d = self.storage.initialized.addCallback(lambda ignore: self.load()) self.memory_data = PersistentDict("memory") d.addCallback(lambda ignore: self.memory_data.load()) d.chainDeferred(self.initialized) @@ -559,7 +561,7 @@ try: _config.read(map(os.path.expanduser, ['/etc/sat.conf', '~/sat.conf', '~/.sat.conf', 'sat.conf', '.sat.conf'])) except: - error (_("Can't read main config !")) + error(_("Can't read main config !")) return _config @@ -569,7 +571,7 @@ @param name: name of the option """ if not section: - section='DEFAULT' + section = 'DEFAULT' try: _value = self.config.get(section, name) except (NoOptionError, NoSectionError): @@ -577,11 +579,10 @@ return os.path.expanduser(_value) if name.endswith('_path') or name.endswith('_dir') else _value - def loadFiles(self): """Load parameters and all memory things from file/db""" - param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+ - self.host.get_const('savefile_param_xml')) + param_file_xml = os.path.expanduser(self.getConfig('', 'local_dir') + + self.host.get_const('savefile_param_xml')) #parameters template if os.path.exists(param_file_xml): @@ -589,13 +590,12 @@ self.params.load_xml(param_file_xml) debug(_("params template loaded")) except Exception as e: - error (_("Can't load params template: %s") % (e,)) + error(_("Can't load params template: %s") % (e, )) self.params.load_default_params() else: - info (_("No params template, using default template")) + info(_("No params template, using default template")) self.params.load_default_params() - def load(self): """Load parameters and all memory things from db""" #parameters data @@ -622,12 +622,11 @@ except KeyError: error(_("Trying to purge roster status cache for a profile not in memory: [%s]") % profile) - 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_xml = os.path.expanduser(self.getConfig('','local_dir')+ - self.host.get_const('savefile_param_xml')) + param_file_xml = os.path.expanduser(self.getConfig('', 'local_dir') + + self.host.get_const('savefile_param_xml')) self.params.save_xml(param_file_xml) debug(_("params saved")) @@ -635,7 +634,6 @@ def getProfilesList(self): return self.storage.getProfilesList() - def getProfileName(self, profile_key): """Return name of profile from keyword @param profile_key: can be the profile name or a keywork (like @DEFAULT@) @@ -660,18 +658,18 @@ return self.params.deleteProfile(name) def addToHistory(self, from_jid, to_jid, message, _type='chat', timestamp=None, profile="@NONE@"): - assert(profile!="@NONE@") + assert profile != "@NONE@" return self.storage.addToHistory(from_jid, to_jid, message, _type, timestamp, profile) def getHistory(self, from_jid, to_jid, limit=0, between=True, profile="@NONE@"): - assert(profile != "@NONE@") + assert profile != "@NONE@" return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between, profile) def addServerFeature(self, feature, profile): """Add a feature discovered from server @param feature: string of the feature @param profile: which profile is using this server ?""" - if not self.server_features.has_key(profile): + if profile not in self.server_features: self.server_features[profile] = [] self.server_features[profile].append(feature) @@ -681,8 +679,8 @@ @param profile: which profile is using this server ?""" if not profile in self.server_identities: self.server_identities[profile] = {} - if not self.server_identities[profile].has_key((category, _type)): - self.server_identities[profile][(category, _type)]=set() + if (category, _type) not in self.server_identities[profile]: + self.server_identities[profile][(category, _type)] = set() self.server_identities[profile][(category, _type)].add(entity) def getServerServiceEntities(self, category, _type, profile): @@ -695,9 +693,9 @@ def getServerServiceEntity(self, category, _type, profile): """Helper method to get first available entity for a service""" entities = self.getServerServiceEntities(category, _type, profile) - if entities == None: - warning(_("Entities (%(category)s/%(type)s) not available, maybe they haven't been asked to server yet ?") % {"category":category, - "type":_type}) + if entities is None: + warning(_("Entities (%(category)s/%(type)s) not available, maybe they haven't been asked to server yet ?") % {"category": category, + "type": _type}) return None else: return list(entities)[0] if entities else None @@ -706,9 +704,9 @@ """Tell if the server of the profile has the required feature""" profile = self.getProfileName(profile_key) if not profile: - error (_('Trying find server feature for a non-existant profile')) + error(_('Trying find server feature for a non-existant profile')) return - assert(self.server_features.has_key(profile)) + assert profile in self.server_features return feature in self.server_features[profile] def getLastResource(self, contact, profile_key): @@ -738,7 +736,7 @@ if "presence" in self.entitiesCache[profile][entity]: entities_presence[entity] = self.entitiesCache[profile][entity]["presence"] - debug ("Memory getPresenceStatus (%s)", entities_presence) + debug("Memory getPresenceStatus (%s)", entities_presence) return entities_presence def setPresenceStatus(self, entity_jid, show, priority, statuses, profile_key): @@ -747,14 +745,14 @@ if not profile: error(_('Trying to add presence status to a non-existant profile')) return - entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(),{}) + entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(), {}) resource = jid.parse(entity_jid.full())[2] or '' if resource: entity_data["last_resource"] = resource if not "last_resource" in entity_data: entity_data["last_resource"] = '' - entity_data.setdefault("presence",{})[resource] = (show, priority, statuses) + entity_data.setdefault("presence", {})[resource] = (show, priority, statuses) def updateEntityData(self, entity_jid, key, value, profile_key): """Set a misc data for an entity @@ -768,9 +766,9 @@ raise exceptions.UnknownProfileError(_('Trying to get entity data for a non-existant profile')) if not profile in self.entitiesCache: raise exceptions.ProfileNotInCacheError - entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(),{}) + entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(), {}) entity_data[key] = value - if isinstance(value,basestring): + if isinstance(value, basestring): self.host.bridge.entityDataUpdated(entity_jid.userhost(), key, value, profile) def getEntityData(self, entity_jid, keys_list, profile_key): @@ -810,21 +808,19 @@ except KeyError: pass - - def addWaitingSub(self, _type, entity_jid, profile_key): """Called when a subcription request is received""" profile = self.getProfileName(profile_key) - assert(profile) - if not self.subscriptions.has_key(profile): + assert profile + if profile not in self.subscriptions: self.subscriptions[profile] = {} self.subscriptions[profile][entity_jid] = _type def delWaitingSub(self, entity_jid, profile_key): """Called when a subcription request is finished""" profile = self.getProfileName(profile_key) - assert(profile) - if self.subscriptions.has_key(profile) and self.subscriptions[profile].has_key(entity_jid): + assert profile + if profile in self.subscriptions and entity_jid in self.subscriptions[profile]: del self.subscriptions[profile][entity_jid] def getWaitingSub(self, profile_key): @@ -833,7 +829,7 @@ if not profile: error(_('Asking waiting subscriptions for a non-existant profile')) return {} - if not self.subscriptions.has_key(profile): + if profile not in self.subscriptions: return {} return self.subscriptions[profile] diff -r 65821b3fa7ab -r e5a875a3311b src/memory/persistent.py --- a/src/memory/persistent.py Fri Jan 18 17:55:35 2013 +0100 +++ b/src/memory/persistent.py Fri Jan 18 17:55:35 2013 +0100 @@ -21,11 +21,13 @@ from logging import debug, info, warning, error + class MemoryNotInitializedError(Exception): pass + class PersistentDict(object): - """A dictionary which save persistently each value assigned + r"""A dictionary which save persistently each value assigned /!\ be careful, each assignment means a database write /!\ Memory must be initialised before loading/setting value with instances of this class""" storage = None @@ -81,9 +83,6 @@ def __nonzero__(self): return self._cache.__len__() - def __unicode__(self): - return self._cache.__unicode__() - def __contains__(self, key): return self._cache.__contains__(key) @@ -146,4 +145,3 @@ if not self.profile: return self.storage.setGenPrivateBinary(self.namespace, name, self._cache[name]) return self.storage.setIndPrivateBinary(self.namespace, name, self._cache[name], self.profile) - diff -r 65821b3fa7ab -r e5a875a3311b src/memory/sqlite.py --- a/src/memory/sqlite.py Fri Jan 18 17:55:35 2013 +0100 +++ b/src/memory/sqlite.py Fri Jan 18 17:55:35 2013 +0100 @@ -19,50 +19,49 @@ along with this program. If not, see . """ - from logging import debug, info, warning, error from twisted.enterprise import adbapi from twisted.internet import defer +from time import time import os.path -import time import cPickle as pickle + class SqliteStorage(object): """This class manage storage with Sqlite database""" - def __init__(self, db_filename): """Connect to the given database @param db_filename: full path to the Sqlite database""" - self.initialized = defer.Deferred() #triggered when memory is fully initialised and ready - init_defers = [] #list of deferred we have to wait to before initialisation is complete - self.profiles={} #we keep cache for the profiles (key: profile name, value: profile id) + self.initialized = defer.Deferred() # triggered when memory is fully initialised and ready + init_defers = [] # list of deferred we have to wait to before initialisation is complete + self.profiles = {} # we keep cache for the profiles (key: profile name, value: profile id) info(_("Connecting database")) - new_base = not os.path.exists(db_filename) #do we have to create the database ? + new_base = not os.path.exists(db_filename) # do we have to create the database ? self.dbpool = adbapi.ConnectionPool("sqlite3", db_filename, check_same_thread=False) init_defers.append(self.dbpool.runOperation("PRAGMA foreign_keys = ON").addErrback(lambda x: error(_("Can't activate foreign keys")))) if new_base: info(_("The database is new, creating the tables")) database_creation = [ - "CREATE TABLE profiles (id INTEGER PRIMARY KEY ASC, name TEXT, UNIQUE (name))", - "CREATE TABLE message_types (type TEXT PRIMARY KEY)", - "INSERT INTO message_types VALUES ('chat')", - "INSERT INTO message_types VALUES ('error')", - "INSERT INTO message_types VALUES ('groupchat')", - "INSERT INTO message_types VALUES ('headline')", - "INSERT INTO message_types VALUES ('normal')", - "CREATE TABLE history (id INTEGER PRIMARY KEY ASC, profile_id INTEGER, source TEXT, dest TEXT, source_res TEXT, dest_res TEXT, timestamp DATETIME, message TEXT, type TEXT, FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE, FOREIGN KEY(type) REFERENCES message_types(type))", - "CREATE TABLE param_gen (category TEXT, name TEXT, value TEXT, PRIMARY KEY (category,name))", - "CREATE TABLE param_ind (category TEXT, name TEXT, profile_id INTEGER, value TEXT, PRIMARY KEY (category,name,profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE)", - "CREATE TABLE private_gen (namespace TEXT, key TEXT, value TEXT, PRIMARY KEY (namespace, key))", - "CREATE TABLE private_ind (namespace TEXT, key TEXT, profile_id INTEGER, value TEXT, PRIMARY KEY (namespace, key, profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE)", - "CREATE TABLE private_gen_bin (namespace TEXT, key TEXT, value BLOB, PRIMARY KEY (namespace, key))", - "CREATE TABLE private_ind_bin (namespace TEXT, key TEXT, profile_id INTEGER, value BLOB, PRIMARY KEY (namespace, key, profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE)", + "CREATE TABLE profiles (id INTEGER PRIMARY KEY ASC, name TEXT, UNIQUE (name))", + "CREATE TABLE message_types (type TEXT PRIMARY KEY)", + "INSERT INTO message_types VALUES ('chat')", + "INSERT INTO message_types VALUES ('error')", + "INSERT INTO message_types VALUES ('groupchat')", + "INSERT INTO message_types VALUES ('headline')", + "INSERT INTO message_types VALUES ('normal')", + "CREATE TABLE history (id INTEGER PRIMARY KEY ASC, profile_id INTEGER, source TEXT, dest TEXT, source_res TEXT, dest_res TEXT, timestamp DATETIME, message TEXT, type TEXT, FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE, FOREIGN KEY(type) REFERENCES message_types(type))", + "CREATE TABLE param_gen (category TEXT, name TEXT, value TEXT, PRIMARY KEY (category,name))", + "CREATE TABLE param_ind (category TEXT, name TEXT, profile_id INTEGER, value TEXT, PRIMARY KEY (category,name,profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE)", + "CREATE TABLE private_gen (namespace TEXT, key TEXT, value TEXT, PRIMARY KEY (namespace, key))", + "CREATE TABLE private_ind (namespace TEXT, key TEXT, profile_id INTEGER, value TEXT, PRIMARY KEY (namespace, key, profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE)", + "CREATE TABLE private_gen_bin (namespace TEXT, key TEXT, value BLOB, PRIMARY KEY (namespace, key))", + "CREATE TABLE private_ind_bin (namespace TEXT, key TEXT, profile_id INTEGER, value BLOB, PRIMARY KEY (namespace, key, profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE)", ] for op in database_creation: d = self.dbpool.runOperation(op) - d.addErrback(lambda x: error(_("Error while creating tables in database [QUERY: %s]") % op )) + d.addErrback(lambda x: error(_("Error while creating tables in database [QUERY: %s]") % op)) init_defers.append(d) def fillProfileCache(ignore): @@ -76,8 +75,8 @@ """Fill the profiles cache @param profiles_result: result of the sql profiles query""" for profile in profiles_result: - name, id = profile - self.profiles[name] = id + name, id_ = profile + self.profiles[name] = id_ def getProfilesList(self): """"Return list of all registered profiles""" @@ -86,20 +85,21 @@ def hasProfile(self, profile_name): """return True if profile_name exists @param profile_name: name of the profile to check""" - return self.profiles.has_key(profile_name) + return profile_name in self.profiles def createProfile(self, name): """Create a new profile @param name: name of the profile @return: deferred triggered once profile is actually created""" + def getProfileId(ignore): - return self.dbpool.runQuery("SELECT (id) FROM profiles WHERE name = ?", (name,)) + return self.dbpool.runQuery("SELECT (id) FROM profiles WHERE name = ?", (name, )) def profile_created(profile_id): _id = profile_id[0][0] - self.profiles[name] = _id #we synchronise the cache + self.profiles[name] = _id # we synchronise the cache - d = self.dbpool.runQuery("INSERT INTO profiles(name) VALUES (?)", (name,)) + d = self.dbpool.runQuery("INSERT INTO profiles(name) VALUES (?)", (name, )) d.addCallback(getProfileId) d.addCallback(profile_created) return d @@ -111,20 +111,21 @@ def deletionError(failure): error(_("Can't delete profile [%s]") % name) return failure + del self.profiles[name] - d = self.dbpool.runQuery("DELETE FROM profiles WHERE name = ?", (name,)) + d = self.dbpool.runQuery("DELETE FROM profiles WHERE name = ?", (name, )) d.addCallback(lambda ignore: info(_("Profile [%s] deleted") % name)) return d - #Params def loadGenParams(self, params_gen): """Load general parameters @param params_gen: dictionary to fill @return: deferred""" + def fillParams(result): for param in result: - category,name,value = param + category, name, value = param params_gen[(category, name)] = value debug(_("loading general parameters from database")) return self.dbpool.runQuery("SELECT category,name,value FROM param_gen").addCallback(fillParams) @@ -134,12 +135,13 @@ @param params_ind: dictionary to fill @param profile: a profile which *must* exist @return: deferred""" + def fillParams(result): for param in result: - category,name,value = param + category, name, value = param params_ind[(category, name)] = value debug(_("loading individual parameters from database")) - d = self.dbpool.runQuery("SELECT category,name,value FROM param_ind WHERE profile_id=?", (self.profiles[profile],)) + d = self.dbpool.runQuery("SELECT category,name,value FROM param_ind WHERE profile_id=?", (self.profiles[profile], )) d.addCallback(fillParams) return d @@ -149,7 +151,7 @@ @param name: name of the parameter @param profile: %(doc_profile)s @return: deferred""" - d = self.dbpool.runQuery("SELECT value FROM param_ind WHERE category=? AND name=? AND profile_id=?", (category,name,self.profiles[profile])) + d = self.dbpool.runQuery("SELECT value FROM param_ind WHERE category=? AND name=? AND profile_id=?", (category, name, self.profiles[profile])) d.addCallback(self.__getFirstResult) return d @@ -160,7 +162,7 @@ @param value: value to set @return: deferred""" d = self.dbpool.runQuery("REPLACE INTO param_gen(category,name,value) VALUES (?,?,?)", (category, name, value)) - d.addErrback(lambda ignore: error(_("Can't set general parameter (%(category)s/%(name)s) in database" % {"category":category, "name":name}))) + d.addErrback(lambda ignore: error(_("Can't set general parameter (%(category)s/%(name)s) in database" % {"category": category, "name": name}))) return d def setIndParam(self, category, name, value, profile): @@ -171,7 +173,7 @@ @param profile: a profile which *must* exist @return: deferred""" d = self.dbpool.runQuery("REPLACE INTO param_ind(category,name,profile_id,value) VALUES (?,?,?,?)", (category, name, self.profiles[profile], value)) - d.addErrback(lambda ignore: error(_("Can't set individual parameter (%(category)s/%(name)s) for [%(profile)s] in database" % {"category":category, "name":name, "profile":profile}))) + d.addErrback(lambda ignore: error(_("Can't set individual parameter (%(category)s/%(name)s) for [%(profile)s] in database" % {"category": category, "name": name, "profile": profile}))) return d #History @@ -185,10 +187,10 @@ """ assert(profile) d = self.dbpool.runQuery("INSERT INTO history(source, source_res, dest, dest_res, timestamp, message, type, profile_id) VALUES (?,?,?,?,?,?,?,?)", - (from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or time.time(), - message, _type, self.profiles[profile])) + (from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or time(), + message, _type, self.profiles[profile])) d.addErrback(lambda ignore: error(_("Can't save following message in history: from [%(from_jid)s] to [%(to_jid)s] ==> [%(message)s]" % - {"from_jid":from_jid.full(), "to_jid":to_jid.full(), "message":message}))) + {"from_jid": from_jid.full(), "to_jid": to_jid.full(), "message": message}))) return d def getHistory(self, from_jid, to_jid, limit=0, between=True, profile=None): @@ -198,35 +200,35 @@ @param size: maximum number of messages to get, or 0 for unlimited """ assert(profile) + def sqliteToDict(query_result): query_result.reverse() result = [] for row in query_result: - timestamp, source, source_res, dest, dest_res, message, _type= row + timestamp, source, source_res, dest, dest_res, message, _type = row result.append((timestamp, "%s/%s" % (source, source_res) if source_res else source, "%s/%s" % (dest, dest_res) if dest_res else dest, message, _type)) return result - query_parts = ["SELECT timestamp, source, source_res, dest, dest_res, message, type FROM history WHERE profile_id=? AND"] values = [self.profiles[profile]] - def test_jid(_type,_jid): + def test_jid(_type, _jid): values.append(_jid.userhost()) if _jid.resource: values.append(_jid.resource) return '(%s=? AND %s_res=?)' % (_type, _type) - return '%s=?' % (_type,) + return '%s=?' % (_type, ) if between: query_parts.append("(%s OR %s) AND (%s or %s)" % (test_jid('source', from_jid), - test_jid('source', to_jid), - test_jid('dest', to_jid), - test_jid('dest', from_jid))) + test_jid('source', to_jid), + test_jid('dest', to_jid), + test_jid('dest', from_jid))) else: - query_parts.append("%s AND %s") % (test_jid('source', from_jid), - test_jid('dest', to_jid)) + query_parts.append("%s AND %s" % (test_jid('source', from_jid), + test_jid('dest', to_jid))) query_parts.append("ORDER BY timestamp DESC") @@ -243,12 +245,13 @@ @param private_gen: dictionary to fill @param namespace: namespace of the values @return: deferred""" + def fillPrivates(result): for private in result: - key,value = private + key, value = private private_gen[key] = value - debug(_("loading general private values [namespace: %s] from database") % (namespace,)) - d = self.dbpool.runQuery("SELECT key,value FROM private_gen WHERE namespace=?", (namespace,)).addCallback(fillPrivates) + debug(_("loading general private values [namespace: %s] from database") % (namespace, )) + d = self.dbpool.runQuery("SELECT key,value FROM private_gen WHERE namespace=?", (namespace, )).addCallback(fillPrivates) return d.addErrback(lambda x: debug(_("No data present in database for namespace %s") % namespace)) def loadIndPrivates(self, private_ind, namespace, profile): @@ -257,11 +260,12 @@ @param namespace: namespace of the values @param profile: a profile which *must* exist @return: deferred""" + def fillPrivates(result): for private in result: - key,value = private + key, value = private private_ind[key] = value - debug(_("loading individual private values [namespace: %s] from database") % (namespace,)) + debug(_("loading individual private values [namespace: %s] from database") % (namespace, )) d = self.dbpool.runQuery("SELECT key,value FROM private_ind WHERE namespace=? AND profile_id=?", (namespace, self.profiles[profile])) d.addCallback(fillPrivates) return d.addErrback(lambda x: debug(_("No data present in database for namespace %s") % namespace)) @@ -272,9 +276,9 @@ @param key: key of the private value @param value: value to set @return: deferred""" - d = self.dbpool.runQuery("REPLACE INTO private_gen(namespace,key,value) VALUES (?,?,?)", (namespace,key,value)) + d = self.dbpool.runQuery("REPLACE INTO private_gen(namespace,key,value) VALUES (?,?,?)", (namespace, key, value)) d.addErrback(lambda ignore: error(_("Can't set general private value (%(key)s) [namespace:%(namespace)s] in database" % - {"namespace":namespace, "key":key}))) + {"namespace": namespace, "key": key}))) return d def setIndPrivate(self, namespace, key, value, profile): @@ -286,7 +290,7 @@ @return: deferred""" d = self.dbpool.runQuery("REPLACE INTO private_ind(namespace,key,profile_id,value) VALUES (?,?,?,?)", (namespace, key, self.profiles[profile], value)) d.addErrback(lambda ignore: error(_("Can't set individual private value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" % - {"namespace":namespace, "key":key, "profile":profile}))) + {"namespace": namespace, "key": key, "profile": profile}))) return d def delGenPrivate(self, namespace, key): @@ -294,9 +298,9 @@ @param category: category of the privateeter @param key: key of the private value @return: deferred""" - d = self.dbpool.runQuery("DELETE FROM private_gen WHERE namespace=? AND key=?", (namespace,key)) + d = self.dbpool.runQuery("DELETE FROM private_gen WHERE namespace=? AND key=?", (namespace, key)) d.addErrback(lambda ignore: error(_("Can't delete general private value (%(key)s) [namespace:%(namespace)s] in database" % - {"namespace":namespace, "key":key}))) + {"namespace": namespace, "key": key}))) return d def delIndPrivate(self, namespace, key, profile): @@ -307,21 +311,21 @@ @return: deferred""" d = self.dbpool.runQuery("DELETE FROM private_ind WHERE namespace=? AND key=? AND profile=?)", (namespace, key, self.profiles[profile])) d.addErrback(lambda ignore: error(_("Can't delete individual private value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" % - {"namespace":namespace, "key":key, "profile":profile}))) + {"namespace": namespace, "key": key, "profile": profile}))) return d - def loadGenPrivatesBinary(self, private_gen, namespace): """Load general private binary values @param private_gen: dictionary to fill @param namespace: namespace of the values @return: deferred""" + def fillPrivates(result): for private in result: - key,value = private + key, value = private private_gen[key] = pickle.loads(str(value)) - debug(_("loading general private binary values [namespace: %s] from database") % (namespace,)) - d = self.dbpool.runQuery("SELECT key,value FROM private_gen_bin WHERE namespace=?", (namespace,)).addCallback(fillPrivates) + debug(_("loading general private binary values [namespace: %s] from database") % (namespace, )) + d = self.dbpool.runQuery("SELECT key,value FROM private_gen_bin WHERE namespace=?", (namespace, )).addCallback(fillPrivates) return d.addErrback(lambda x: debug(_("No binary data present in database for namespace %s") % namespace)) def loadIndPrivatesBinary(self, private_ind, namespace, profile): @@ -330,11 +334,12 @@ @param namespace: namespace of the values @param profile: a profile which *must* exist @return: deferred""" + def fillPrivates(result): for private in result: - key,value = private + key, value = private private_ind[key] = pickle.loads(str(value)) - debug(_("loading individual private binary values [namespace: %s] from database") % (namespace,)) + debug(_("loading individual private binary values [namespace: %s] from database") % (namespace, )) d = self.dbpool.runQuery("SELECT key,value FROM private_ind_bin WHERE namespace=? AND profile_id=?", (namespace, self.profiles[profile])) d.addCallback(fillPrivates) return d.addErrback(lambda x: debug(_("No binary data present in database for namespace %s") % namespace)) @@ -345,9 +350,9 @@ @param key: key of the private value @param value: value to set @return: deferred""" - d = self.dbpool.runQuery("REPLACE INTO private_gen_bin(namespace,key,value) VALUES (?,?,?)", (namespace,key,pickle.dumps(value,0))) + d = self.dbpool.runQuery("REPLACE INTO private_gen_bin(namespace,key,value) VALUES (?,?,?)", (namespace, key, pickle.dumps(value, 0))) d.addErrback(lambda ignore: error(_("Can't set general private binary value (%(key)s) [namespace:%(namespace)s] in database" % - {"namespace":namespace, "key":key}))) + {"namespace": namespace, "key": key}))) return d def setIndPrivateBinary(self, namespace, key, value, profile): @@ -357,9 +362,9 @@ @param value: value to set @param profile: a profile which *must* exist @return: deferred""" - d = self.dbpool.runQuery("REPLACE INTO private_ind_bin(namespace,key,profile_id,value) VALUES (?,?,?,?)", (namespace, key, self.profiles[profile], pickle.dumps(value,0))) + d = self.dbpool.runQuery("REPLACE INTO private_ind_bin(namespace,key,profile_id,value) VALUES (?,?,?,?)", (namespace, key, self.profiles[profile], pickle.dumps(value, 0))) d.addErrback(lambda ignore: error(_("Can't set individual binary private value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" % - {"namespace":namespace, "key":key, "profile":profile}))) + {"namespace": namespace, "key": key, "profile": profile}))) return d def delGenPrivateBinary(self, namespace, key): @@ -367,9 +372,9 @@ @param category: category of the privateeter @param key: key of the private value @return: deferred""" - d = self.dbpool.runQuery("DELETE FROM private_gen_bin WHERE namespace=? AND key=?", (namespace,key)) + d = self.dbpool.runQuery("DELETE FROM private_gen_bin WHERE namespace=? AND key=?", (namespace, key)) d.addErrback(lambda ignore: error(_("Can't delete general private binary value (%(key)s) [namespace:%(namespace)s] in database" % - {"namespace":namespace, "key":key}))) + {"namespace": namespace, "key": key}))) return d def delIndPrivateBinary(self, namespace, key, profile): @@ -380,7 +385,7 @@ @return: deferred""" d = self.dbpool.runQuery("DELETE FROM private_ind_bin WHERE namespace=? AND key=? AND profile=?)", (namespace, key, self.profiles[profile])) d.addErrback(lambda ignore: error(_("Can't delete individual private binary value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" % - {"namespace":namespace, "key":key, "profile":profile}))) + {"namespace": namespace, "key": key, "profile": profile}))) return d ##Helper methods##