From 5ab93abaf2332f3a14daf72ee4e1a2a9b5df0a7b Mon Sep 17 00:00:00 2001 From: phear Date: Sun, 9 Oct 2005 13:58:06 +0000 Subject: [PATCH] added a simple parser for static sgf files --- filehandling.py | 9 ++---- helper.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/filehandling.py b/filehandling.py index a2a1350..f6b5697 100644 --- a/filehandling.py +++ b/filehandling.py @@ -1,12 +1,6 @@ import os,tempfile,dircache,string,commands -def gen_temp_dir(prefix=None): - """generate a secure temporary directory. returns name of dir.""" - dirname=tempfile.mkdtemp("",prefix) - dirname+="/" - return dirname - def read_file(filename): """ @@ -82,4 +76,5 @@ def gen_temp_file(suffix="--gnugo"): returns the name of a generated temporay file. optionally gets a suffix for the filename. """ - return tempfile.mkstemp(suffix)[1] \ No newline at end of file + return tempfile.mkstemp(suffix)[1] + diff --git a/helper.py b/helper.py index 62cf77e..556fb04 100644 --- a/helper.py +++ b/helper.py @@ -1,4 +1,4 @@ -import string +import string,re DEBUG = 1 @@ -100,8 +100,84 @@ def dict_coords_to_gnugo_coords(coords,size): digit = size+1-coords[0] return letter+str(digit) + +def parse_static_gnugo_sgf(s): + """ + gets a string containing the data saved by the gnugo "printsgf" order. + """ + #create a list of letters + letters = [" "] + letters.extend(list(string.letters)[0:26]) + + #dicitonary to return + ret = {} + + #removing newlines vom given string + s = s.replace("\r\n","") #windoze + s = s.replace("\n","") #unix + + #getting the board size + # looks like SZ[19] + # the pattern finds a number ([0-9]) with length (1-2) between "SZ[" and "]" + boardsize = re.search("SZ\[([0-9]{1,2})\]",s).groups()[0] + boardsize = int(boardsize) + ret["size"] = boardsize + + + #some regexp patterns: + #(?: ...) groups items + stones = "%s((?:\[[a-z][a-z]\])*)" #dont forget to set %s! + whitestones = stones % "AW" + blackstones = stones % "AB" + stonestolist = "\[([a-z][a-z])\]" + + #getting white stones + #looks like AW[bb][cb][cc][cd][de][df] + + stonestring = re.search(whitestones,s).groups()[0] + rawlist = re.findall(stonestolist,stonestring) + for item in rawlist: + #fill in specific values + ret[(letters.index(item[0]),letters.index(item[1]))] = 1 + + + #getting black stones + stonestring = re.search(blackstones,s).groups()[0] + rawlist = re.findall(stonestolist,stonestring) + for item in rawlist: + #fill in specific values + ret[(letters.index(item[0]),letters.index(item[1]))] = 2 + + #who's turn is it? + if string.find(s,"PL[B]")>0: + ret["player"] = "black" + else: + ret["player"] = "white" + + return ret + + + + def test(): print dict_coords_to_gnugo_coords((6,5),7) + + #sample output + s = """(; +GM[1] +FF[4] +SZ[19] +GN[GNU Go 3.7.4 load and print] +DT[2005-10-09] +KM[5.5] +RU[Japanese] +AP[GNU Go:3.7.4] +AW[bb][cb][cc][cd][de][df] +[ag][cg][ah][ch][dh][ai][bi][ci] +AB[ba][ab][ac][bc][bd][be][ce][af][cf][bg][bh] +PL[B] +)""" + print parse_static_gnugo_sgf(s) if __name__ == "__main__": test()