Files @ 2ed7f0dab5ef
Branch filter:

Location: Diana/src/sgfParser/node.py

Laman
expanding compressed game trees
from . import skipWhitespace, ParserError
from .property import Property, GAME_INFO


class Node:
	def __init__(self):
		self.properties=dict()
		self.parent=None
		self.children=[]

	@staticmethod
	def create(s,start):
		res=Node()
		if s[start]!=";":
			# print("error when parsing Node")
			return (start,None)
		i=skipWhitespace(s,start+1)
		i,x=Property.create(s,start+1)
		while x is not None:
			if x.name in res.properties:
				print(res.properties)
				raise ParserError(0,0,'duplicate "{0}" property in node at position {1}. second value ignored'.format(x.name,start))
			else:
				res.properties[x.name]=x
			i=skipWhitespace(s,i)
			i,x=Property.create(s,i)
		return (i,res)

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

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

	def setParent(self,node):
		self.parent=node

	def setChildren(self,children):
		self.children=children

	def addChild(self,node):
		if node in self.children: return node
		self.children.append(node)
		return node

	def removeChild(self,node):
		if node not in self.children:
			return None
		del self.children[self.children.index(node)]
		return node

	def removeChildAt(self,i):
		if -len(self.children)<i<len(self.children):
			res=self.children[i]
			del self.children[i]
			return res
		return None

	## Create a copy of the Node, with deep copied propeties and shallow copied parent and children.
	def copy(self):
		res=Node()
		res.properties={k: v.copy() for (k,v) in self.properties.items()}
		res.parent=self.parent
		res.setChildren(self.children[:])
		return res

	def getProperty(self,name):
		if name in self.properties: return self.properties[name]
		else: return None