improving transition - config tool for oregyen :)

This commit is contained in:
Gabor Körber 2014-05-26 17:03:11 +02:00
parent 3ed80d40b2
commit d0ce5ef086
18 changed files with 1063 additions and 340 deletions

17
app.py Normal file
View File

@ -0,0 +1,17 @@
"""
Main application functions.
* backing up logs.
- from directory to directory
- compression as option
* log-sessions:
- contains one session of log
- has a source (directory, file)
- determines user
- parses logs
"""

69
backup.py Normal file
View File

@ -0,0 +1,69 @@
"""
Backup Directories, Handle Files...
"""
import os, logging, zipfile
def make_zipfile(output_filename, source_dir):
relroot = os.path.abspath(os.path.join(source_dir, os.pardir))
with zipfile.ZipFile(output_filename, "w", zipfile.ZIP_DEFLATED) as zip:
for root, dirs, files in os.walk(source_dir):
# add directory (needed for empty dirs)
zip.write(root, os.path.relpath(root, relroot))
for file in files:
filename = os.path.join(root, file)
if os.path.isfile(filename): # regular files only
arcname = os.path.join(os.path.relpath(root, relroot), file)
zip.write(filename, arcname)
def backup_log_directory(log_directory, backup_directory, compress=True,
ommit_level=2, verbose=False):
# @todo: raw copy
# ommit_level 0: overwrite.
# ommit_level 1: write if selected compression method not backuped yet
# ommit_level 2: write only if neither method contains directory.
nothing_found = True
# get all directory names in log_directory.
# zip them into backup_directory
for directory in os.listdir(log_directory):
full_dir = os.path.join(log_directory, directory)
nothing_found = False
if os.path.isdir(full_dir):
if os.path.exists(os.path.join(full_dir, 'combat.log'))\
and os.path.exists(os.path.join(full_dir, 'game.log'))\
and os.path.exists(os.path.join(full_dir, 'chat.log'))\
and os.path.exists(os.path.join(full_dir, 'game.net.log')):
output_filename = '%s.zip' % directory
if os.path.exists(os.path.join(backup_directory, output_filename))\
and ((ommit_level >= 1 and compress) or (ommit_level==2 and not compress)):
logging.warning('Log %s exists as zip backup, ommited.' % output_filename)
elif os.path.exists(os.path.join(backup_directory, directory))\
and ((ommit_level == 2 and compress) or (ommit_level>=1 and not compress)):
logging.warning('Log %s exists as directory backup, ommited.' % directory)
else:
# do the backup
if compress:
make_zipfile(os.path.join(backup_directory, output_filename),
full_dir)
logging.info('Backed up %s' % directory)
if verbose:
print "Backed up %s" % directory
else:
if verbose:
print "Directory Raw Backup not implemented yet."
raise NotImplementedError
else:
if verbose:
print "%s is not a directory." % full_dir
if verbose and nothing_found:
print "Nothing to backup found in %s" % log_directory
if __name__ == '__main__':
print "Performing Log Backup (Dev)"
log_source = os.path.join(os.path.expanduser('~'),
'Documents', 'My Games', 'StarConflict', 'logs')
log_dest = os.path.join(os.path.expanduser('~'),
'Documents', 'My Games', 'sc')
backup_log_directory(log_source, log_dest, verbose=True, compress=True)

View File

@ -64,6 +64,7 @@ if __name__ == '__main__':
#f = open('output.txt', 'w') #f = open('output.txt', 'w')
rex = {} rex = {}
for logf in logfiles: for logf in logfiles:
logf.read()
logf.parse() logf.parse()
for l in logf.lines: for l in logf.lines:
if isinstance(l, dict): if isinstance(l, dict):

4
config/__init__.py Normal file
View File

@ -0,0 +1,4 @@
"""
Handle SCon's config file.
"""

52
config/display_config.py Normal file
View File

