coding standards

This commit is contained in:
Yann Leboulanger 2009-09-29 16:31:36 +02:00
parent 4cfd56a69b
commit da398aa418

View file

@ -160,7 +160,8 @@ class Logger:
def get_jids_already_in_db(self): def get_jids_already_in_db(self):
try: try:
self.cur.execute('SELECT jid FROM jids') self.cur.execute('SELECT jid FROM jids')
rows = self.cur.fetchall() # list of tupples: [(u'aaa@bbb',), (u'cc@dd',)] # list of tupples: [(u'aaa@bbb',), (u'cc@dd',)]
rows = self.cur.fetchall()
except sqlite.DatabaseError: except sqlite.DatabaseError:
raise exceptions.DatabaseMalformed raise exceptions.DatabaseMalformed
self.jids_already_in = [] self.jids_already_in = []
@ -361,7 +362,8 @@ class Logger:
return 'both' return 'both'
def commit_to_db(self, values, write_unread=False): def commit_to_db(self, values, write_unread=False):
sql = 'INSERT INTO logs (jid_id, contact_name, time, kind, show, message, subject) VALUES (?, ?, ?, ?, ?, ?, ?)' sql = '''INSERT INTO logs (jid_id, contact_name, time, kind, show,
message, subject) VALUES (?, ?, ?, ?, ?, ?, ?)'''
try: try:
self.cur.execute(sql, values) self.cur.execute(sql, values)
except sqlite.DatabaseError: except sqlite.DatabaseError:
@ -432,8 +434,7 @@ class Logger:
all_messages.append(results[0] + (shown,)) all_messages.append(results[0] + (shown,))
return all_messages return all_messages
def write(self, kind, jid, message = None, show = None, tim = None, def write(self, kind, jid, message=None, show=None, tim=None, subject=None):
subject = None):
'''write a row (status, gcstatus, message etc) to logs database '''write a row (status, gcstatus, message etc) to logs database
kind can be status, gcstatus, gc_msg, (we only recv for those 3), kind can be status, gcstatus, gc_msg, (we only recv for those 3),
single_msg_recv, chat_msg_recv, chat_msg_sent, single_msg_sent single_msg_recv, chat_msg_recv, chat_msg_sent, single_msg_sent
@ -759,10 +760,12 @@ class Logger:
return answer return answer
# A longer note here: # A longer note here:
# The database contains a blob field. Pysqlite seems to need special care for such fields. # The database contains a blob field. Pysqlite seems to need special care for
# such fields.
# When storing, we need to convert string into buffer object (1). # When storing, we need to convert string into buffer object (1).
# When retrieving, we need to convert it back to a string to decompress it. (2) # When retrieving, we need to convert it back to a string to decompress it.
# GzipFile needs a file-like object, StringIO emulates file for plain strings. # (2)
# GzipFile needs a file-like object, StringIO emulates file for plain strings
def iter_caps_data(self): def iter_caps_data(self):
''' Iterate over caps cache data stored in the database. ''' Iterate over caps cache data stored in the database.
The iterator values are pairs of (node, ver, ext, identities, features): The iterator values are pairs of (node, ver, ext, identities, features):
@ -787,7 +790,8 @@ class Logger:
# ..., 'FEAT', feature1, feature2, ...).join(' ')) # ..., 'FEAT', feature1, feature2, ...).join(' '))
# NOTE: if there's a need to do more gzip, put that to a function # NOTE: if there's a need to do more gzip, put that to a function
try: try:
data = GzipFile(fileobj=StringIO(str(data))).read().decode('utf-8').split('\0') data = GzipFile(fileobj=StringIO(str(data))).read().decode(
'utf-8').split('\0')
except IOError: except IOError:
# This data is corrupted. It probably contains non-ascii chars # This data is corrupted. It probably contains non-ascii chars
to_be_removed.append((hash_method, hash_)) to_be_removed.append((hash_method, hash_))
@ -811,7 +815,8 @@ class Logger:
# yield the row # yield the row
yield hash_method, hash_, identities, features yield hash_method, hash_, identities, features
for hash_method, hash_ in to_be_removed: for hash_method, hash_ in to_be_removed:
sql = 'DELETE FROM caps_cache WHERE hash_method = "%s" AND hash = "%s"' % (hash_method, hash_) sql = '''DELETE FROM caps_cache WHERE hash_method = "%s" AND
hash = "%s"''' % (hash_method, hash_)
self.simple_commit(sql) self.simple_commit(sql)
def add_caps_entry(self, hash_method, hash_, identities, features): def add_caps_entry(self, hash_method, hash_, identities, features):
@ -875,8 +880,12 @@ class Logger:
jid_id = self.get_jid_id(jid) jid_id = self.get_jid_id(jid)
except exceptions.PysqliteOperationalError, e: except exceptions.PysqliteOperationalError, e:
raise exceptions.PysqliteOperationalError(str(e)) raise exceptions.PysqliteOperationalError(str(e))
self.cur.execute('DELETE FROM roster_group WHERE account_jid_id=? AND jid_id=?', (account_jid_id, jid_id)) self.cur.execute(
self.cur.execute('DELETE FROM roster_entry WHERE account_jid_id=? AND jid_id=?', (account_jid_id, jid_id)) 'DELETE FROM roster_group WHERE account_jid_id=? AND jid_id=?',
(account_jid_id, jid_id))
self.cur.execute(
'DELETE FROM roster_entry WHERE account_jid_id=? AND jid_id=?',
(account_jid_id, jid_id))
self.con.commit() self.con.commit()
def add_or_update_contact(self, account_jid, jid, name, sub, ask, groups): def add_or_update_contact(self, account_jid, jid, name, sub, ask, groups):
@ -893,7 +902,9 @@ class Logger:
# Update groups information # Update groups information
# First we delete all previous groups information # First we delete all previous groups information
self.cur.execute('DELETE FROM roster_group WHERE account_jid_id=? AND jid_id=?', (account_jid_id, jid_id)) self.cur.execute(
'DELETE FROM roster_group WHERE account_jid_id=? AND jid_id=?',
(account_jid_id, jid_id))
# Then we add all new groups information # Then we add all new groups information
for group in groups: for group in groups:
self.cur.execute('INSERT INTO roster_group VALUES(?, ?, ?)', self.cur.execute('INSERT INTO roster_group VALUES(?, ?, ?)',
@ -914,14 +925,19 @@ class Logger:
account_jid_id = self.get_jid_id(account_jid) account_jid_id = self.get_jid_id(account_jid)
# First we fill data with roster_entry informations # First we fill data with roster_entry informations
self.cur.execute('SELECT j.jid, re.jid_id, re.name, re.subscription, re.ask FROM roster_entry re, jids j WHERE re.account_jid_id=? AND j.jid_id=re.jid_id', (account_jid_id,)) self.cur.execute('''
SELECT j.jid, re.jid_id, re.name, re.subscription, re.ask
FROM roster_entry re, jids j
WHERE re.account_jid_id=? AND j.jid_id=re.jid_id''', (account_jid_id,))
for jid, jid_id, name, subscription, ask in self.cur: for jid, jid_id, name, subscription, ask in self.cur:
data[jid] = {} data[jid] = {}
if name: if name:
data[jid]['name'] = name data[jid]['name'] = name
else: else:
data[jid]['name'] = None data[jid]['name'] = None
data[jid]['subscription'] = self.convert_db_api_values_to_human_subscription_values(subscription) data[jid]['subscription'] = \
self.convert_db_api_values_to_human_subscription_values(
subscription)
data[jid]['groups'] = [] data[jid]['groups'] = []
data[jid]['resources'] = {} data[jid]['resources'] = {}
if ask: if ask:
@ -932,7 +948,10 @@ class Logger:
# Then we add group for roster entries # Then we add group for roster entries
for jid in data: for jid in data:
self.cur.execute('SELECT group_name FROM roster_group WHERE account_jid_id=? AND jid_id=?', (account_jid_id, data[jid]['id'])) self.cur.execute('''
SELECT group_name FROM roster_group
WHERE account_jid_id=? AND jid_id=?''',
(account_jid_id, data[jid]['id']))
for (group_name,) in self.cur: for (group_name,) in self.cur:
data[jid]['groups'].append(group_name) data[jid]['groups'].append(group_name)
del data[jid]['id'] del data[jid]['id']