Files
@ c705c709565b
Branch filter:
Location: Diana/src/diana.py - annotation
c705c709565b
3.2 KiB
text/x-python
enhanced SVG, added pleb.svg circumventing a rendering bug in librsvg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 c705c709565b c705c709565b 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 c705c709565b 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 c705c709565b 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 2a5d1585e748 | import os
import re
import sys
from jinja2 import Environment,FileSystemLoader
import go
from go import BLACK,WHITE,EMPTY
from sgfParser.collection import Collection
from drawer.svg import Svg
from drawer.tikz import Tikz
templateDir=os.path.join(os.path.dirname(__file__),"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
def collectMoves(root):
node=root
while len(node.children)>0:
b=node.getProp("B")
w=node.getProp("W")
if b is not None: yield ("b",b)
elif w is not None: yield ("w",w)
# else: yield None # !! not really robust
node=node.children[0]
def processFile(fileName):
shortName="".join(re.split(r'[/\\]',fileName)[-1].split('.')[:-1])
game=go.Go()
global t
games=Collection(open(fileName, 'r', encoding="utf-8").read()).listGames()
record=list(games)[0]
moves=list(collectMoves(record))
localBoard=dict()
overlays=""
letters=dict()
letter='a'
diagramsNeeded=(len(moves)-minMovesPerDiagram)//movesPerDiagram+1
moveNumber=0
for i in range(diagramsNeeded):
# initialize the diagram
diagram=Svg()
for lineNumber,line in enumerate(game.board):
for itemNumber,item in enumerate(line):
if item==BLACK: diagram.addStone(itemNumber,lineNumber,"b")
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):
# draw the moves
if moveNumber>=len(moves): break
c,(x,y)=moves[moveNumber]
c=c.lower()
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
# draw the move on an empty intersection
if not (x,y) in localBoard:
localBoard[(x,y)]=moveNumber+1
diagram.addMove(x,y,c,moveNumber+1)
# intersection occupied by an unlabeled stone
elif localBoard[(x,y)]<1:
# intersection not labeled even by a letter
if not (x,y) in letters:
letters[(x,y)]=letter
color='b' if localBoard[(x,y)]==EMPTY else 'w'
diagram.addMove(x,y,color,letter)
letter=chr(ord(letter)+1)
overlays+="{0} = {1}\n".format(moveNumber+1,letters[(x,y)])
# intersection occupied by a numbered stone
else: overlays+="{0} = {1}\n".format(moveNumber+1,localBoard[(x,y)])
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.write(diagram.render(env.get_template("templ.svg")))
t.close()
notes=open(os.path.join("out","{0}.txt".format(shortName)),'w')
notes.write(overlays)
notes.close()
print("processing:")
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))
else: print("the '{0}' path could not be resolved to either a file nor a directory".format(item))
|