Py3 compatibility update 3

- zip files correctly read, currently hardcoded encoding
- file encoding hardcoded generally
- various fixes
This commit is contained in:
Gabor Körber 2017-05-16 21:40:38 +02:00
parent 3bde3a7570
commit 50b15b218b
6 changed files with 30 additions and 15 deletions

View File

@ -29,7 +29,9 @@ if __name__ == '__main__':
datefmt='%Y-%m-%d %H:%M:%S') datefmt='%Y-%m-%d %H:%M:%S')
coll = LogSessionCollector(os.path.join(os.path.expanduser('~'), coll = LogSessionCollector(os.path.join(os.path.expanduser('~'),
'Documents', 'My Games', 'sc')) 'Documents', 'My Games', 'sc'))
logging.info('Collecting Sessions...')
coll.collect_unique() coll.collect_unique()
logging.info('collected %s sessions.' % (len(coll.sessions)))
#f = open('output.txt', 'w') #f = open('output.txt', 'w')
rex_combat = {} rex_combat = {}
rex_game = {} rex_game = {}
@ -50,6 +52,8 @@ if __name__ == '__main__':
if not isinstance(l, combat.UserEvent): if not isinstance(l, combat.UserEvent):
if not LOG_GOOD: if not LOG_GOOD:
print((l.values['log'])) print((l.values['log']))
else:
logging.debug('No combat log in %s' % logf.idstr)
if logf.game_log: if logf.game_log:
for l in logf.game_log.lines: for l in logf.game_log.lines:
if isinstance(l, dict): if isinstance(l, dict):
@ -63,6 +67,8 @@ if __name__ == '__main__':
rex_game[l.__class__.__name__] = rex_game.get(l.__class__.__name__, 0) + 1 rex_game[l.__class__.__name__] = rex_game.get(l.__class__.__name__, 0) + 1
if not LOG_GOOD: if not LOG_GOOD:
print((l.values['log'])) print((l.values['log']))
else:
logging.debug('No game log in %s' % logf.idstr)
if logf.chat_log: if logf.chat_log:
for l in logf.chat_log.lines: for l in logf.chat_log.lines:
if isinstance(l, dict): if isinstance(l, dict):
@ -76,6 +82,8 @@ if __name__ == '__main__':
rex_chat[l.__class__.__name__] = rex_chat.get(l.__class__.__name__, 0) + 1 rex_chat[l.__class__.__name__] = rex_chat.get(l.__class__.__name__, 0) + 1
if not LOG_GOOD: if not LOG_GOOD:
print((l.values['log'])) print((l.values['log']))
else:
logging.debug('No chat log in %s' % logf.idstr)
logf.clean(True) logf.clean(True)
# additional cleanup: # additional cleanup:
if logf.chat_log: if logf.chat_log:

View File

