|
|
|
|
|
|
|
|
self.name = name |
|
|
self.name = name |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
|
|
|
def type(self): |
|
|
|
|
|
return NODETYPE_NODE |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
def parent(self): |
|
|
def parent(self): |
|
|
return self._parent |
|
|
return self._parent |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
raise e |
|
|
raise e |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
|
|
|
def root(self): |
|
|
|
|
|
if self._parent is None: |
|
|
|
|
|
return self |
|
|
|
|
|
return self._parent.root |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
def name(self): |
|
|
def name(self): |
|
|
return self._name |
|
|
return self._name |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def name(self, value): |
|
|
def name(self, value): |
|
|
self._name = value |
|
|
self._name = value |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
|
def full_name(self): |
|
|
|
|
|
if self._parent is None: |
|
|
|
|
|
return self._name |
|
|
|
|
|
return self._parent.full_name + "." + self._name |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
|
def child_count(self): |
|
|
|
|
|
return len(this._children) |
|
|
|
|
|
|
|
|
def parent_to_node(self, parent, allow_reparenting=False): |
|
|
def parent_to_node(self, parent, allow_reparenting=False): |
|
|
if not isinstance(value, Node): |
|
|
if not isinstance(value, Node): |
|
|
raise NodeError("Node may only parent to another Node instance.") |
|
|
raise NodeError("Node may only parent to another Node instance.") |
|
|
|
|
|
|
|
|
self._children.insert(index, node) |
|
|
self._children.insert(index, node) |
|
|
|
|
|
|
|
|
def remove_node(self, node): |
|
|
def remove_node(self, node): |
|
|
if node.parent != self: |
|
|
|
|
|
if node.parent == None: |
|
|
|
|
|
raise NodeError("Cannot remove an unparented node.") |
|
|
|
|
|
return node.parent.remove_node(node) |
|
|
|
|
|
if node in self._children: |
|
|
|
|
|
self._children.remove(node) |
|
|
|
|
|
node._parent = None |
|
|
|
|
|
return node |
|
|
|
|
|
|
|
|
if isinstance(node, (str, unicode)): |
|
|
|
|
|
n = self.get_node(node) |
|
|
|
|
|
if n is not None: |
|
|
|
|
|
try: |
|
|
|
|
|
return self.remove_node(n) |
|
|
|
|
|
except NodeError as e: |
|
|
|
|
|
raise e |
|
|
|
|
|
elif isinstance(node, Node): |
|
|
|
|
|
if node.parent != self: |
|
|
|
|
|
if node.parent == None: |
|
|
|
|
|
raise NodeError("Cannot remove an unparented node.") |
|
|
|
|
|
try: |
|
|
|
|
|
return node.parent.remove_node(node) |
|
|
|
|
|
except NodeError as e: |
|
|
|
|
|
raise e |
|
|
|
|
|
if node in self._children: |
|
|
|
|
|
self._children.remove(node) |
|
|
|
|
|
node._parent = None |
|
|
|
|
|
return node |
|
|
|
|
|
else: |
|
|
|
|
|
raise NodeError("Expected a Node instance or a string.") |
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_node(self, name): |
|
|
|
|
|
if len(self._children) <= 0: |
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
subnames = name.split(".") |
|
|
|
|
|
for c in self._children: |
|
|
|
|
|
if c.name == subnames[0]: |
|
|
|
|
|
if len(subnames) > 1: |
|
|
|
|
|
return c.get_node(".".join(subnames[1:-1])) |
|
|
|
|
|
return c |
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|