Changeset - 686166c7d5bc
[Not reviewed]
default
0 13 0
Laman - 3 years ago 2022-03-05 18:08:48

added more whitespace
10 files changed with 123 insertions and 80 deletions:
0 comments (0 inline, 0 general)
src/diana/diana.py
Show inline comments
 
@@ -15,8 +15,10 @@ def collectMoves(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)
 
		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]
 
@@ -61,11 +63,13 @@ W: {PW} {WR}
 
		# draw current state
 
		for lineNumber,line in enumerate(self._game.board):
 
			for itemNumber,item in enumerate(line):
 
				if item!=EMPTY: template.addStone(itemNumber,lineNumber,"b" if item==BLACK else "w")
 
				if item != EMPTY:
 
					template.addStone(itemNumber, lineNumber, "b" if item==BLACK else "w")
 

	
 
		# draw the moves
 
		for k in range(start,end):
 
			if k>=len(self._moves): break
 
			if k >= len(self._moves):
 
				break
 

	
 
			color,move=self._moves[k]
 
			if move==tuple():
 
@@ -75,8 +79,10 @@ W: {PW} {WR}
 
				(c,r)=move
 

	
 
			if not self._move(color,c,r):
 
				if cfg.keepBroken: continue
 
				else: return False
 
				if cfg.keepBroken:
 
					continue
 
				else:
 
					return False
 

	
 
			# draw the move
 
			template.addMove(c,r,color,k+1)
 
@@ -100,7 +106,8 @@ W: {PW} {WR}
 

	
 
		for i in range(k):
 
			(color,move)=self._moves[i]
 
			if move==tuple(): continue # pass
 
			if move == tuple():
 
				continue # pass
 
			self._move(color,*move)
 

	
 
	def _move(self,color,c,r):
 
@@ -131,4 +138,5 @@ def main():
 
		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))
 
		else:
 
			print("the '{0}' path could not be resolved to either a file nor a directory".format(item))
src/diana/drawer/svg.py
Show inline comments
 
@@ -3,20 +3,12 @@ from .base import Base
 

	
 
def adjustFont(base,text):
 
	text=str(text)
 
	if len(text)<2: return round(0.7*base)
 
	elif len(text)<3: return round(0.55*base)
 
	else: return round(0.4*base)
 

	
 

	
 
class DiagramPoint:
 
	def __init__(self,x,y,color="",label=""):
 
		self.x=x
 
		self.y=y
 
		self.color=color
 
		self.label=label
 

	
 
	def __repr__(self):
 
		return 'DiagramPoint({0},{1},"{2}","{3}")'.format(self.x,self.y,self.color,self.label)
 
	if len(text) < 2:
 
		return round(0.7*base)
 
	elif len(text) < 3:
 
		return round(0.55*base)
 
	else:
 
		return round(0.4*base)
 

	
 

	
 
class Svg(Base):
src/diana/drawer/tikz.py
Show inline comments
 