@ -4,10 +4,10 @@
Tool to analyze Logs in general. Tool to analyze Logs in general.
""" """
import os, sys, logging import os, sys, logging
from .logs.logfiles import LogFileResolver as LogFile from logs.logfiles import LogFileResolver as LogFile
from .logs import combat, game, chat from logs import combat, game, chat
from .logs.session import LogSessionCollector from logs.session import LogSessionCollector
from .logs.game import ClientInfo from logs.game import ClientInfo
# for windows its kinda this: # for windows its kinda this:
settings = {'root_path': os.path.join(os.path.expanduser('~'), settings = {'root_path': os.path.join(os.path.expanduser('~'),

View File

@ -16,8 +16,8 @@
""" """
#from win32com.shell import shell, shellcon #from win32com.shell import shell, shellcon
import os, sys, logging import os, sys, logging
from .logs.logfiles import LogFileResolver as LogFile from logs.logfiles import LogFileResolver as LogFile
from .logs import combat from logs import combat
# for windows its kinda this: # for windows its kinda this:
settings = {'root_path': os.path.join(os.path.expanduser('~'), settings = {'root_path': os.path.join(os.path.expanduser('~'),

View File

@ -22,7 +22,7 @@ class LogFile(LogStream):
def read(self, fname=None): def read(self, fname=None):
fname = fname or self.fname fname = fname or self.fname
try: try:
f = io.open(fname, 'r', ) f = io.open(fname, 'r', encoding="iso8859-1")
self.set_data(f.read()) self.set_data(f.read())
except Exception as e: except Exception as e:
logging.error("Error %s reading file %s " % (e, fname, )) logging.error("Error %s reading file %s " % (e, fname, ))

View File

@ -118,7 +118,7 @@ class LogStream(object):
#if isinstance(self._last_object, Stacktrace) and line.startswith('\t'): #if isinstance(self._last_object, Stacktrace) and line.startswith('\t'):
# logging.debug('Workaround: %s, worked: %s' % (line, self._last_object.append(line))) # logging.debug('Workaround: %s, worked: %s' % (line, self._last_object.append(line)))
# return # return
if self._last_object is not None: if self._last_object is not None and isinstance(self._last_object, Log):
self._last_object.unpack() self._last_object.unpack()
if self._last_object.append(line): if self._last_object.append(line):
return return
@ -127,6 +127,10 @@ class LogStream(object):
elif isinstance(line, dict): elif isinstance(line, dict):
# Unresolved Log. # Unresolved Log.
o = self.resolve(line) o = self.resolve(line)
# this is where the whole thing gets polluted with weird dicts.
# what exactly should resolve do!?
# by default it returns what its given, if unknown.
# #@TODO @XXX @CRITICAL
self._last_object = o self._last_object = o
else: else:
self._last_object = o self._last_object = o

View File

@ -1,7 +1,7 @@
""" """
Logging Session. Logging Session.
""" """
import zipfile, logging, os import zipfile, logging, os, io
from .logfiles import CombatLogFile, GameLogFile, ChatLogFile from .logfiles import CombatLogFile, GameLogFile, ChatLogFile
class LogSession(object): class LogSession(object):
@ -63,7 +63,8 @@ class LogFileSession(LogSession):
v = self._validate_files_exist() v = self._validate_files_exist()
if v > 0: if v > 0:
self.idstr = os.path.split(self.directory)[1].lower() self.idstr = os.path.split(self.directory)[1].lower()
except: except Exception as e:
logging.error("exception in logfilesession.validate %s" % e)
return False return False
return v return v
@ -71,6 +72,7 @@ class LogFileSession(LogSession):
''' parses the logfiles ''' ''' parses the logfiles '''
# perform simple validation. # perform simple validation.
if self._zip_source is None: if self._zip_source is None:
logging.error('_zip_source is None!')
self.validate(False) self.validate(False)
if self._zip_source: if self._zip_source:
self._unzip_logs(files) self._unzip_logs(files)
@ -110,22 +112,23 @@ class LogFileSession(LogSession):
if fn: if fn:
if fn == 'combat.log' and (not files or fn in files) and not 'combat.log' in self.files_parsed: if fn == 'combat.log' and (not files or fn in files) and not 'combat.log' in self.files_parsed:
self.combat_log = CombatLogFile(fn) self.combat_log = CombatLogFile(fn)
self.combat_log.set_data(z.read(filename)) self.combat_log.set_data(io.TextIOWrapper(io.BytesIO(z.read(filename)), encoding='iso8859-1').read())
self.combat_log.parse() self.combat_log.parse()
self.files_parsed.append('combat.log') self.files_parsed.append('combat.log')
elif fn == 'game.log' and (not files or fn in files) and not 'game.log' in self.files_parsed: elif fn == 'game.log' and (not files or fn in files) and not 'game.log' in self.files_parsed:
self.game_log = GameLogFile(fn) self.game_log = GameLogFile(fn)
self.game_log.set_data(z.read(filename)) self.game_log.set_data(io.TextIOWrapper(io.BytesIO(z.read(filename)), encoding='iso8859-1').read())
self.game_log.parse() self.game_log.parse()
self.files_parsed.append('game.log') self.files_parsed.append('game.log')
elif fn == 'chat.log' and (not files or fn in files) and not 'chat.log' in self.files_parsed: elif fn == 'chat.log' and (not files or fn in files) and not 'chat.log' in self.files_parsed:
self.chat_log = ChatLogFile(fn) self.chat_log = ChatLogFile(fn)
self.chat_log.set_data(z.read(filename)) self.chat_log.set_data(io.TextIOWrapper(io.BytesIO(z.read(filename)), encoding='iso8859-1').read())
self.chat_log.parse() self.chat_log.parse()
self.files_parsed.append('chat.log') self.files_parsed.append('chat.log')
except: except Exception as e:
self._error = True self._error = True
return logging.error('_unzip logs encountered error %s' % e)
raise
finally: finally:
z.close() z.close()