webgo/helper.py

142 lines
3.2 KiB
Python
Raw Normal View History

import string,re,time
DEBUG = 1
2005-09-06 19:52:46 +02:00
def header():
"""return html header"""
data = """
<html>
<head>
<title>WebGo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<link rel="stylesheet" media="screen" href="default.css" type="text/css" />
</head>
<h1> WebGo </h1>
<body>
2005-09-06 19:52:46 +02:00
"""
return data
def debug(req,form, optstr = ""):
"""
print various debug infos, e.g. form content.
gets request, util.FieldStorage form, optional extra string.
returns nothing, writes with request.write.
"""
if DEBUG:
if optstr == "":
req.write("Debug: "+str(form.keys())+"<br>\n")
else:
req.write("Debug: "+optstr+"<br>\n")
2005-09-06 19:52:46 +02:00
def footer():
"""return html footer"""
data = """
</body></html>
"""
return data
# create a unique session id
def generate_session_id():
import md5, time, base64, random, string
m = md5.new()
m.update(str(time.time()))
m.update(str(random.random()))
s = string.replace(base64.encodestring(m.digest())[:-3], '/', '$')
return s
2005-09-06 19:52:46 +02:00
def generate_game_name():
from whrandom import choice
chars = string.letters
name = ''
for i in range(16):
name = name + choice(chars)
return name.lower()
2005-09-06 19:52:46 +02:00
def check_for_int(data):
"""
gets a string. if string is an integer: return integer.
else return given string.
"""
#check if value is int
num = [n for n in data if n.isdigit()]
tmp = "".join(num)
if tmp == data:
ret = int(data)
else:
ret = data
return ret
def string_to_tuple(str):
"""
gets a string. If the string contains '(',')' and ',', then return
a tuple processed from the string. If the partial string is empty, then
-1 will be returned for that value.
"""
if (str[0] =='(') and (str[-1] ==')') and (string.find(str,',')):
splitlist = string.split(str[1:-1],",")
returnlist = []
for item in splitlist:
try:
returnlist.append(int(item))
except: #empty string
returnlist.append(-1)
return tuple(returnlist)
def dict_coords_to_gnugo_coords(coords,size):
"""
gets a (x,y) coordinate tuple and boardsize.
returns a string in gnugo syntax. examples:
gets (1,1), returns "A7".
gets (6,2), returns "B2".
"""
letterlist = [" "]
letterlist.extend(list(string.letters[26:]))
letterlist.remove("I")
letter = letterlist[coords[1]]
digit = size+1-coords[0]
return letter+str(digit)
def format_time(mytime):
"""
gets output of time.time()
formats it nicely and returns as string.
"""
acttime = int(time.time())
if mytime == None:
retstring = ""
elif acttime-mytime<86400: #given time is less than 24h ago
tmp = acttime-mytime
hour = int(tmp/3600)
minute = int((tmp-hour*3600)/60)
if hour == 0:
retstring = "%dm ago" % (minute)
else:
retstring = "%dh %dm ago" % (hour,minute)
else:
mytime = int(mytime)
retstring = time.strftime("%Y-%m-%d %H:%M",time.gmtime(mytime))
return retstring
def clean_list(l):
#removing None-entries
clean = []
for item in l:
if (item != "None") and (item != None):
clean.append(item)
return clean
def test():
print dict_coords_to_gnugo_coords((6,5),7)
print format_time(time.time()-20000)
if __name__ == "__main__":
test()