981e936b5d9e27a9a4e37f1dffbb7ede50e56375
[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 write(self, s):
32                 self.log.close()
33                 self.log = open(self.file_name, "w", 0)
34
35                 LogFile.setup(self, game.board, game.players)
36
37                 LogFile.write(self, s)
38                 
39
40 class HeadRequest(urllib2.Request):
41         def get_method(self):
42                 return "HEAD"
43                 
44 class HttpReplay():
45         def __init__(self, address):
46                 self.read_setup = False
47                 self.log = urllib2.urlopen(address)
48                 self.address = address
49
50         def readline(self):
51                 
52                 line = self.log.readline()
53                 sys.stderr.write(sys.argv[0] + " : " + str(self.__class__.__name__) + " read \""+str(line.strip("\r\n")) + "\" from address " + str(self.address) + "\n")
54                 if line == "":
55                         sys.stderr.write(sys.argv[0] + " : " + str(self.__class__.__name__) + " retrieving from address " + str(self.address) + "\n")
56                         date_mod = datetime.datetime.strptime(self.log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
57                         self.log.close()
58
59                         next_log = urllib2.urlopen(HeadRequest(self.address))
60                         date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
61                         while date_new <= date_mod:
62                                 next_log = urllib2.urlopen(HeadRequest(self.address))
63                                 date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
64
65                         self.log = urllib2.urlopen(self.address)
66                         game.setup()
67                         line = self.log.readline()
68
69
70                 return line
71                         
72         def close(self):
73                 self.log.close()
74                                                 
75 def log(s):
76         if log_file != None:
77                 log_file.write(s)
78                 
79
80 def log_init(board, players):
81         if log_file != None:
82                 log_file.setup(board, players)
83

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