@ -0,0 +1,52 @@
"""
Simple brainstorm to display a config file.
"""
import os, logging
logging.basicConfig(level=logging.INFO)
# import ET:
try:
ET = None
import lxml.etree as ET
logging.info('Using LXML.')
except ImportError:
try:
import cElementTree as ET
logging.info('Using cElementTree')
except ImportError:
try:
import elementtree.ElementTree as ET
logging.info('Using ElementTree')
except ImportError:
import xml.etree.ElementTree as ET # python 2.5
logging.info('Using xml.ElementTree')
finally:
if not ET:
raise NotImplementedError, "XML Parser not found in your Python."
##################################################################################################
CONFIG_FILE = os.path.join(os.path.expanduser('~'),
'Documents',
'My Games',
'StarConflict',
'user_config.xml')
def read_config(config_file):
tree = ET.parse(config_file)
# doc = tree.getroot()
return tree
if __name__ == '__main__':
# Read the config
tree = read_config(CONFIG_FILE)
doc = tree.getroot()
if doc.tag == 'UserConfig' \
and len(doc) == 1\
and doc[0].tag == 'CVars'\
and doc[0].attrib['version'] == '4':
print "Found valid config file."
cvars = doc[0]
for child in cvars:
print '%s = %s' % (child.tag, child.attrib['val'])
else:
print "Not found valid config file."

0
game/__init__.py Normal file
View File

17
game/battle.py Normal file
View File

@ -0,0 +1,17 @@
"""
Represents a battle instance.
todo: finding battles. factory for missions, skirmishes?
"""
class Battle(object):
__slots__ = ['players',
'teams',
'time_start',
'time_end',]
def __init__(self, parent=None):
# parent is a log-session usually
pass

0
game/mission.py Normal file
View File

0
game/skirmish.py Normal file
View File

444
gui/treeview.py Normal file
View File