@@ -12,7 +12,7 @@ class Tikz:
 

	
 
	'''
 

	
 
		# hvězdy
 
		# stars
 
		for i in range(3):
 
			for j in range(3):
 
				self.content+=r'''  \filldraw[fill=black] ({0}\boardSquare,{1}\boardSquare) circle[radius=0.04];'''.format(6*i+3, 6*j+3)+'\n'
 
@@ -30,7 +30,8 @@ class Tikz:
 
	def drawMove(self,x,y,label,color):
 
		fill="black" if color=="b" else "white"
 
		labelColor="white" if color=="b" else "black"
 
		if (not self.highNumbers) and isinstance(label,int) and label%100!=0: label=label%100 # dost neobratná logika
 
		if (not self.highNumbers) and isinstance(label, int) and label%100 != 0:
 
			label = label%100
 

	
 
		self.content+=r'  \filldraw[draw=black,fill={0}] ({1}\boardSquare,{2}\boardSquare) circle[radius=0.5\boardSquare] node[color={3}]{{{4}}};'.format(fill,x,18-y,labelColor,label)+'\n'
 

	
src/diana/go.py
Show inline comments
 
@@ -11,13 +11,15 @@ class Go:
 
		self.moveCount=0
 
	
 
	def move(self,color,y,x):
 
		if self.board[x][y]!=EMPTY: return False
 
		if self.board[x][y] != EMPTY:
 
			return False
 

	
 
		self.board[x][y]=color
 

	
 
		for i,j in ((-1,0),(1,0),(0,-1),(0,1)):
 
			self.temp=[[False]*19 for i in range(19)]
 
			if not self._floodFill(-color,x+i,y+j): self._remove()
 
			if not self._floodFill(-color, x+i, y+j):
 
				self._remove()
 
		self.temp=[[False]*19 for i in range(19)]
 
		if not self._floodFill(color,x,y):
 
			self.board[x][y]=EMPTY
 
@@ -26,14 +28,23 @@ class Go:
 
		return True
 

	
 
	def _floodFill(self,color,x,y):
 
		if x<0 or x>18 or y<0 or y>18: return False
 
		if self.temp[x][y]: return False
 
		if self.board[x][y]==EMPTY: return True
 
		if self.board[x][y]!=color: return False
 
		if x < 0 or x > 18 or y < 0 or y > 18:
 
			return False
 
		if self.temp[x][y]:
 
			return False
 
		if self.board[x][y] == EMPTY:
 
			return True
 
		if self.board[x][y] != color:
 
			return False
 
		self.temp[x][y]=True
 
		return self._floodFill(color,x-1,y) or self._floodFill(color,x+1,y) or self._floodFill(color,x,y-1) or self._floodFill(color,x,y+1)
 

	
 
		return self._floodFill(color, x-1, y) or \
 
			self._floodFill(color, x+1, y) or \
 
			self._floodFill(color, x, y-1) or \
 
			self._floodFill(color, x, y+1)
 
	
 
	def _remove(self):
 
		for i in range(19):
 
			for j in range(19):
 
				if self.temp[i][j]: self.board[i][j]=EMPTY
 
				if self.temp[i][j]:
 
					self.board[i][j] = EMPTY
src/diana/sgfParser/__init__.py
Show inline comments
 
def skipWhitespace(s,start):
 
	i=start
 
	while i<len(s) and s[i].isspace(): i+=1
 
	while i < len(s) and s[i].isspace():
 
		i+=1
 

	
 
	return i
 

	
 

	
 
def strRowCol(s, i):
 
	k=0
 
	r,c=0,0
 
	(r, c) = (0, 0)
 
	for (r,line) in enumerate(s.splitlines(True)):
 
		c=i-k
 
		if k+len(line)>i:
 
@@ -19,7 +21,7 @@ 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):
src/diana/sgfParser/collection.py
Show inline comments
 
@@ -7,18 +7,20 @@ class Collection:
 
	def __init__(self,s):
 
		self.gameTrees=[]
 
		i=skipWhitespace(s,0)
 
		if i>=len(s): return
 
		if i >= len(s):
 
			return
 
		elif not GameTree.fits(s,i):
 
			raise ParserError("expected a GameTree starting with '('",s,i)
 
		while GameTree.fits(s,i):
 
			i,x=GameTree.create(s,i)
 
			(i, x) = GameTree.create(s, i)
 
			self.gameTrees.append(x)
 
		if i<len(s):
 
			raise ParserError("expected EOF",s,i)
 

	
 
	def listGames(self):
 
		for tree in self.gameTrees:
 
			for game in tree.listGames(): yield game
 
			for game in tree.listGames():
 
				yield game
 

	
 

	
 
class GameTree:
 
@@ -41,20 +43,23 @@ class GameTree:
 

	
 
		y=None
 
		while Node.fits(s,i):
 
			i,x=Node.create(s,i)
 
			(i, x) = Node.create(s, i)
 
			res.nodes.append(x)
 
			if y: y.addChild(x)
 
			if y:
 
				y.addChild(x)
 
			x.parent=y
 
			y=x
 
			i=skipWhitespace(s,i)
 

	
 
		while GameTree.fits(s,i):
 
			i,x=GameTree.create(s,i)
 
			(i, x) = GameTree.create(s, i)
 
			res.branches.append(x)
 
			subroot=x.getNode(0)
 
			subroot.parent=y
 
			if y: y.addChild(subroot)
 
			if y:
 
				y.addChild(subroot)
 
			i=skipWhitespace(s,i)
 

	
 
		if i>=len(s) or s[i]!=")":
 
			raise ParserError("expected end of a GameTree marked by ')'",s,i)
 
		i=skipWhitespace(s,i+1)
 
@@ -62,7 +67,8 @@ class GameTree:
 

	
 
	## Expand multiple games into distinct GameTrees and yield each.
 
	def listGames(self):
 
		if len(self.nodes)==0: return None
 
		if len(self.nodes) == 0:
 
			return None
 
		for node in self.nodes[0].listGINodes():
 
			yield GameRecord(self._buildSubtree(node))
 

	
src/diana/sgfParser/node.py
Show inline comments
 
@@ -22,7 +22,7 @@ class Node:
 

	
 
		i=skipWhitespace(s,start+1)
 
		while Property.fits(s,i):
 
			i,x=Property.create(s,i)
 
			(i, x) = Property.create(s, i)
 
			if x.name in res.properties:
 
				log.warning(ParserWarning('duplicate "{0}" property in a node. second value ignored'.format(x.name),s,i))
 
			else:
 
@@ -31,7 +31,8 @@ class Node:
 
		return (i,res)
 

	
 
	def listGINodes(self):
 
		if self.isGINode(): yield self
 
		if self.isGINode():
 
			yield self
 
		empty=not self.isGINode()
 

	
 
		node=self
 
@@ -48,21 +49,24 @@ class Node:
 
				empty=False
 
				yield node
 
			queue.extend(node.children)
 
		if empty: yield self # always yield at least self, can work as GINode as well as any other
 
		if empty:
 
			yield self  # always yield at least self, can work as GINode as well as any other
 

	
 
	def isGINode(self):
 
		return any(prop.type==GAME_INFO for prop in self.properties.values())
 

	
 
	def setProp(self,name,value):
 
		self.properties[name]=value
 
		# zkontrolovat typ value
 
		# check value type
 

	
 
	def setChildren(self,children):
 
		self.children=children
 
		for child in children: child.parent=self
 
		for child in children:
 
			child.parent = self
 

	
 
	def addChild(self,node):
 
		if node in self.children: return node
 
		if node in self.children:
 
			return node
 
		node.parent=self
 
		self.children.append(node)
 
		return node
 
@@ -90,8 +94,10 @@ class Node:
 
		return res
 

	
 
	def getProp(self,name,default=None):
 
		if name in self.properties: return self.properties[name].value
 
		else: return default
 
		if name in self.properties:
 
			return self.properties[name].value
 
		else:
 
			return default
 

	
 
	## Returns textual representation of the Node itself, but disregards its children.
 
	def __str__(self):
 
@@ -103,8 +109,9 @@ class Node:
 
		output=[]
 

	
 
		while len(stack)>0:
 
			node,left,right=stack.pop()
 
			if left>0: output.append("("*left)
 
			(node, left, right) = stack.pop()
 
			if left > 0:
 
				output.append("("*left)
 
			output.append(str(node)+"\n")
 

	
 
			childCount=len(node.children)
 
@@ -115,6 +122,6 @@ class Node:
 
			else: # a branching node
 
				# first child pops first, last child closes parent's parentheses
 
				children=zip(node.children,[1]*childCount,[1]*(childCount-1)+[1+right])
 
				stack.extend(reversed(children))
 
				stack.extend(reversed(list(children)))
 

	
 
		return "".join(output)
src/diana/sgfParser/propValues.py
Show inline comments
 
@@ -48,9 +48,10 @@ def choose(*vTypes):
 
	def f(s,start):
 
		for vType in vTypes:
 
			try:
 
				i,x=vType(s,start)
 
				(i, x) = vType(s, start)
 
				return (i,x)
 
			except ParserError: pass
 
			except ParserError:
 
				pass
 
		raise ParserError("no variant of a 'choose' property value matched",s,start)
 
	return f
 

	
 
@@ -67,7 +68,7 @@ def singleton(vType):
 
	def f(s,start):
 
		if not singletonFits(s,start):
 
			raise ParserError("expected a property value starting with '['",s,start)
 
		i,x=vType(s,start+1)
 
		(i, x) = vType(s, start+1)
 
		if not singletonEnds(s,i):
 
			raise ParserError("expected a property value ending with ']'",s,i)
 
		i=skipWhitespace(s,i+1)
 
@@ -84,10 +85,10 @@ def listOf(vType,allowEmpty=False):
 
			i=skipWhitespace(s,i+2)
 
			return (i,[])
 
		single=singleton(vType)
 
		i,x=single(s,i)
 
		(i, x) = single(s, i)
 
		res=[x]
 
		while singletonFits(s,i):
 
			i,x=single(s,i)
 
			(i, x) = single(s, i)
 
			res.append(x)
 
		return (i,res)
 
	return f
 
@@ -95,24 +96,26 @@ def listOf(vType,allowEmpty=False):
 

	
 
def compose(vTypeA,vTypeB):
 
	def f(s,start):
 
		i,a=vTypeA(s,start)
 
		(i, a) = vTypeA(s, start)
 
		if i>=len(s) or s[i]!=":":
 
			raise ParserError("expected a composed property value separated by ':'",s,i)
 
		i,b=vTypeB(s,i+1)
 
		(i, b) = vTypeB(s, i+1)
 
		return (i,Composed(a,b))
 
	return f
 

	
 

	
 
def number(s,start):
 
	m=Regexp.number.match(s,start)
 
	if m is None: raise ParserError("expected a number matching '{0}'".format(Regexp.number.pattern),s,start)
 
	if m is None:
 
		raise ParserError("expected a number matching '{0}'".format(Regexp.number.pattern), s, start)
 
	res=int(m.group(0))
 
	return (m.end(),res)
 

	
 

	
 
def real(s,start):
 
	m=Regexp.real.match(s,start)
 
	if m is None: raise ParserError("expected a real number matching '{0}'".format(Regexp.real.pattern),s,start)
 
	if m is None:
 
		raise ParserError("expected a real number matching '{0}'".format(Regexp.real.pattern), s, start)
 
	res=float(m.group(0))
 
	return (m.end(),res)
 

	
 
@@ -148,13 +151,14 @@ def text(simple=True,composed=False):
 
	return f
 

	
 

	
 
def empty(s,start): return (start,"")
 
def empty(s, start):
 
	return (start, "")
 

	
 

	
 
def anything(s,start):
 
	esc=False
 
	i=start
 
	for i,c in enumerate(s[start:],start):
 
	for (i, c) in enumerate(s[start:], start):
 
		if esc: esc=False
 
		elif c=="\\": esc=True
 
		elif c=="]": break
 
@@ -164,7 +168,8 @@ def anything(s,start):
 
# go specific
 
def point(s,start):
 
	m=Regexp.point.match(s,start) # !! limit to board size
 
	if m is None: raise ParserError("expected a point value matching '{0}'".format(Regexp.point.pattern),s,start)
 
	if m is None:
 
		raise ParserError("expected a point value matching '{0}'".format(Regexp.point.pattern), s, start)
 
	if m.group(0)=="": # pass, !! tt
 
		return (m.end(),tuple())
 
	col=m.group(0)[0]
src/diana/sgfParser/property.py
Show inline comments
 
@@ -28,13 +28,13 @@ class Property:
 
	def create(s,start):
 
		assert Property.fits(s,start)
 
		res=Property()
 
		i,res.name=Property.ident(s,start)
 
		(i, res.name) = Property.ident(s, start)
 
		i=skipWhitespace(s,i)
 
		try:
 
			i,x=Property.createValue(s,i,res.name)
 
		except ParserError as e: # malformed value
 
			(i, x) = Property.createValue(s, i, res.name)
 
		except ParserError as e:  # a malformed value
 
			log.warning(e)
 
			i,x=choose(listOf(anything), singleton(anything))(s,i)
 
			(i, x) = choose(listOf(anything), singleton(anything))(s, i)
 
			res.name="_"+res.name
 
		res.value=x
 
		if res.name=="DT":
 
@@ -45,7 +45,8 @@ class Property:
 
	@staticmethod
 
	def ident(s,start):
 
		m=Property.identRegexp.match(s,start)
 
		if m is None: raise ParserError("expected a property identifier matching '[A-Z]+'",s,start)
 
		if m is None:
 
			raise ParserError("expected a property identifier matching '[A-Z]+'", s, start)
 
		return (m.end(),m.group())
 

	
 
	@staticmethod
 
@@ -59,8 +60,10 @@ class Property:
 
	@property
 
	def type(self):
 
		gameInfo={"AN","BR","BT","CP","DT","EV","GN","GC","ON","OT","PB","PC","PW","RE","RO","RU","SO","TM","US","WR","WT"}
 
		if self.name in gameInfo: return GAME_INFO
 
		else: return UNKNOWN
 
		if self.name in gameInfo:
 
			return GAME_INFO
 
		else:
 
			return UNKNOWN
 

	
 
	def copy(self):
 
		res=Property()
 
@@ -70,7 +73,9 @@ class Property:
 

	
 
	def __str__(self):
 
		name=self.name.lstrip("_")
 
		val="[{0}]".format(self.value) if not isinstance(self.value,list) else "".join("[{0}]".format(x) for x in self.value)
 
		val = "[{0}]".format(self.value) \
 
			if not isinstance(self.value, list) \
 
			else "".join("[{0}]".format(x) for x in self.value)
 
		return "{0}{1}".format(name,val)
 

	
 
	patterns={
 
@@ -179,9 +184,14 @@ class DateProperty(Property):
 
		if len(tokens)==3:
 
			return ("YMD",date(*num_tokens))
 
		elif len(tokens)==2:
 
			if len(tokens[0])==4: return ("YM",date(*num_tokens,1))
 
			else: return ("MD",date(prev.year,*num_tokens))
 
			if len(tokens[0]) == 4:
 
				return ("YM", date(*num_tokens, 1))
 
			else:
 
				return ("MD", date(prev.year, *num_tokens))
 
		else:
 
			if len(tokens[0])==4: return ("Y",date(*num_tokens,1,1))
 
			elif prevFormat in ("YM","M"): return ("M",date(prev.year,*num_tokens,1))
 
			else: return ("D",date(prev.year,prev.month,*num_tokens))
 
			if len(tokens[0]) == 4:
 
				return ("Y", date(*num_tokens, 1, 1))
 
			elif prevFormat in ("YM","M"):
 
				return ("M", date(prev.year, *num_tokens, 1))
 
			else:
 
				return ("D", date(prev.year, prev.month, *num_tokens))
src/diana/tests/testSgfParser.py
Show inline comments
 
@@ -35,7 +35,7 @@ class TestProperty(TestCase):
 
		with self.assertRaises(AssertionError):
 
			Property.create("99[99]",0)
 

	
 
		i,prop=Property.create("MN[99]",0)
 
		(i, prop) = Property.create("MN[99]", 0)
 
		self.assertNotEqual((i,prop), (0,None))
 
		self.assertEqual((i,prop.name), (6,"MN"))
 

	
 
@@ -102,5 +102,6 @@ class TestCollection(TestCase):
 
		with open(os.path.join(dataDir, "kogos.sgf")) as f:
 
			Collection(f.read())
 

	
 

	
 
if __name__ == '__main__':
 
	unittest.main()
0 comments (0 inline, 0 general)