Ejemplo n.º 1
0
	def __init__(self, guid, label, parent):
		assert type(label) == unicode
		assert (not parent) or isinstance(parent, Tree)
		self.guid   = guid
		self.label  = label
		self.parent = parent
		self.render = ItemRender(
			fonts.get_path('LiberationSerif-Regular'), 27, (2, 0)
		)
Ejemplo n.º 2
0
class Tree(object):
	guid     = None # string used when querying the CM for stats, listings, etc
	label    = None # string: the item's pretty printed identity
	parent   = None # another Tree node
	children = None # list of Tree nodes
	render   = None # Render object that knows how to draw this Tree node

	def __init__(self, guid, label, parent):
		assert type(label) == unicode
		assert (not parent) or isinstance(parent, Tree)
		self.guid   = guid
		self.label  = label
		self.parent = parent
		self.render = ItemRender(
			fonts.get_path('LiberationSerif-Regular'), 27, (2, 0)
		)

	def __str__(self):
		return unicode(json.dumps(self.dump(),indent=4))
	
	def __cmp__(self, other):
		if self.guid == other.guid:
			return 0
		if self.guid < other.guid:
			return -1
		return 1

	def __eq__(self, other):
		if not other:
			return False
		if type(self) != type(other):
			return False
		return self.guid == other.guid

	def __ne__(self, other):
		return not self.__eq__(other)

	def dump(self):
		if self.parent:
			parent = self.parent.dump()
		else:
			parent = None
		return {
			'class' : unicode(type(self)),
			'guid'  : self.guid,
			'label' : self.label,
			'parent': parent
		}

	def get_pretty(self):
		return self.label

	def curry(self):
		self.render.curry(self)
		return (self.guid, self.render)

	def ticker(self):
		return (self.guid, self.render)

	def ls(self):
		return self.children
	
	def next(self, wrap=False, rand=False):
		if not self.parent:
			return None
		try:
			if rand:
				index = random.randint(0, len(self.parent.children)-1)
			else:
				index = self.parent.children.index(self) + 1
				if wrap:
					index %= len(self.parent.children)
			return self.parent.children[index]
		except:
			return None

	def prev(self, wrap=False, rand=False):
		if not self.parent:
			return None
		try:
			if rand:
				index = random.randint(0, len(self.parent.children)-1)
			else:
				index = self.parent.children.index(self) - 1
				if wrap:
					index %= len(self.parent.children)
				elif index < 0:
					return None
			return self.parent.children[index]
		except:
			return None

	def is_parent_of(self, other):
		while other.parent:
			if other.parent == self:
				return True
			other = other.parent
		return False

	def up(self):
		return None
	def down(self):
		return None
	def left(self):
		return None
	def right(self):
		return None
	def add(self):
		return None
	def play(self):
		return None
	def number(self, ir_code):
		return None

	def focused(self):
		return self.parent.focused()

	def set_focus(self, item):
		self.parent.set_focus(item)