@ -0,0 +1,444 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
## FROM:
# https://github.com/cloudformdesign/cloudtb/blob/master/extra/PyQt/treeview.py
# ****** The Cloud Toolbox v0.1.2******
# This is the cloud toolbox -- a single module used in several packages
# found at <https://github.com/cloudformdesign>
# For more information see <cloudformdesign.com>
#
# This module may be a part of a python package, and may be out of date.
# This behavior is intentional, do NOT update it.
#
# You are encouraged to use this pacakge, or any code snippets in it, in
# your own projects. Hopefully they will be helpful to you!
#
# This project is Licenced under The MIT License (MIT)
#
# Copyright (c) 2013 Garrett Berg cloudformdesign.com
# An updated version of this file can be found at:
# <https://github.com/cloudformdesign/cloudtb>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
# http://opensource.org/licenses/MIT
# import pdb
import os
from PyQt4 import QtCore, QtGui
import sys
#import icons_rc
# from cloudtb import dbe
class Node(object):
'''A general node stucture to be used in treeview
the attrib_dict can store any information your overall treeview
needs it to store.
'''
def __init__(self, name, parent=None, icon = None,
attrib_dict = None):
self._name = name
self._attrib = attrib_dict
self._children = []
self._parent = parent
self.icon = icon
if parent is not None:
parent.addChild(self)
def addChild(self, child):
self._children.append(child)
def insertChild(self, position, child):
if position < 0 or position > len(self._children):
return False
self._children.insert(position, child)
child._parent = self
return True
def removeChild(self, position):
if position < 0 or position > len(self._children):
return False
child = self._children.pop(position)
child._parent = None
return True
def name(self):
return self._name
def setName(self, name):
self._name = name
def child(self, row):
return self._children[row]
def childCount(self):
return len(self._children)
def parent(self):
return self._parent
def row(self):
if self._parent is not None:
return self._parent._children.index(self)
def log(self, tabLevel=-1):
output = ""
tabLevel += 1
for i in range(tabLevel):
output += " "
output += "|-" + self._name + "\n"
for child in self._children:
output += child.log(tabLevel)
tabLevel -= 1
# output += "\n"
return output
def __repr__(self):
return self.log()
class TreeViewModel(QtCore.QAbstractItemModel):
"""INPUTS: Node, QObject"""
def __init__(self, root, parent=None, header_title = None):
super(TreeViewModel, self).__init__(parent)
self.is_editable = False
self.is_selectable = True
self.is_enabled = True
self.set_flags()
self._rootNode = root
self.header_title = header_title
# The following are created functions called in "data" -- which is a
# Qt defined funciton. This way of doing things is FAR more pythonic
# and allows classes to inherit this one and not have to rewrite the
# entire data method
# all of them recieve an index and a node
def role_display(self, index, node):
if index.column() == 0:
return node.name()
def role_edit(self, index, node):
return self.role_display(index, node)
def role_tool_tip(self, index, node):
return
def role_check_state(self, index, node):
return
def role_decoration(self, index, node):
if index.column() == 0:
icon = node.icon
if icon == None:
return False
else:
return icon
def role_flags(self, index, node):
'''While not technically a "role" it behaves in much the same way.
This method is called by the "flags" method for all indexes with
a node'''
return self.BASE_FLAGS
def set_flags(self, is_editable = None, is_selectable = None,
is_enabled = None):
''' Sets new flags to the BASE_FLAGS variable'''
if is_editable != None:
self.is_editable = is_editable
if is_selectable != None:
self.is_selectable = is_selectable
if is_enabled != None:
self.is_enabled = is_enabled
self.BASE_FLAGS = QtCore.Qt.ItemFlags(
(QtCore.Qt.ItemIsEnabled * bool(self.is_enabled))
| (QtCore.Qt.ItemIsSelectable * bool(self.is_selectable))
| (QtCore.Qt.ItemIsEditable * bool(self.is_editable))
)
"""INPUTS: QModelIndex"""
"""OUTPUT: int"""
def rowCount(self, parent):
if not parent.isValid():
parentNode = self._rootNode
else:
parentNode = parent.internalPointer()
return parentNode.childCount()
"""INPUTS: QModelIndex"""
"""OUTPUT: int"""
def columnCount(self, parent):
return 1
"""INPUTS: QModelIndex, int"""
"""OUTPUT: QVariant, strings are cast to QString which is a QVariant"""
def data(self, index, role):
'''index is an object that contains a pointer to the item inside
internPointer(). Note that this was set during the insertRows
method call, so you don't need to track them!
'''
if not index.isValid():
return None
node = index.internalPointer()
if role == QtCore.Qt.EditRole:
return self.role_edit(index, node)
if role == QtCore.Qt.ToolTipRole:
return self.role_tool_tip(index, node)
if role == QtCore.Qt.CheckStateRole:
return self.role_check_state(index, node)
if role == QtCore.Qt.DisplayRole:
return self.role_display(index, node)
if role == QtCore.Qt.DecorationRole:
return self.role_decoration(index, node)
"""INPUTS: QModelIndex, QVariant, int (flag)"""
def setData(self, index, value, role=QtCore.Qt.EditRole):
if index.isValid():
if role == QtCore.Qt.EditRole:
node = index.internalPointer()
node.setName(value)
return True
return False
"""INPUTS: int, Qt::Orientation, int"""
"""OUTPUT: QVariant, strings are cast to QString which is a QVariant"""
def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if self.header_title != None:
return self.header_title
if section == 0:
return "Scenegraph"
else:
return "Typeinfo"
"""INPUTS: QModelIndex"""
"""OUTPUT: int (flag)"""
# def flags(self, index):
# return (QtCore.Qt.ItemIsEnabled |
# QtCore.Qt.ItemIsSelectable #|
## QtCore.Qt.ItemIsEditable
# )
def flags(self, index):
if not index.isValid():
return self.BASE_FLAGS
node = index.internalPointer()
return self.role_flags(index, node)
"""INPUTS: QModelIndex"""
"""OUTPUT: QModelIndex"""
"""Should return the parent of the node with the given QModelIndex"""
def parent(self, index):
node = self.getNode(index)
parentNode = node.parent()
if parentNode == self._rootNode:
return QtCore.QModelIndex()
return self.createIndex(parentNode.row(), 0, parentNode)
"""INPUTS: int, int, QModelIndex"""
"""OUTPUT: QModelIndex"""
"""Should return a QModelIndex that corresponds to the given row,
column and parent node"""
def index(self, row, column, parent):
# This is how Qt creates the nested (tree) list. It knows how many
# rows it has because of insertRows, and it uses index and
# createIndex to build the tree.
# print 'Index called', row, column
parentNode = self.getNode(parent)
childItem = parentNode.child(row)
if childItem:
return self.createIndex(row, column, childItem)
else:
return QtCore.QModelIndex()
"""CUSTOM"""
"""INPUTS: QModelIndex"""
def getNode(self, index):
if index.isValid():
node = index.internalPointer()
if node:
return node
return self._rootNode
"""INPUTS: int, List of Nodes, QModelIndex"""
def insertRows(self, position, rows, parent=QtCore.QModelIndex()):
parentNode = self.getNode(parent)
self.beginInsertRows(parent, position, position + len(rows) - 1)
for i, row in enumerate(rows):
# childCount = parentNode.childCount()
childNode = row
success = parentNode.insertChild(position + i, childNode)
self.endInsertRows()
return success
"""INPUTS: int, int, QModelIndex"""
def removeRows(self, position, rows, parent=QtCore.QModelIndex()):
if rows == 0:
return
parentNode = self.getNode(parent)
self.beginRemoveRows(parent, position, position + rows - 1)
for row in range(rows):
success = parentNode.removeChild(position)
# TODO: break if not success?
self.endRemoveRows()
return success
def clear_rows(self):
return self.removeRows(0, self._rootNode.childCount())
# TODO: doesn't work. Not sure how to get icons
ICON_FOLDER = QtGui.QIcon.fromTheme('folder')
def _node_compare(a, b):
return b.isdir - a.isdir
def get_file_folder_node(fdata, parent):
'''return the node structure of the data.
[[(dir_name, path),
[dir_name, path),
[(file, path),
(file, path)]]
]
]
'''
# TODO: set icons correctly
nodes = []
for fobj in fdata:
path = fobj[0]
name = os.path.split(path)[1]
if len(fobj) == 1:
fileobj = Node(name, parent = parent, icon = None)
fileobj.full_path = path
fileobj.isdir = False
nodes.append(fileobj)
continue
folderobj = Node(name, parent = parent, icon = ICON_FOLDER,
)
folderobj.full_path = path
folderobj.isdir = True
get_file_folder_node(fobj[1], parent = folderobj)
nodes.append(folderobj)
nodes.sort(cmp = _node_compare)
return nodes
import itertools
def _get_filelist_nodes(iter_file_list, dir_path = ''):
'''Takes a sorted file list iterator and returns the files in a
format that can be converted'''
files = []
dir_path = os.path.join(dir_path, '') # Put into directory syntax
len_dp = len(dir_path)
while True:
try:
fpath = next(iter_file_list)
except StopIteration:
break
if dir_path != fpath[:len_dp]:
iter_file_list = itertools.chain((fpath,), iter_file_list)
break
if os.path.isdir(fpath):
iter_file_list, new_files = _get_filelist_nodes(iter_file_list,
dir_path = fpath)
files.append((fpath, new_files))
else:
files.append((fpath,))
return iter_file_list, files
def get_filelist_nodes(file_list, parent = None):
file_list = sorted(file_list)
file_tuples = _get_filelist_nodes(iter(file_list))[1]
return get_file_folder_node(file_tuples, parent)
def dev_show_file_list(file_objects):
'''For developemnet'''
app = QtGui.QApplication(sys.argv)
rootNode = Node("Rootdir")
model = TreeViewModel(rootNode)
treeView = QtGui.QTreeView()
treeView.show()
treeView.setModel(model)
model.insertRows(0, file_objects, QtCore.QModelIndex())
sys.exit(app.exec_())
if __name__ == '__main__':
from pprint import pprint
files = '''/home/user/Projects/Learning/LearningQt/LearningQt.pro.user
/home/user/Projects/Learning/LearningQt/LearningQt.pro
/home/user/Projects/Learning/LearningQt/qmlapplicationviewer/qmlapplicationviewer.h
/home/user/Projects/Learning/LearningQt/qmlapplicationviewer/qmlapplicationviewer.cpp
/home/user/Projects/Learning/LearningQt/qmlapplicationviewer/qmlapplicationviewer.pri
/home/user/Projects/Learning/LearningQt/qmlapplicationviewer
/home/user/Projects/Learning/LearningQt/LearningQt64.png
/home/user/Projects/Learning/LearningQt/LearningQt_harmattan.desktop
/home/user/Projects/Learning/LearningQt/LearningQt.svg
/home/user/Projects/Learning/LearningQt/main.cpp
/home/user/Projects/Learning/LearningQt/LearningQt.desktop
/home/user/Projects/Learning/LearningQt/qml/LearningQt/main.qml
/home/user/Projects/Learning/LearningQt/qml/LearningQt
/home/user/Projects/Learning/LearningQt/qml
/home/user/Projects/Learning/LearningQt/LearningQt80.png'''
nodes = get_filelist_nodes(files.split('\n'))
for n in nodes:
print n
dev_show_file_list(nodes)

