diff --git a/src/diana.py b/src/diana.py --- a/src/diana.py +++ b/src/diana.py @@ -4,26 +4,20 @@ import sys from jinja2 import Environment,FileSystemLoader +import config as cfg import go from go import BLACK,WHITE,EMPTY +from sgfParser import ParserError from sgfParser.collection import Collection - from drawer.svg import Svg from drawer.tikz import Tikz -templateDir=os.path.join(os.path.dirname(__file__),"templ") +curDir=os.path.dirname(__file__) +templateDir=os.path.join(curDir,"templ") env=Environment(loader=FileSystemLoader(templateDir)) -if len(sys.argv)>1: - files=sys.argv[1:] -else: - sys.exit("no input file specified") - -movesPerDiagram=75 -minMovesPerDiagram=10 - t=sys.stdout @@ -40,12 +34,17 @@ def collectMoves(root): def processFile(fileName): + print("{0}... ".format(fileName), end="") shortName="".join(re.split(r'[/\\]',fileName)[-1].split('.')[:-1]) game=go.Go() global t - games=Collection(open(fileName, 'r', encoding="utf-8").read()).listGames() + try: + games=Collection(open(fileName, 'r', encoding=cfg.encoding).read()).listGames() + except ParserError as e: + print("Couldn't parse {0}, following error occured: {1}".format(fileName,e)) + return False record=list(games)[0] moves=list(collectMoves(record)) @@ -55,7 +54,7 @@ def processFile(fileName): letters=dict() letter='a' - diagramsNeeded=(len(moves)-minMovesPerDiagram)//movesPerDiagram+1 + diagramsNeeded=(len(moves)-cfg.minMovesPerDiagram)//cfg.movesPerDiagram+1 moveNumber=0 for i in range(diagramsNeeded): @@ -68,17 +67,30 @@ def processFile(fileName): if item==WHITE: diagram.addStone(itemNumber,lineNumber,"w") localBoard={(a,b):game.board[b][a]-1 for a in range(19) for b in range(19) if game.board[b][a]!=EMPTY} - for j in range(movesPerDiagram): + for j in range(cfg.movesPerDiagram): # draw the moves if moveNumber>=len(moves): break - c,(x,y)=moves[moveNumber] + c,move=moves[moveNumber] c=c.lower() + if move==tuple(): + overlays+="{0} pass\n".format(moveNumber) + moveNumber+=1 + continue + else: + (x,y)=move if not game.move(BLACK if c=='b' else WHITE, x,y): - print("illegal move: {0} at {1},{2}".format(moveNumber+1,x,y)) - moveNumber+=1 - continue + # !! we do not honor http://red-bean.com/sgf/ff5/m_vs_ax.htm at the moment + msg="illegal move: {0} at {1},{2}".format(moveNumber+1,x,y) + if cfg.keepBroken: + print(msg) + moveNumber+=1 + continue + else: + msg+=". aborted" + print(msg) + return False # draw the move on an empty intersection if not (x,y) in localBoard: @@ -99,22 +111,22 @@ def processFile(fileName): moveNumber+=1 # finish and save the diagram - t=open(os.path.join("out","{0}-{1}.{2}".format(shortName,i+1,diagram.extension)),'w') # a new file + t=open(os.path.join(cfg.outputDir,"{0}-{1}.{2}".format(shortName,i+1,diagram.extension)),'w') # a new file t.write(diagram.render(env.get_template("templ.svg"))) t.close() - notes=open(os.path.join("out","{0}.txt".format(shortName)),'w') + notes=open(os.path.join(cfg.outputDir,"{0}.txt".format(shortName)),'w') notes.write(overlays) notes.close() - + print("done") + + print("processing:") +files=cfg.inputFiles[:] + for item in files: - # relativně vůči work directory nebo vůči skriptu? - # item=os.path.join(os.path.dirname(__file__),item) if os.path.isfile(item): - print("{0}... ".format(item),end="") processFile(item) - print("done") elif os.path.isdir(item): files+=[os.path.join(item,child) for child in os.listdir(item)] print("contents of the '{0}' directory added to the queue".format(item))