1 import logging
2 import threading
3
4 from concurrent_tree_crawler.abstract_tree_navigator import NavigationException
5
7 """
8 Wrapper for the tree navigator (L{AbstractTreeNavigator}).
9 It makes sure that accessing our tree representation
10 object L{TreeAccessor} is synchronized with actions executed on
11 L{AbstractTreeNavigator} object. It also handles L{NavigationException}s
12 that might occur while using the navigator.
13 """
14
16 """
17 @type navigator: L{AbstractTreeNavigator}
18 @type tree: L{AbstractTreeAccessor}
19 """
20 self.__nav = navigator
21 self.__tree = tree
22 self.__current_node = None
23
25 return self.__current_node
26
28 self.__current_node = self.__tree.get_sentinel()
29 self.__log("Started in sentinel")
30
32 """
33 @return: children names
34 @rtype: list of strings
35 """
36 ret = None
37 if self.__current_node == self.__tree.get_sentinel():
38 ret = ["root"]
39 else:
40 try:
41 ret = self.__nav.get_children()
42 except NavigationException as ex:
43 self.__handle_exception(ex, "Getting children failed")
44 self.__log("Got possible children")
45 return ret
46
60
68
70 """
71 @return: C{True} iff the navigator is currently in the root. It means
72 that the whole tree has been traversed.
73 """
74 ret = None
75
76
77 if self.__current_node.get_parent() is None or \
78 self.__current_node.get_parent() == self.__tree.get_sentinel():
79
80
81 self.__current_node = self.__tree.get_sentinel()
82 ret = True
83 else:
84 try:
85 self.__current_node = self.__current_node.get_parent()
86 self.__nav.move_to_parent()
87 ret = False
88 except NavigationException as ex:
89 self.__handle_exception(ex, "Moving to parent failed")
90 self.__log("Moved to parent")
91 return ret
92
105
107 self.__log_exception(error_message, ex, self.__current_node)
108 self.__tree.set_error(self.__current_node)
109 raise ex
110
112 """
113 @type error_message: string
114 @type ex: L{Exception}
115 @type error_node: L{AbstractNode}
116 """
117 path_str = "/"+("/".join(self.__tree.get_path(error_node)))
118 logging.warning("thread==\"{}\", navigation exception in node \"{}\":"
119 "{}\". Detailed message: \"{}\".".format(
120 threading.current_thread().name, path_str, error_message, ex))
121
122 - def __log(self, message):
123 """
124 @type message: string
125 """
126 path_str = "/"+("/".join(self.__tree.get_path(self.get_current_node())))
127 logging.debug("thread=\"{}\", node=\"{}\": {}".format(
128 threading.current_thread().name, path_str, message))
129