View File

@ -7,6 +7,19 @@
import sys import sys
from PyQt4 import QtCore, QtGui, QtWebKit from PyQt4 import QtCore, QtGui, QtWebKit
from treeview import TreeViewModel, Node
class MenuTree(QtGui.QTreeView):
def __init__(self, *args, **kwargs):
QtGui.QTreeView.__init__(self, *args, **kwargs)
self.test_tree()
def test_tree(self):
self.rootNode = Node("Rootdir")
model = TreeViewModel(self.rootNode)
self.setModel(model)
self.rootNode.addChild(Node('Hey'))
class Browser(QtGui.QMainWindow): class Browser(QtGui.QMainWindow):
@ -42,8 +55,13 @@ class Browser(QtGui.QMainWindow):
self.horizontalLayout.addWidget(self.tb_url) self.horizontalLayout.addWidget(self.tb_url)
self.gridLayout.addLayout(self.horizontalLayout) self.gridLayout.addLayout(self.horizontalLayout)
self.horizontalMainLayout = QtGui.QHBoxLayout()
self.gridLayout.addLayout(self.horizontalMainLayout)
#
self.menu = MenuTree()
self.html = QtWebKit.QWebView() self.html = QtWebKit.QWebView()
self.gridLayout.addWidget(self.html) self.horizontalMainLayout.addWidget(self.menu)
self.horizontalMainLayout.addWidget(self.html)
self.mainLayout.addWidget(self.frame) self.mainLayout.addWidget(self.frame)
self.setCentralWidget(self.centralwidget) self.setCentralWidget(self.centralwidget)

