Changeset - 69ccf3e0aefe
[Not reviewed]
default
0 3 1
Laman - 8 years ago 2017-02-11 18:19:59

created config.py and added command line arguments
4 files changed with 81 insertions and 30 deletions:
0 comments (0 inline, 0 general)
src/config.py
Show inline comments
 
new file 100755
 
import os
 
from argparse import ArgumentParser
 

	
 

	
 
progName="DianaXO"
 
version=(0,0,0)
 

	
 
curDir=os.path.dirname(__file__)
 

	
 
parser=ArgumentParser()
 
parser.add_argument("--encoding")
 
parser.add_argument("-s","--suffix")
 
parser.add_argument("-r","--recursive")
 
parser.add_argument("-i","--input", nargs="+", required=True)
 
parser.add_argument("-o","--output")
 
parser.add_argument("--format", choices={"svg"})
 
parser.add_argument("-k","--keep-broken")
 
parser.add_argument("--moves-p-d", type=int)
 
parser.add_argument("--min-p-d", type=int)
 
parser.add_argument("--version", action="version", version="{0} {1}.{2}.{3}".format(progName,*version))
 

	
 
args=parser.parse_args()
 

	
 

	
 
inputFiles=args.input
 
encoding=args.encoding or "utf-8-sig"
 
sgfSuffix=args.suffix if args.suffix is not None else True
 
recursive=args.recursive
 
outputDir=args.output or curDir
 
outputFormat=args.format or "svg"
 
keepBroken=True if args.keep_broken is not None else False
 

	
 
movesPerDiagram=args.moves_p_d or 100
 
minMovesPerDiagram=args.min_p_d or 10
src/diana.py
Show inline comments
 
@@ -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))
src/drawer/svg.py
Show inline comments
 
from itertools import count
 

	
 

	
 
c=28
 
padding=15
 
highNumbers=True
 
@@ -32,21 +35,22 @@ class Svg:
 
		self.padding=30
 

	
 
		self._index=dict()
 
		self._indexGen=count()
 

	
 
	def addStone(self,x,y,color):
 
		self._index[(x,y)]=(len(self._index),DiagramPoint(x,y,color))
 
		self._index[(x,y)]=(next(self._indexGen),DiagramPoint(x,y,color))
 

	
 
	def addMove(self,x,y,color,label):
 
		if (not self.highNumbers) and isinstance(label,int) and label%100!=0:
 
			label%=100
 

	
 
		self._index[(x,y)]=(len(self._index),DiagramPoint(x,y,color,label))
 
		self._index[(x,y)]=(next(self._indexGen),DiagramPoint(x,y,color,label))
 

	
 
	def addLabel(self,x,y,label):
 
		self._index[(x,y)]=(len(self._index),DiagramPoint(x,y,"",label))
 
		self._index[(x,y)]=(next(self._indexGen),DiagramPoint(x,y,"",label))
 

	
 
	def render(self, template):
 
		points = [p for (i,p) in sorted(self._index.values())]
 
		points = [p for (i,p) in sorted(self._index.values(), key=lambda x: x[0])]
 

	
 
		stones = [p for p in points if p.color and not p.label]
 
		moves = [p for p in points if p.color and p.label]
src/sgfParser/__init__.py
Show inline comments
 
modified file chmod 100644 => 100755
 
@@ -19,10 +19,11 @@ def strRowCol(s, i):
 
class ParserError(Exception):
 
	def __init__(self,msg,s,i):
 
		self.msg=msg
 
		self.row,self.col=strRowCol(s, i)
 
		self.row,self.col=strRowCol(s,i)
 
		self.context=s[i:i+16]
 

	
 
	def __str__(self):
 
		return "{0} at row {1}, col {2}".format(self.msg,self.row,self.col)
 
		return "{0} at row {1}, col {2}, got '{3}...' instead".format(self.msg,self.row,self.col,self.context)
 

	
 

	
 
class ParserWarning(ParserError):
0 comments (0 inline, 0 general)