def expand_node(self, parent_node): log.trace("expand_node", parent_node) run = self.get_current_run() if not run: return # Get the folder - which is in data parent_info = self.fs_tree.GetItemPyData(parent_node) log.debug("expanding node", parent_info) if parent_info.expanded: log.debug("Already expanded") return if parent_info.type == "F": log.debug("File node (no children)") return # Clean out the dummy sub-node self.fs_tree.DeleteChildren(parent_node) # Now add the subnodes (Only up to the currently selected run files = self.db.list_dir_id(parent_info.fs_id, run_id=run.run_id) for name, item in files.iteritems(): if parent_info.fs_id == 0 and name == "/": continue # The type may be None because this could be a folder that # we log, but dont back up. type = item.type if type is None: type = "D" # We dont add in deleted files if type != "X": node_name = utils.display_escape(name) new_node = self.fs_tree.AppendItem(parent_node, node_name, image=0 if type == "D" else 1) new_info = node_info(parent_info.fs_id, item.fs_id, type, False, os.path.join(parent_info.path, name)) log.debug("New node: ", new_info) self.fs_tree.SetItemPyData(new_node, new_info) # For any folders, add a dummy sub-node so we can expand it later if type == "D": self.fs_tree.AppendItem(new_node, DummyTreeNode) # Update the parent node to show that its been expanded. upd_info = node_info(parent_info.parent_id, parent_info.fs_id, parent_info.type, True, parent_info.path) self.fs_tree.SetItemPyData(parent_node, upd_info) self.fs_tree.SortChildren(parent_node)
def load_files(self, limit): with ProgressDialog(self, _("Loading"), _("Loading run files.\nPlease wait...")): self.lstFiles.DeleteAllColumns() self.lstFiles.DeleteAllItems() self.lstFiles.InsertColumn(0, _("Path")) self.lstFiles.InsertColumn(1, _("Size")) self.lstFiles.InsertColumn(2, _("Mod Time")) self.lstFiles.Freeze() try: files = self.db.run_contents(self.run.run_id, limit) for file in files: wx.Yield() if file.type == "F": size = utils.readable_form(file.size) mod_time = file.mod_time elif file.type == 'D': size = _("Folder") mod_time = file.mod_time elif file.type == 'X': size = _("(deleted)") mod_time = "" else: size = "ERROR: Bad type" path = os.path.join(self.get_path(file.parent_id), file.name) item = (utils.display_escape(path), size, mod_time) self.lstFiles.Append(item) self.lstFiles.SetColumnWidth(0, wx.LIST_AUTOSIZE) self.lstFiles.SetColumnWidth(1, wx.LIST_AUTOSIZE) self.lstFiles.SetColumnWidth(2, wx.LIST_AUTOSIZE) finally: self.lstFiles.Thaw() if self.lstFiles.GetItemCount() < limit: # There probably aren't that many files. self.pnlAllFiles.Hide() self.pnlFiles.Fit()
def rebuild_node(self, run, node): ''' Given a node, we want to correct it's state as at the given run. We do this as non-destructively as possible, so that the tree remains in shape. We are correcting the CHILDREN of the current node, so if the node is a leaf - we quit (it should have been corrected when correcting its parent) @param run: @type run: @param node: @type node: ''' node_data = self.fs_tree.GetItemPyData(node) # If this is NOT a folder node, exit. if node_data.type != "D": log.debug("Node not directory") return # If this node has not been expanded, exit if not node_data.expanded: log.debug("Node not expanded") return log.debug("rebuild_node", run, node, node_data) # From the DB, get all fs entries under the current one dbentries = self.db.list_dir_id(node_data.fs_id, run.run_id) if not dbentries: # This node has no children self.fs_tree.DeleteChildren(node) log.debug("Node has no children") return # # Get the child list - we have to get this in advance because our changes to # # the tree will cause iterators to fail # (child, cookie) = self.fs_tree.GetFirstChild(node) # For each of its children children_ids = [] (child, cookie) = self.fs_tree.GetFirstChild(node) while child: children_ids.append(child) child, cookie = self.fs_tree.GetNextChild(node, cookie) for child in children_ids: # Get information on this node. child_name = self.fs_tree.GetItemText(child) log.debug("Got ", child_name) child_data = self.fs_tree.GetItemPyData(child) if not child_data: raise Exception("Illegal: empty child node") log.debug("Visiting node:", child_name, child_data) # Fix the node. # If the node type is right, then we will leave it in. # Get the DB node: try: if child_name in dbentries: # Exists in tree AND in DB. Make sure the types are right db_data = dbentries[child_name] if db_data.type == 'X': log.debug("Type X = delete", child_name) self.fs_tree.Delete(child) elif db_data.type == 'D' and child_data.type == 'F': new_info = node_info(db_data.parent_id, db_data.fs_id, db_data.type, False, os.path.join(node_data.path, child_name)) log.debug("New node: ", new_info) self.fs_tree.SetItemPyData(child, new_info) # Adding a DIR node, so add a dummy to ensure it shows properly. self.fs_tree.AppendItem(child, DummyTreeNode) # TODO. change the icon from dir to file. elif db_data.type == 'F' and child_data.type == 'D': # Fix new_info = node_info(db_data.parent_id, db_data.fs_id, db_data.type, False, os.path.join(node_data.path, child_name)) log.debug("New node: ", new_info) self.fs_tree.SetItemPyData(child, new_info) self.fs_tree.DeleteChildren(child) # TODO. change the icon from file to dir. else: # The tree and DB nodes agree pass # Check on the children log.debug("Recursing") self.rebuild_node(run, child) log.debug("Completed node ", child_name) # This node has been completely fixed del dbentries[child_name] else: # NOTE IN DB log.debug("Entry no longer exists. Removing from tree") self.fs_tree.Delete(child) except Exception as e: log.error("Exception on %s: %s" % (child_name, str(e))) # Next node log.debug("Items Left:", dbentries) # Now add any missing items back in for name, db_data in dbentries.iteritems(): # Special case - watch for root if db_data.fs_id == 0: continue if db_data.type is None: type = 'D' else: type = db_data.type if db_data.type == 'X': continue node_name = utils.display_escape(name) new_node = self.fs_tree.AppendItem(node, node_name, image=0 if type == "D" else 1) new_info = node_info(db_data.parent_id, db_data.fs_id, type, False, os.path.join(node_data.path, name)) log.debug("New node: ", new_info) self.fs_tree.SetItemPyData(new_node, new_info) # For any folders, add a dummy sub-node so we can expand it later if type == "D": self.fs_tree.AppendItem(new_node, DummyTreeNode) # Ensure the nodes keep roughly the same position/order self.fs_tree.SortChildren(node)