diff src/memory/sqlite.py @ 592:e5a875a3311b

Fix pep8 support in src/memory.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:35 +0100
parents beaf6bec2fcd
children 70bae685d05c
line wrap: on
line diff
--- 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 <http://www.gnu.org/licenses/>.
 """
 
-
 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##