From 5e4b175a7410b8878705b69ed8f97caf10b45a64 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Wed, 30 Jan 2013 17:03:57 +0800 Subject: [PATCH] Feature creep in log files I want to be able to watch a game that's playing on the server! Without having to actually use sockets and shit! I've used enough sockets already! --- qchess/qchess.py | 124 ++++++++++++++++++++++++++++++++-------- qchess/src/Makefile | 2 +- qchess/src/game.py | 29 ++-------- qchess/src/images.py | 5 +- qchess/src/log.py | 81 ++++++++++++++++++++++++++ qchess/src/main.py | 16 ++++-- qchess/tools/images.pyc | Bin 2392 -> 0 bytes 7 files changed, 204 insertions(+), 53 deletions(-) create mode 100644 qchess/src/log.py delete mode 100644 qchess/tools/images.pyc diff --git a/qchess/qchess.py b/qchess/qchess.py index 02a724a..1a9f398 100755 --- a/qchess/qchess.py +++ b/qchess/qchess.py @@ -1199,29 +1199,91 @@ class StoppableThread(threading.Thread): def stopped(self): return self._stop.isSet() # --- thread_util.py --- # - - log_file = None +import datetime +import urllib2 -def log(s): - if log_file != None: - import datetime - log_file.write(str(datetime.datetime.now()) + " : " + s + "\n") +class LogFile(): + def __init__(self, file_name): + + self.log = open(file_name, "w", 0) -def log_init(board, players): - if log_file != None: - import datetime - log_file.write("# Log starts " + str(datetime.datetime.now()) + "\n") + def write(self, s): + self.log.write(str(datetime.datetime.now()) + " : " + s + "\n") + + def setup(self, board, players): + self.log.write("# Log starts " + str(datetime.datetime.now()) + "\n") for p in players: - log_file.write("# " + p.colour + " : " + p.name + "\n") + self.log.write("# " + p.colour + " : " + p.name + "\n") - log_file.write("# Initial board\n") + self.log.write("# Initial board\n") for x in range(0, w): for y in range(0, h): if board.grid[x][y] != None: - log_file.write(str(board.grid[x][y]) + "\n") + self.log.write(str(board.grid[x][y]) + "\n") + + self.log.write("# Start game\n") + +class HttpLog(LogFile): + def __init__(self, file_name): + LogFile.__init__(self, file_name) + self.file_name = file_name + + def write(self, s): + self.log.close() + self.log = open(self.file_name, "w", 0) + + LogFile.setup(self, game.board, game.players) + + LogFile.write(self, s) + + +class HeadRequest(urllib2.Request): + def get_method(self): + return "HEAD" + +class HttpReplay(): + def __init__(self, address): + self.read_setup = False + self.log = urllib2.urlopen(address) + self.address = address + + def readline(self): + + line = self.log.readline() + sys.stderr.write(sys.argv[0] + " : " + str(self) + " read \""+str(line) + "\" from address " + str(self.address) + "\n") + if line == "": + sys.stderr.write(sys.argv[0] + " : " + str(self) + " retrieving from address " + str(self.address) + "\n") + date_mod = datetime.datetime.strptime(self.log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT") + self.log.close() + + next_log = urllib2.urlopen(HeadRequest(self.address)) + date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT") + while date_new <= date_mod: + next_log = urllib2.urlopen(HeadRequest(self.address)) + date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT") + + self.log = urllib2.urlopen(address) + game.setup() + line = self.log.readline() + + + return line + + +def log(s): + if log_file != None: + log_file.write(s) + + +def log_init(board, players): + if log_file != None: + log_file.setup(board, players) + +# --- log.py --- # + + - log_file.write("# Start game\n") # A thread that runs the game @@ -1356,7 +1418,10 @@ class ReplayThread(GameThread): self.line_number = 0 self.end = end - try: + self.setup() + + def setup(self): + if True: while self.src.readline().strip(" \r\n") != "# Initial board": self.line_number += 1 @@ -1381,8 +1446,8 @@ class ReplayThread(GameThread): line = self.src.readline().strip(" \r\n") - except Exception, e: - raise Exception("FILE line: " + str(self.line_number) + " \""+str(line)+"\"") #\n" + e.message) + #except Exception, e: + # raise Exception("FILE line: " + str(self.line_number) + " \""+str(line)+"\"") #\n" + e.message) def run(self): i = 0 @@ -1470,7 +1535,10 @@ def opponent(colour): else: return "white" # --- game.py --- # -import pygame +try: + import pygame +except: + pass import os # Dictionary that stores the unicode character representations of the different pieces @@ -1552,7 +1620,7 @@ class GraphicsThread(StoppableThread): #print "Test font" pygame.font.Font(os.path.join(os.path.curdir, "data", "DejaVuSans.ttf"), 32).render("Hello", True,(0,0,0)) - #create_images(grid_sz) + #load_images() create_images(grid_sz) """ @@ -2079,17 +2147,25 @@ def main(argv): if len(arg[2:].split("=")) == 1: src_file = sys.stdin else: - src_file = open(arg[2:].split("=")[1].split(":")[0]) + f = arg[2:].split("=")[1] + if f[0] == '@': + src_file = HttpReplay("http://" + f.split(":")[0][1:]) + else: + src_file = open(f.split(":")[0], "r", 0) - if len(arg[2:].split(":")) == 2: - max_lines = int(arg[2:].split(":")[1]) + if len(f.split(":")) == 2: + max_lines = int(f.split(":")[1]) elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"): # Log file if len(arg[2:].split("=")) == 1: log_file = sys.stdout else: - log_file = open(arg[2:].split("=")[1], "w") + f = arg[2:].split("=")[1] + if f[0] == '@': + log_file = HttpLog(f[1:]) + else: + log_file = LogFile(f) elif (arg[1] == '-' and arg[2:].split("=")[0] == "delay"): # Delay if len(arg[2:].split("=")) == 1: @@ -2245,4 +2321,4 @@ if __name__ == "__main__": sys.exit(102) # --- main.py --- # -# EOF - created from make on Wed Jan 30 00:45:46 WST 2013 +# EOF - created from make on Wed Jan 30 17:03:00 WST 2013 diff --git a/qchess/src/Makefile b/qchess/src/Makefile index 268cbfe..c7929ab 100644 --- a/qchess/src/Makefile +++ b/qchess/src/Makefile @@ -1,7 +1,7 @@ # Makefile that builds qchess.py from the component files TARGET = qchess.py -COMPONENTS = piece.py board.py player.py agent_bishop.py timeout_player.py network.py thread_util.py game.py images.py graphics.py main.py +COMPONENTS = piece.py board.py player.py agent_bishop.py timeout_player.py network.py thread_util.py log.py game.py images.py graphics.py main.py #COMPONENTS=$(shell ls *.py | tr '\t' '\n' | grep -v $(TARGET)) $(TARGET) : $(COMPONENTS) diff --git a/qchess/src/game.py b/qchess/src/game.py index 07a1325..9938e0d 100644 --- a/qchess/src/game.py +++ b/qchess/src/game.py @@ -1,26 +1,6 @@ -log_file = None - -def log(s): - if log_file != None: - import datetime - log_file.write(str(datetime.datetime.now()) + " : " + s + "\n") - -def log_init(board, players): - if log_file != None: - import datetime - log_file.write("# Log starts " + str(datetime.datetime.now()) + "\n") - for p in players: - log_file.write("# " + p.colour + " : " + p.name + "\n") - - log_file.write("# Initial board\n") - for x in range(0, w): - for y in range(0, h): - if board.grid[x][y] != None: - log_file.write(str(board.grid[x][y]) + "\n") - log_file.write("# Start game\n") # A thread that runs the game @@ -155,7 +135,10 @@ class ReplayThread(GameThread): self.line_number = 0 self.end = end - try: + self.setup() + + def setup(self): + if True: while self.src.readline().strip(" \r\n") != "# Initial board": self.line_number += 1 @@ -180,8 +163,8 @@ class ReplayThread(GameThread): line = self.src.readline().strip(" \r\n") - except Exception, e: - raise Exception("FILE line: " + str(self.line_number) + " \""+str(line)+"\"") #\n" + e.message) + #except Exception, e: + # raise Exception("FILE line: " + str(self.line_number) + " \""+str(line)+"\"") #\n" + e.message) def run(self): i = 0 diff --git a/qchess/src/images.py b/qchess/src/images.py index dd79b10..7284868 100644 --- a/qchess/src/images.py +++ b/qchess/src/images.py @@ -1,4 +1,7 @@ -import pygame +try: + import pygame +except: + pass import os # Dictionary that stores the unicode character representations of the different pieces diff --git a/qchess/src/log.py b/qchess/src/log.py new file mode 100644 index 0000000..0712910 --- /dev/null +++ b/qchess/src/log.py @@ -0,0 +1,81 @@ +log_file = None +import datetime +import urllib2 + +class LogFile(): + def __init__(self, file_name): + + self.log = open(file_name, "w", 0) + + def write(self, s): + self.log.write(str(datetime.datetime.now()) + " : " + s + "\n") + + def setup(self, board, players): + self.log.write("# Log starts " + str(datetime.datetime.now()) + "\n") + for p in players: + self.log.write("# " + p.colour + " : " + p.name + "\n") + + self.log.write("# Initial board\n") + for x in range(0, w): + for y in range(0, h): + if board.grid[x][y] != None: + self.log.write(str(board.grid[x][y]) + "\n") + + self.log.write("# Start game\n") + +class HttpLog(LogFile): + def __init__(self, file_name): + LogFile.__init__(self, file_name) + self.file_name = file_name + + def write(self, s): + self.log.close() + self.log = open(self.file_name, "w", 0) + + LogFile.setup(self, game.board, game.players) + + LogFile.write(self, s) + + +class HeadRequest(urllib2.Request): + def get_method(self): + return "HEAD" + +class HttpReplay(): + def __init__(self, address): + self.read_setup = False + self.log = urllib2.urlopen(address) + self.address = address + + def readline(self): + + line = self.log.readline() + sys.stderr.write(sys.argv[0] + " : " + str(self) + " read \""+str(line) + "\" from address " + str(self.address) + "\n") + if line == "": + sys.stderr.write(sys.argv[0] + " : " + str(self) + " retrieving from address " + str(self.address) + "\n") + date_mod = datetime.datetime.strptime(self.log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT") + self.log.close() + + next_log = urllib2.urlopen(HeadRequest(self.address)) + date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT") + while date_new <= date_mod: + next_log = urllib2.urlopen(HeadRequest(self.address)) + date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT") + + self.log = urllib2.urlopen(address) + game.setup() + line = self.log.readline() + + + return line + + +def log(s): + if log_file != None: + log_file.write(s) + + +def log_init(board, players): + if log_file != None: + log_file.setup(board, players) + diff --git a/qchess/src/main.py b/qchess/src/main.py index 137d1cb..87fe35b 100644 --- a/qchess/src/main.py +++ b/qchess/src/main.py @@ -115,17 +115,25 @@ def main(argv): if len(arg[2:].split("=")) == 1: src_file = sys.stdin else: - src_file = open(arg[2:].split("=")[1].split(":")[0]) + f = arg[2:].split("=")[1] + if f[0] == '@': + src_file = HttpReplay("http://" + f.split(":")[0][1:]) + else: + src_file = open(f.split(":")[0], "r", 0) - if len(arg[2:].split(":")) == 2: - max_lines = int(arg[2:].split(":")[1]) + if len(f.split(":")) == 2: + max_lines = int(f.split(":")[1]) elif (arg[1] == '-' and arg[2:].split("=")[0] == "log"): # Log file if len(arg[2:].split("=")) == 1: log_file = sys.stdout else: - log_file = open(arg[2:].split("=")[1], "w") + f = arg[2:].split("=")[1] + if f[0] == '@': + log_file = HttpLog(f[1:]) + else: + log_file = LogFile(f) elif (arg[1] == '-' and arg[2:].split("=")[0] == "delay"): # Delay if len(arg[2:].split("=")) == 1: diff --git a/qchess/tools/images.pyc b/qchess/tools/images.pyc deleted file mode 100644 index 0a48eb3c42ed7b00e35fe3a13bf90f07bd66a2be..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2392 zcmb_c-EJFI5T3J+?Zk1CHgPa+3izS|_kx&!lq=Mt{%}VGn~F-2khR%8vDZJl>pjP+ zQzbV|9{~#GuTQ{3@Dkkh2Jr^maZ|pT-J~Q+fJ$9^&&+;j{?444Dg3!miHoH#e9E60 z&`CZWRCYrlL&mV~Sz_XrNgt0ri?|nJh_9C-x?Vj%d;vg${Z{syfK#+k0 ztPVAB(W|vXT=>jsq(IX@lqKpbI)crwep82#kct>Mls>2Fue=SUaV+wP4vS=+48sj7 zLSWg29l)PMM-KJNlxrOnsbQt2aDDjLJW{0W+(p`j^R+8{vB%oudvt%iAyJlyquikB z+Sw>e>OZ1HvDZ%`MFTXBBc1=Jul$_6*RTAqgI1*m7x*0idd>^|FP`$BW$HZt7o=)Q z{9h9PvT;)lqeckbUUMh_-(ECPuG8}nbow1Vy?V<%u_PgFli{xG$Sz$h9 zhMOkUQJ^z3_tJ^BRbXc14TqiF7>|tIVQ5<{Eq&ch!=PgxqLPW&U?*bpyglrg@L|T9 zjOCR2G82*(SdztXm$rNHNVknQYTt?d$w)`mv_DVdZt9Ijm{9EmydRH}4?o!Ws6F<3 z+L*SD-$dwDbDb>at_+eoCP^LGBSjI>^q0IJj(Bi9qeDg0Ck$Zc7$zAs5*v69TSTC5^nI7O^fQQs29(wk3T~bY~~%n7qlhG zxgAf2LG+$&4r4E9X6bD1rSYg~Rw2}EC7$gygIF6D($mn`>vD%3!_}~!L|wyW*pWiv z&lV>Y1A{0wQeSLAN-xIEL{B`h-VB_1)t;R(}^!^o{c zKsrZ^iBu;DQuercDP>v!mulMn3IBrtQD5w>@H&u&MA>q4kGbe}Rd`QIZ zJIr0fBI)oaDaU-3!FRE|jQRC;#w=`qJc@(KP~YHMH|(scs>KCWKKae7$_Zaqi%w0| IoNKke0cgn0PXGV_ -- 2.20.1