Created
August 13, 2014 06:02
-
-
Save TorelTwiddler/43bce4e7bafddf141254 to your computer and use it in GitHub Desktop.
CombatLogSplitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################ | |
## Made to split WoW combat log by day | |
## can also split any space-delimited file by the first line | |
## | |
## Written by Salvatore Lopiparo | |
## 2008 | |
import tkFileDialog, threading | |
from Tkinter import * | |
class LogSplitter(Frame): | |
def __init__(self, master=None): | |
self.master = master | |
Frame.__init__(self, master) | |
self.grid() | |
self.make_widgets() | |
def make_widgets(self): | |
self.get_log_button = Button(self, text='Split Log', command=self.get_log) | |
self.get_log_button.grid(column=0, row=1, columnspan=2) | |
self.break_length_label = Label(self, text="Break Length:") | |
self.break_length_label.grid(column=0, row=0) | |
self.break_length_scale = Scale(self, from_=1, to=59, orient=HORIZONTAL) | |
self.break_length_scale.grid(column=1, row=0) | |
self.combine_files_label1 = Label(self, text="Combines two files into one. ") | |
self.combine_files_label1.grid(column=2, row=1) | |
self.combine_files_label2 = Label(self, text="1st is source, 2nd is destination.") | |
self.combine_files_label2.grid(column=2, row=2) | |
self.combine_files_button = Button(self, text="Combine Files", command=self.combine_files) | |
self.combine_files_button.grid(column=2, row=0) | |
self.quit_button = Button(self, text='Exit', command=self.quit) | |
self.quit_button.grid(column=0, row=5, columnspan=2, sticky=N+E+S+W) | |
def combine_files(self): | |
source_file = tkFileDialog.askopenfile(mode='r') | |
if not source_file: | |
return | |
destination_file = tkFileDialog.askopenfile(mode='a') | |
if not destination_file: | |
return | |
#source_list = [] | |
#destination_file.seek(-1, 2) | |
for line in source_file.readlines(): | |
destination_file.write(line) | |
def get_log(self): | |
self.input_filepath = tkFileDialog.askopenfilename() | |
self.output_dir = tkFileDialog.askdirectory() | |
SplitLog(self, self.input_filepath, self.output_dir).start() | |
class SplitLog(threading.Thread): | |
def __init__(self, master, input_filepath, output_dir): | |
self.input_filepath = input_filepath | |
self.output_dir = output_dir | |
threading.Thread.__init__ ( self ) | |
self.master = master | |
def run(self): | |
self.split_log() | |
def split_log(self): | |
in_file = open(self.input_filepath, 'r') | |
#print self.input_filepath | |
self.out_file_root = self.input_filepath.split('/')[-1] | |
self.out_file_root = self.out_file_root.split('.')[0] | |
line_list = [] | |
current_file = 0 | |
#print self.out_file_root, current_file, list[0]['date'].replace('/','-') | |
current_list = "%s_%s"%(self.out_file_root, current_file) | |
self.file_dict = {current_list: []} | |
prev_line = {} | |
current_number = 0 | |
for line in in_file.readlines(): | |
date = line.split(' ')[0] | |
time = line.split(' ')[1] | |
hour, min, sec = time.split(':') | |
line_dict = { | |
"name" : line.split(' '), | |
"line" : line, | |
"date" : date, | |
"time" : {"day":date, "hour":hour, "min":min, "sec":sec} | |
} | |
line_list.append(line_dict) | |
#check if this line is soon after the prev line | |
add_to_file = True | |
if prev_line: | |
add_to_file = self.is_soon_after(prev_line['time'], line_dict['time']) | |
else: | |
file_date = line_dict['date'].replace('/','-') | |
if add_to_file: | |
self.file_dict[current_list].append(line_dict['line']) | |
line_list.remove(line_dict) | |
else: | |
file_name = "%s_%s_%s.txt"%(self.out_file_root, current_file, file_date) | |
file_path = '%s/%s'%(self.output_dir, file_name) | |
#print file_path | |
write_file = open(file_path, 'w') | |
write_file.writelines(self.file_dict[current_list]) | |
write_file.close() | |
self.file_dict[current_list] = [] | |
current_file += 1 | |
current_list = "%s_%s"%(self.out_file_root, current_file) | |
self.file_dict[current_list] = [] | |
file_date = line_dict['date'].replace('/','-') | |
prev_line = line_dict | |
if float(current_number)/1000 == int(float(current_number)/1000): | |
self.master.progress_label1 = Label(self.master, text='Current Line: %s'%current_number) | |
self.master.progress_label1.grid(column=0, row=3, columnspan=3, sticky=N+E+S+W) | |
self.master.progress_label2 = Label(self.master, | |
text='Current File: %s_%s_%s.txt'%(self.out_file_root, | |
current_file, | |
file_date)) | |
self.master.progress_label2.grid(column=0, row=4, columnspan=3, sticky=N+E+S+W) | |
current_number += 1 | |
file_name = "%s_%s_%s.txt"%(self.out_file_root, current_file, file_date) | |
file_path = '%s/%s'%(self.output_dir, file_name) | |
#print file_path | |
write_file = open(file_path, 'w') | |
write_file.writelines(self.file_dict[current_list]) | |
write_file.close() | |
self.file_dict[current_list] = [] | |
current_file += 1 | |
current_list = "%s_%s"%(self.out_file_root, current_file) | |
self.file_dict[current_list] = [] | |
file_date = line_dict['date'].replace('/','-') | |
self.master.progress_label1 = Label(self.master, text='Finished! Lines: %s'%current_number) | |
self.master.progress_label1.grid(column=0, row=3, columnspan=3, sticky=N+E+S+W) | |
self.master.progress_label2 = Label(self.master, | |
text='Directory: %s'%self.output_dir) | |
self.master.progress_label2.grid(column=0, row=4, columnspan=3, sticky=N+E+S+W) | |
def is_soon_after(self, prev, next): | |
break_length = self.master.break_length_scale.get() | |
#print prev, next | |
if prev['day'] == next['day']: | |
#print 'same day' | |
if prev['hour'] == next['hour']: | |
#print 'same hour' | |
if int(next['min']) - int(prev['min']) > break_length: | |
#print 'returning false' | |
return False | |
else: # prev hour is not next hour | |
#print 'different hour' | |
prev_close = self.is_close_to_hour(prev) | |
next_close = self.is_close_to_hour(next) | |
if prev_close['up'] + next_close['down'] > break_length: | |
#print 'returning false' | |
return False | |
else: #prev day is not next day | |
#print 'different day' | |
prev_close = self.is_close_to_midnight(prev) | |
next_close = self.is_close_to_midnight(next) | |
#print prev_close, next_close | |
if prev_close and next_close: | |
if int(prev_close) + int(next_close) > break_length: | |
#print 'returning false' | |
return False | |
else: | |
#print 'returning false' | |
return False | |
return True | |
def is_close_to_midnight(self, line): | |
break_length = self.master.break_length_scale.get() | |
if int(line['hour']) == 23: | |
return str(self.is_close_to_hour(line)['up']) | |
elif int(line['hour']) == 0: | |
return str(self.is_close_to_hour(line)['down']) | |
return False | |
def is_close_to_hour(self, line): | |
up = 59 - int(line['min']) | |
down = int(line['min']) | |
return {'up': up, 'down': down} | |
if __name__ == '__main__': | |
master = Tk() | |
app = LogSplitter(master) | |
app.master.title("Log Splitter - Created by Salvatore Lopiparo") | |
master.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was a project I did several years ago, when I was learning programming. This application split and combined combat logs from World of Warcraft from two or more guild members to create the most exhaustive logs possible for uploading to WoW Web Stats. This was done to track DPS, healing, and damage taken for guild raids. Ensuring that it could detect and adapt to differences in time zone and different computer time was important.