9
logs/base.py Normal file
View File

@ -0,0 +1,9 @@
class Log(object):
matcher = None
@classmethod
def is_handler(cls, log):
return False

View File

@ -19,13 +19,7 @@
The typical log entry The typical log entry
""" """
import re import re
from base import Log
class Log(object):
matcher = None
@classmethod
def is_handler(cls, log):
return False
class CombatLog(Log): class CombatLog(Log):
@classmethod @classmethod
@ -134,6 +128,7 @@ class GameEvent(CombatLog):
def unpack(self): def unpack(self):
# unpacks the data from the values. # unpacks the data from the values.
# small override to remove trailing "="s in the matching.
if hasattr(self, 'matcher') and self.matcher: if hasattr(self, 'matcher') and self.matcher:
matchers = self.matcher matchers = self.matcher
if not isinstance(matchers, list): if not isinstance(matchers, list):

4
logs/game.py Normal file
View File

@ -0,0 +1,4 @@
GAME_LOGS = []

View File

@ -18,14 +18,18 @@ class LogFile(object):
self.folder = folder # only for custom tagging. self.folder = folder # only for custom tagging.
self.lines = [] self.lines = []
self._data = None self._data = None
if self.fname is not None:
self.open(self.fname)
def open(self, fname): def read(self, fname=None):
fname = fname or self.fname
try:
f = open(fname, 'r') f = open(fname, 'r')
self._data = f.read() self._data = f.read()
finally:
f.close() f.close()
def set_data(self, data):
self._data = data
def parse(self): def parse(self):
# parse _data if we still have no lines. # parse _data if we still have no lines.
if self._data: if self._data:

