def show_number(self, ggnumber): if FileManager.has_history( ggnumber ): sql = SQL() query = 'select * from users where ggnumber=:ggnumber;' ret = sql.execute(query, {'ggnumber' : ggnumber }) row = ret.fetchone() user = User(row, ggnumber) view = UserView( user ) self.clear() view() else: self.refresh() ROText(1, 0, u'Nie znaleziono histori dla podanego numeru.', u'Błąd' ).run()
def show_number(self, ggnumber): if FileManager.has_history(ggnumber): sql = SQL() query = 'select * from users where ggnumber=:ggnumber;' ret = sql.execute(query, {'ggnumber': ggnumber}) row = ret.fetchone() user = User(row, ggnumber) view = UserView(user) self.clear() view() else: self.refresh() ROText(1, 0, u'Nie znaleziono histori dla podanego numeru.', u'Błąd').run()
def cache_history( number ): """cache_history( number ) -> None Update cache for the specific user. number - GG number of the person to reloads cache """ def create_time(text): if EKG_CONFIG.get('log_timestamp') != None: time_struct = time.strptime(text, EKG_CONFIG.get('log_timestamp')) return time.mktime(time_struct) else: return int(text) main_sql = SQL() ret = main_sql.execute( 'select seek from cache_info where ggnumber=:ggnumber', {'ggnumber':number} ) row = ret.fetchone() if row != None: offset = row[0] else: offset = 0 sql = SQL_MSG(number) if not FileManager.has_history(number): return file = open(FileManager.history_of(number), "rb") file.seek(offset) reader = GaduReader(file, encoding=FileManager.history_encoding) loop = -1 for row in reader: try: #jeśli jest już taki rekord, to nie dodawaj nowego query = 'select * from msg where time=:time' tab = {} if row[0] in ['chatsend', 'msgsend']: tab['msg'] = row[4] tab['time'] = create_time(row[3]) else: tab['msg'] = row[5] tab['time'] = create_time(row[4]) ret = sql.execute(query,tab) rows = ret.fetchall() if len(rows) > 0: continue #-------------------------------- loop += 1 if row[0] == 'status': tab = { 'type' : row[0], #'ggnumber' : int( strip_gg( row[1] ) ), 'nick' : row[2], 'ip' : row[3], 'time' : create_time( row[4] ), 'status' : row[5], } if len( row ) > 6: tab['descr'] = row[6] else: tab['descr'] = None query = '''insert into msg( type, nick, ip, time, status, descr ) values ( :type, :nick, :ip, datetime(:time, 'unixepoch'), :status, :descr);''' elif row[0] in ['chatrecvign', 'msgrecvign', 'chatrecv', 'msgrecv', 'chatrcv']: tab = { 'type' : row[0], #'ggnumber' : int( strip_gg( row[1] ) ), 'nick' : row[2], 'time' : create_time( row[3] ), 'send_time' : create_time( row[4] ), 'msg' : row[5], } query = '''insert into msg( type, nick, time, send_time, msg ) values ( :type, :nick, datetime(:time, 'unixepoch'), :send_time, :msg);''' elif row[0] in ['chatsend', 'msgsend']: tab = { 'type' : row[0], #'ggnumber' : int( strip_gg( row[1] ) ), 'nick' : row[2], 'time' : create_time( row[3] ), 'msg' : row[4], } query = '''insert into msg( type, nick, time, msg ) values ( :type, :nick, datetime(:time, 'unixepoch'), :msg);''' else: raise ValueError('Unknown row type [%s] in %s when reading line %d from %s.' % (row[0], str(row), loop, str(file))) sql.execute( query, tab ) except IndexError: raise ValueError('Row %s has less fields (%d) than expected when reading ' 'line %d from %s, counting from byte %d.' % (str(row), len(row), loop, str(file), offset)) except UnicodeEncodeError: print 'Unicode Encoding Error in %s when reading line %d from %s, couting from byte %d.' % (str(row), loop, str(file), offset) #raise ValueError('Unicode Encoding Error in %s when reading line %d from %s, ' # 'couting from byte %d.' % (str(row), loop, str(file), offset)) except ValueError: print "Error in line %d: '%s'. Probalby wrong timestamp." % (loop, str(row) ) from datetime import datetime tab = { 'ggnumber' : number, 'seek' : file.tell(), } try: query = 'insert into cache_info values ( :ggnumber, :seek );' main_sql.execute( query, tab ) except sqlite3.IntegrityError: query = 'update cache_info set seek=:seek where ggnumber=:ggnumber;' main_sql.execute( query, tab ) main_sql.commit() sql.commit()