coding standards

This commit is contained in:
Yann Leboulanger 2009-09-29 16:31:36 +02:00
parent 4cfd56a69b
commit da398aa418
1 changed files with 43 additions and 24 deletions

View File

@ -130,8 +130,8 @@ class Logger:
# if locked, wait up to 20 sec to unlock
# before raise (hopefully should be enough)
self.con = sqlite.connect(LOG_DB_FILE, timeout = 20.0,
isolation_level = 'IMMEDIATE')
self.con = sqlite.connect(LOG_DB_FILE, timeout=20.0,
isolation_level='IMMEDIATE')
os.chdir(back)
self.cur = self.con.cursor()
self.set_synchronous(False)
@ -160,7 +160,8 @@ class Logger:
def get_jids_already_in_db(self):
try:
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:
raise exceptions.DatabaseMalformed
self.jids_already_in = []
@ -200,7 +201,7 @@ class Logger:
else:
return True
def get_jid_id(self, jid, typestr = None):
def get_jid_id(self, jid, typestr=None):
'''jids table has jid and jid_id
logs table has log_id, jid_id, contact_name, time, kind, show, message
so to ask logs we need jid_id that matches our jid in jids table
@ -360,8 +361,9 @@ class Logger:
if sub == constants.SUBSCRIPTION_BOTH:
return 'both'
def commit_to_db(self, values, write_unread = False):
sql = 'INSERT INTO logs (jid_id, contact_name, time, kind, show, message, subject) VALUES (?, ?, ?, ?, ?, ?, ?)'
def commit_to_db(self, values, write_unread=False):
sql = '''INSERT INTO logs (jid_id, contact_name, time, kind, show,
message, subject) VALUES (?, ?, ?, ?, ?, ?, ?)'''
try:
self.cur.execute(sql, values)
except sqlite.DatabaseError:
@ -432,8 +434,7 @@ class Logger:
all_messages.append(results[0] + (shown,))
return all_messages
def write(self, kind, jid, message = None, show = None, tim = None,
subject = None):
def write(self, kind, jid, message=None, show=None, tim=None, subject=None):
'''write a row (status, gcstatus, message etc) to logs database
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
@ -515,7 +516,7 @@ class Logger:
return self.commit_to_db(values, write_unread)
def get_last_conversation_lines(self, jid, restore_how_many_rows,
pending_how_many, timeout, account):
pending_how_many, timeout, account):
'''accepts how many rows to restore and when to time them out (in minutes)
(mark them as too old) and number of messages that are in queue
and are already logged but pending to be viewed,
@ -594,7 +595,7 @@ class Logger:
# Error trying to create a new jid_id. This means there is no log
return []
if False: #query.startswith('SELECT '): # it's SQL query (FIXME)
if False: # query.startswith('SELECT '): # it's SQL query (FIXME)
try:
self.cur.execute(query)
except sqlite.OperationalError, e:
@ -648,7 +649,7 @@ class Logger:
return days_with_logs
def get_last_date_that_has_logs(self, jid, account = None, is_room = False):
def get_last_date_that_has_logs(self, jid, account=None, is_room=False):
'''returns last time (in seconds since EPOCH) for which
we had logs (excluding statuses)'''
where_sql = ''
@ -759,10 +760,12 @@ class Logger:
return answer
# 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 retrieving, we need to convert it back to a string to decompress it. (2)
# GzipFile needs a file-like object, StringIO emulates file for plain strings.
# When retrieving, we need to convert it back to a string to decompress it.
# (2)
# GzipFile needs a file-like object, StringIO emulates file for plain strings
def iter_caps_data(self):
''' Iterate over caps cache data stored in the database.
The iterator values are pairs of (node, ver, ext, identities, features):
@ -787,12 +790,13 @@ class Logger:
# ..., 'FEAT', feature1, feature2, ...).join(' '))
# NOTE: if there's a need to do more gzip, put that to a function
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:
# This data is corrupted. It probably contains non-ascii chars
to_be_removed.append((hash_method, hash_))
continue
i=0
i = 0
identities = list()
features = list()
while i < (len(data) - 3) and data[i] != 'FEAT':
@ -811,11 +815,12 @@ class Logger:
# yield the row
yield hash_method, hash_, identities, features
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)
def add_caps_entry(self, hash_method, hash_, identities, features):
data=[]
data = []
for identity in identities:
# there is no FEAT category
if identity['category'] == 'FEAT':
@ -875,8 +880,12 @@ class Logger:
jid_id = self.get_jid_id(jid)
except exceptions.PysqliteOperationalError, 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('DELETE FROM roster_entry 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))
self.cur.execute(
'DELETE FROM roster_entry WHERE account_jid_id=? AND jid_id=?',
(account_jid_id, jid_id))
self.con.commit()
def add_or_update_contact(self, account_jid, jid, name, sub, ask, groups):
@ -893,7 +902,9 @@ class Logger:
# Update 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
for group in groups:
self.cur.execute('INSERT INTO roster_group VALUES(?, ?, ?)',
@ -914,14 +925,19 @@ class Logger:
account_jid_id = self.get_jid_id(account_jid)
# 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:
data[jid] = {}
if name:
data[jid]['name'] = name
else:
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]['resources'] = {}
if ask:
@ -932,7 +948,10 @@ class Logger:
# Then we add group for roster entries
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:
data[jid]['groups'].append(group_name)
del data[jid]['id']