38
logs/logfiles.py Normal file
View File

@ -0,0 +1,38 @@
"""
Resolves Logs.
"""
from logfile import LogFile
from combat import COMBAT_LOGS
from game import GAME_LOGS
class LogFileResolver(LogFile):
''' dynamic logfile resolver '''
resolution_classes = COMBAT_LOGS
def __init__(self, *args, **kwargs):
super(LogFileResolver, self).__init__(*args, **kwargs)
self.resolution_classes = self.resolution_classes or []
def resolve(self, line):
for klass in self.resolution_classes:
if klass.is_handler(line):
return klass(line)
return line
class CombatLogFile(LogFile):
''' Combat Log '''
def resolve(self, line):
for klass in COMBAT_LOGS:
if klass.is_handler(line):
return klass(line)
return line
class GameLogFile(LogFile):
''' Game Log '''
def resolve(self, line):
for klass in GAME_LOGS:
if klass.is_handler(line):
return klass(line)
return line

View File

@ -1,19 +0,0 @@
"""
Resolves Logs.
"""
from logfile import LogFile
from combat import COMBAT_LOGS
class LogFileResolver(LogFile):
resolution_classes = COMBAT_LOGS
def __init__(self, *args, **kwargs):
super(LogFileResolver, self).__init__(*args, **kwargs)
self.resolution_classes = self.resolution_classes or []
def resolve(self, line):
for klass in self.resolution_classes:
if klass.is_handler(line):
return klass(line)
return line

70
logs/session.py Normal file
View File

@ -0,0 +1,70 @@
"""
Logging Session.
"""
import zipfile, logging, os
from logfiles import CombatLogFile, GameLogFile
class LogSession(object):
"""
The Log-Session is supposed to save one directory of logs.
It can parse its logs, and build up its internal structure into Battle Instances etc.
"""
def __init__(self, directory):
''' if directory is a file, it will be handled as a compressed folder '''
self.battles = []
self.user = None
# various logfiles used.
self.combat_log = None
self.game_log = None
self.chat_log = None
# self.net_log = None
self.directory = directory
self._zip_source = False
def parse_files(self):
''' parses the logfiles '''
# check if directory is a file
self._zip_source = os.path.isfile(self.directory) or False
if self._zip_source:
self._unzip_logs()
else:
self.combat_log = CombatLogFile(os.path.join(self.directory, 'combat.log'))
self.combat_log.read()
self.game_log = GameLogFile(os.path.join(self.directory, 'game.log'))
self.game_log.read()
# parse all files
self.combat_log.parse()
self.game_log.parse()
def determine_owner(self):
''' determines the user in the parsed gamelog '''
pass
def parse_battles(self):
''' parses the battles '''
pass
def _unzip_logs(self):
z = zipfile.ZipFile(self.directory, "r")
for filename in z.namelist():
fn = os.path.split(filename)[1] or ''
fn = fn.lower()
if fn:
if fn == 'combat.log':
self.combat_log = CombatLogFile(fn)
self.combat_log.set_data(z.read(filename))
elif fn == 'game.log':
self.game_log = GameLogFile(fn)
self.game_log.set_data(z.read(filename))
if __name__ == '__main__':
l_raw = LogSession('D:\\Users\\g4b\\Documents\\My Games\\sc\\2014.05.17 15.50.28')
l_zip = LogSession('D:\\Users\\g4b\\Documents\\My Games\\sc\\2014.05.20 23.49.19.zip')
l_zip.parse_files()
print l_zip.combat_log.lines