59e23e1158683b8bcf1a01fccc9856efd898dd28
[progcomp2013.git] / qchess / src / log.py
1 log_file = None
2 import datetime
3 import urllib2
4
5 class LogFile():
6         def __init__(self, file_name):  
7                 
8                 self.log = open(file_name, "w", 0)
9
10         def write(self, s):
11                 self.log.write(str(datetime.datetime.now()) + " : " + s + "\n")
12
13         def setup(self, board, players):
14                 self.log.write("# Log starts " + str(datetime.datetime.now()) + "\n")
15                 for p in players:
16                         self.log.write("# " + p.colour + " : " + p.name + "\n")
17                 
18                 self.log.write("# Initial board\n")
19                 for x in range(0, w):
20                         for y in range(0, h):
21                                 if board.grid[x][y] != None:
22                                         self.log.write(str(board.grid[x][y]) + "\n")
23
24                 self.log.write("# Start game\n")
25
26 class HttpLog(LogFile):
27         def __init__(self, file_name):
28                 LogFile.__init__(self, file_name)
29                 self.file_name = file_name
30
31         def prelog(self):
32                 self.log.close()
33                 self.log = open(self.file_name, "w", 0)
34
35                 LogFile.setup(self, game.board, game.players)
36                 
37
38 class HeadRequest(urllib2.Request):
39         def get_method(self):
40                 return "HEAD"
41                 
42 class HttpReplay():
43         def __init__(self, address):
44                 self.read_setup = False
45                 self.log = urllib2.urlopen(address)
46                 self.address = address
47
48         def readline(self):
49                 
50                 line = self.log.readline()
51                 sys.stderr.write(sys.argv[0] + " : " + str(self.__class__.__name__) + " read \""+str(line.strip("\r\n")) + "\" from address " + str(self.address) + "\n")
52                 if line == "":
53                         sys.stderr.write(sys.argv[0] + " : " + str(self.__class__.__name__) + " retrieving from address " + str(self.address) + "\n")
54                         date_mod = datetime.datetime.strptime(self.log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
55                         self.log.close()
56
57                         next_log = urllib2.urlopen(HeadRequest(self.address))
58                         date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
59                         while date_new <= date_mod:
60                                 next_log = urllib2.urlopen(HeadRequest(self.address))
61                                 date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
62
63                         self.log = urllib2.urlopen(self.address)
64                         game.setup()
65                         line = self.log.readline()
66
67
68                 return line
69                         
70         def close(self):
71                 self.log.close()
72                                                 
73 def log(s):
74         if log_file != None:
75                 log_file.write(s)
76                 
77
78 def log_init(board, players):
79         if log_file != None:
80                 log_file.setup(board, players)
81

UCC git Repository :: git.ucc.asn.au