def load_from_file(): hosts = [] with open(Config.__get_or_create_config_file(), "r") as file: host_info = None for f_line in file: line = f_line.strip() if line.startswith('#') or not line: continue kv = line.split(' ') key = kv[0].lower() value = kv[1] if key == "host": if host_info is not None: hosts.append(host_info) host_info = HostInfo(value) if key == "hostname": host_info.ip = value if key == "user": host_info.username = value if key == "port": host_info.port = value if host_info is not None: hosts.append(host_info) return Config.__construct_tree(hosts)
def on_save(self, args=None): host_info = HostInfo(self.connection_name.edit_text) host_info.ip = self.ip.edit_text host_info.port = self.port.edit_text host_info.username = self.username.edit_text self.target.name = self.connection_name.edit_text self.target.hostinfo = host_info self.parent.add_child(self.target) self.on_close(self.target)
def on_save(self, args=None): host_info = HostInfo(self.connection_name.edit_text) host_info.ip = self.ip.edit_text host_info.username = self.username.edit_text host_info.port = self.port.edit_text host_info.identity_file = self.id_file.edit_text host_info.dynamic_forward = self.dynamic_forward.edit_text host_info.local_forward = (self.local_forward_from.edit_text, self.local_forward_to.edit_text) host_info.remote_forward = (self.remote_forward_from.edit_text, self.remote_forward_to.edit_text) self.target.name = self.connection_name.edit_text self.target.hostinfo = host_info self.parent.add_child(self.target) self.on_close(self.target)
def load_from_file(): hosts = [] with open(Config.__get_or_create_config_file(), "r") as file: host_info = None for f_line in file: line = f_line.strip() if line.startswith('#') or not line: continue kv = list(filter(None, line.split(' '))) key = kv[0].lower() # SSH config file keys are case insensitive try: if key == "host": if host_info is not None: hosts.append(host_info) if len(kv) == 1: host_info = None else: host_info = HostInfo(kv[1]) if key == "hostname" and host_info: host_info.ip = kv[1] if key == "user" and host_info: host_info.username = kv[1] if key == "port" and host_info: host_info.port = kv[1] if key == "identityfile" and host_info: host_info.identity_file = kv[1] if key == "dynamicforward" and host_info: host_info.dynamic_forward = kv[1] if key == "localforward" and host_info: host_info.local_forward = (kv[1], kv[2]) if key == "remoteforward" and host_info: host_info.remote_forward = (kv[1], kv[2]) except IndexError: continue if host_info is not None: hosts.append(host_info) return Config.__construct_tree(hosts)
def keypress(self, size, key): """allow subclasses to intercept keystrokes""" key = self.__super.keypress(size, key) this_node = self.get_node().get_value() parent_node = this_node if (self.get_node().get_parent() is None or this_node.nodetype == "folder") \ else self.get_node().get_parent().get_value() if key == "enter": if isinstance(self.get_node(), UITreeNode): close_ui_and_run(this_node.hostinfo.get_ssh_command()) if key in ("-", "left") and not self.is_leaf: self.expanded = False self.update_expanded_icon() elif key == "f5" and self.is_leaf: if package_available(package_name="mc"): close_ui_and_run(this_node.hostinfo.get_mc_command()) else: MessageDialog( State, "Error", "Please install mc (Midnight Commander) package" " to use this feature", reset_layout).show() elif key == "f6": AddFolderDialog(State, parent_node, Node("", "folder"), reload_data).show() elif key == "f7": AddHostDialog(State, parent_node, Node("", "session", HostInfo("")), reload_data).show() elif key == "f8": if this_node.nodetype == "folder": # TODO implement removing folder MessageDialog(State, "Error", "Folders can not be removed", reload_data).show() else: RemoveHostDialog(State, parent_node, this_node, reload_data).show() elif key == "f9" and self.is_leaf: AddHostDialog(State, parent_node, this_node, reload_data).show() elif key in ('q', 'Q'): close_ui_and_exit() return key
def on_key_press(self, key: str, w: UITreeWidget): this_node = w.get_node().get_value() folder_node = this_node if (w.get_node().get_parent() is None or this_node.nodetype == "folder") \ else w.get_node().get_parent().get_value() parent_node = None if w.get_node().get_parent( ) is None else w.get_node().get_parent().get_value() if key in 'qQ': self.command = EXIT_REDIAL raise urwid.ExitMainLoop() elif key == "enter": if isinstance(w.get_node(), UITreeNode): self.command = w.get_node().get_value( ).hostinfo.get_ssh_command() raise urwid.ExitMainLoop() elif key == "f5" and w.is_leaf: if package_available(package_name="mc"): self.command = this_node.hostinfo.get_mc_command() raise urwid.ExitMainLoop() else: MessageDialog( "Error", "Please install mc (Midnight Commander) package" " to use this feature", self.close_dialog).show(self.loop) elif key == "f6": AddFolderDialog(folder_node, Node("", "folder"), self.save_and_focus).show(self.loop) elif key == "f7": AddHostDialog(folder_node, Node("", "session", HostInfo("")), self.save_and_focus).show(self.loop) elif key == "f8": if this_node.nodetype == "folder": # TODO implement removing folder MessageDialog("Error", "Folders can not be removed", self.close_dialog).show(self.loop) else: RemoveHostDialog(parent_node, this_node, self.save_and_focus).show(self.loop) elif key == "f9" and w.is_leaf: AddHostDialog(parent_node, this_node, self.save_and_focus).show(self.loop) elif key in ["meta down", "ctrl down"]: if parent_node is None: return i = parent_node.children.index(this_node) if i == len(parent_node.children) - 1: return # at bottom parent_node.children[i], parent_node.children[ i + 1] = parent_node.children[i + 1], parent_node.children[i] Config.save_to_file(self.sessions) self.walker.set_focus( UIParentNode(self.sessions, key_handler=self.on_key_press)) self.listbox.set_focus_to_node(this_node) elif key in ["meta up", "ctrl up"]: if parent_node is None: return i = parent_node.children.index(this_node) if i == 0: return # at top parent_node.children[i], parent_node.children[ i - 1] = parent_node.children[i - 1], parent_node.children[i] Config.save_to_file(self.sessions) self.walker.set_focus( UIParentNode(self.sessions, key_handler=self.on_key_press)) self.listbox.set_focus_to_node(this_node) else: return key
def on_key_press(self, key: str, w: UITreeWidget): this_node = w.get_node().get_value() folder_node = this_node if (w.get_node().get_parent() is None or this_node.nodetype == "folder") \ else w.get_node().get_parent().get_value() parent_node = None if w.get_node().get_parent( ) is None else w.get_node().get_parent().get_value() if key in ['q', 'Q', 'ctrl d']: self.command = EXIT_REDIAL raise urwid.ExitMainLoop() elif key == "enter": if isinstance(w.get_node(), UITreeNode): self.command = w.get_node().get_value( ).hostinfo.get_ssh_command() raise urwid.ExitMainLoop() elif key == "f3" and w.is_leaf: if (len(get_public_ssh_keys())) == 0: MessageDialog( "Error", "There is no public SSH Key (.pub) in ~/.ssh folder. You can use ssh-keygen to " "generate SSH key pairs", self.close_dialog).show(self.loop) else: self.log = "SSH key is copied successfully" CopySSHKeyDialog(this_node, self.close_dialog_and_run, self.change_log).show(self.loop) elif key == "f5" and w.is_leaf: if package_available(package_name="mc"): self.command = this_node.hostinfo.get_mc_command() raise urwid.ExitMainLoop() else: MessageDialog( "Error", "Please install mc (Midnight Commander) package" " to use this feature", self.close_dialog).show(self.loop) elif key == "f6": AddFolderDialog(folder_node, Node("", "folder"), self.save_and_focus).show(self.loop) elif key == "f7": AddHostDialog(folder_node, Node("", "session", HostInfo("")), self.save_and_focus).show(self.loop) elif key == "f8": if this_node.nodetype == "folder": # TODO implement removing folder MessageDialog("Error", "Folders can not be removed", self.close_dialog).show(self.loop) else: RemoveHostDialog(parent_node, this_node, self.save_and_focus).show(self.loop) elif key == "f9" and w.is_leaf: AddHostDialog(parent_node, this_node, self.save_and_focus).show(self.loop) elif key in ["meta down", "ctrl down"]: if parent_node is None: return i = parent_node.children.index(this_node) if i == len(parent_node.children) - 1: return # at bottom parent_node.children[i], parent_node.children[ i + 1] = parent_node.children[i + 1], parent_node.children[i] self.save_and_focus(this_node) elif key in ["meta up", "ctrl up"]: if parent_node is None: return i = parent_node.children.index(this_node) if i == 0: return # at top parent_node.children[i], parent_node.children[ i - 1] = parent_node.children[i - 1], parent_node.children[i] self.save_and_focus(this_node) else: return key