Changeset - afb861f616bf
[Not reviewed]
default
0 3 0
Laman - 6 years ago 2019-05-29 16:37:59

better configuration handling
3 files changed with 39 insertions and 20 deletions:
0 comments (0 inline, 0 general)
.hgignore
Show inline comments
 
@@ -3,3 +3,4 @@
 
^in/
 
^out/
 
\.pyc$
 
^build/
src/diana/config.py
Show inline comments
 
@@ -19,16 +19,32 @@ parser.add_argument("--moves-p-d", type=
 
parser.add_argument("--min-p-d", type=int)
 
parser.add_argument("--version", action="version", version="{0} {1}.{2}.{3}".format(progName,*version))
 

	
 

	
 
inputFiles=[]
 
encoding="utf-8-sig"
 
sgfSuffix=True
 
recursive=False
 
outputDir=curDir
 
outputFormat="svg"
 
keepBroken=False
 

	
 
movesPerDiagram=100
 
minMovesPerDiagram=10
 

	
 

	
 
def parseArgs():
 
	global inputFiles,encoding,sgfSuffix,recursive,outputDir,outputFormat,keepBroken,movesPerDiagram
 
	global minMovesPerDiagram
 

	
 
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
 
	if args.encoding: encoding=args.encoding
 
	if args.suffix: sgfSuffix=True
 
	if args.recursive: recursive=True
 
	if args.output: outputDir=args.output
 
	if args.format: outputFormat=args.format
 
	if args.keep_broken is not None: keepBroken=True
 

	
 
movesPerDiagram=args.moves_p_d or 100
 
minMovesPerDiagram=args.min_p_d or 10
 
	if args.moves_p_d: movesPerDiagram=args.moves_p_d
 
	if args.min_p_d: minMovesPerDiagram=args.min_p_d
src/diana/diana.py
Show inline comments
 
@@ -28,7 +28,8 @@ class SourceFile:
 
		self._shortName= "".join(re.split(r'[/\\]', fileName)[-1].split('.')[:-1])
 
		self._game=go.Go()
 

	
 
		games=Collection(open(self.fileName, 'r', encoding=cfg.encoding).read()).listGames()
 
		with open(self.fileName, 'r', encoding=cfg.encoding) as f:
 
			games=Collection(f.read()).listGames()
 
		self._record=list(games)[0]
 
		self._moves=list(collectMoves(self._record.root))
 

	
 
@@ -41,8 +42,13 @@ class SourceFile:
 
			self.createDiagram(k,k+cfg.movesPerDiagram).save(filename,"templ-pleb.svg")
 
			i+=1
 

	
 
		infoStr="""{GN}
 
B: {PB} {BR}
 
W: {PW} {WR}
 
{DT}
 
{RE}""".format(**self.fetchGameInfo(["GN","PB","BR","PW","WR","DT","RE"],""))
 
		notes=open(os.path.join(cfg.outputDir,"{0}.txt".format(self._shortName)), 'w')
 
		notes.write(self.createGameInfo())
 
		notes.write(infoStr)
 
		notes.close()
 
		print("done")
 

	
 
@@ -77,13 +83,8 @@ class SourceFile:
 

	
 
		return template
 

	
 
	def createGameInfo(self):
 
		rec=self._record
 
		return """{title}
 
B: {black} {bRank}
 
W: {white} {wRank}
 
{date}
 
{result}""".format(title=rec.get("GN",""), black=rec.get("PB",""), bRank=rec.get("BR",""), white=rec.get("PW",""), wRank=rec.get("WR",""), date=rec.get("DT",""), result=rec.get("RE",""))
 
	def fetchGameInfo(self,fieldNames,default=None):
 
		return {k:self._record.get(k,default) for k in fieldNames}
 

	
 
	def _setMove(self,k):
 
		self._game=go.Go()
 
@@ -92,10 +93,10 @@ W: {white} {wRank}
 
		whiteStones=self._record.root.getProp("AW")
 
		if blackStones:
 
			for p in blackStones:
 
				self._game.board[p.c][p.r]=BLACK
 
				self._game.board[p.r][p.c]=BLACK
 
		if whiteStones:
 
			for p in whiteStones:
 
				self._game.board[p.c][p.r]=WHITE
 
				self._game.board[p.r][p.c]=WHITE
 

	
 
		for i in range(k):
 
			(color,move)=self._moves[i]
 
@@ -116,6 +117,7 @@ W: {white} {wRank}
 

	
 

	
 
if __name__=="__main__":
 
	cfg.parseArgs()
 
	print("processing:")
 
	files=cfg.inputFiles[:]
 

	
0 comments (0 inline, 0 general)