Example #1
0
 def main(self):
     edit_form = Form()
     self.value_box = edit_form.add(MultiLineInput,
                                    value=self.value,
                                    name=f'Input value for: {self.name}',
                                    max_height=12)
     self.description_box = edit_form.add(MultiLineInput,
                                          value=self.description,
                                          name="Optional Description: ",
                                          max_height=12)
     edit_form.edit()
Example #2
0
class BrowseApp(NPSApp):
    """
    Extends NPSApp class to add support for getting a list of deleted tree nodes that were marked for deletion.
    """
    BUFFER = 3
    SELECTED = 'SELECTED'
    TO_DELETE = 'TO_DELETE"'

    def __init__(self, browse: Browse, config_view: RBACLimitedConfigView,
                 cfg: ConfigService):
        self._browse = browse
        self._config_view = config_view
        self._cfg = cfg
        self.value_box = None
        self.browse_box = None
        self.select_state_box = None
        self.selected_params = {}

    def main(self):
        global npy_form
        self._browse_box = Form(
            name=
            "Browse Parameters: - 'e' to expand, 'c' to collapse, <s> to select, <d> to delete, "
            "<Tab> & <Shift+Tab> moves cursor between `OK` and `Tree` views.")

        # Value Box Relative Location
        val_relx, val_rely = int(self._browse_box.columns /
                                 2) * -1, int(self._browse_box.lines - 1) * -1
        val_max_height = int(self._browse_box.lines / 2) - self.BUFFER
        val_max_width = int(self._browse_box.columns / 2) - self.BUFFER

        # Selection Box Relative Location
        sel_relx, sel_rely = int(
            self._browse_box.columns /
            2) * -1, int(self._browse_box.lines / 2 + 1) * -1
        sel_max_height = int(self._browse_box.lines / 2) - self.BUFFER
        sel_max_width = int(self._browse_box.columns / 2) - self.BUFFER

        tree = self._browse_box.add(LogicalMLTree,
                                    on_select_callable=self.on_select,
                                    on_delete_callable=self.on_delete,
                                    max_width=self._browse_box.columns +
                                    val_relx - self.BUFFER)

        self.value_box = self._browse_box.add_widget(SelectedValueBox,
                                                     name="Parameter Value: ",
                                                     relx=val_relx,
                                                     rely=val_rely,
                                                     max_height=val_max_height,
                                                     max_width=val_max_width,
                                                     allow_filtering=False,
                                                     editable=False)

        self.select_state_box = self._browse_box.add_widget(
            SelectedValueBox,
            name="Selections: ",
            relx=sel_relx,
            rely=sel_rely,
            max_height=sel_max_height,
            max_width=sel_max_width,
            allow_filtering=False,
            editable=False)

        td = DeletableNPSTreeData(content='Root',
                                  selectable=True,
                                  expanded=True,
                                  ignoreRoot=True)
        start = Utils.millis_since_epoch()
        children = []
        if self._browse.prefix:
            prefix_child = td.newChild(content=self._browse.prefix,
                                       selectable=False,
                                       expanded=False)
            children = [prefix_child]
        else:
            log.info(
                f"--{prefix.name} missing, defaulting to normal browse tree.")

            for namespace in self._config_view.get_authorized_namespaces():
                child = td.newChild(content=namespace,
                                    selectable=False,
                                    expanded=False)
                children.append(child)

        for child in children:
            self._browse.dirs.add(child.getContent())

        futures = []

        with ThreadPoolExecutor(max_workers=10) as pool:
            for child in children:
                futures.append(
                    pool.submit(self._browse.add_children, child.getContent(),
                                child))

        for future in as_completed(futures):
            pass

        tree.values = td
        self._browse_box.edit()
        selection_objs = tree.get_selected_objects(return_node=True)

        for selection in selection_objs:
            full_path = ''
            while selection._parent is not None:
                full_path = selection.content + full_path
                selection = selection._parent
            self._browse.selected_ps_paths.append(full_path)

        delete_objs = tree.get_objects_to_delete(return_node=True)
        for selection in delete_objs:
            full_path = ''
            while selection._parent is not None:
                full_path = selection.content + full_path
                selection = selection._parent
            self._browse.deleted_ps_paths.append(full_path)

    def generate_selections(self) -> List:
        selects = [
            key for key in self.selected_params.keys()
            if self.selected_params[key] == self.SELECTED
        ]
        deletes = [
            key for key in self.selected_params.keys()
            if self.selected_params[key] == self.TO_DELETE
        ]

        select_prefix = ["", "Selections: ", "---"] if selects else []
        delete_prefix = ["", "To Delete: ", "---"] if deletes else []

        return select_prefix + selects + delete_prefix + deletes

    def on_delete(self, ps_name):
        if self.selected_params.get(ps_name) == self.TO_DELETE:
            del self.selected_params[ps_name]
        else:
            self.selected_params[ps_name] = self.TO_DELETE

        self.select_state_box.values = self.generate_selections()
        self.select_state_box.update()

    def on_select(self, ps_name: str, update: bool = True):
        """
        Lookup and update the value form when a user selects an item in the DisplayBox
        """

        # Only update query box on selects
        if update:
            self.value_box.name = ps_name
            try:
                val, desc, = self._cfg.get_parameter_with_description(ps_name)
            except ParameterUndecryptable:
                val = "Undecryptable"
                desc = "You do not have access to decrypt this parameter."
            except TypeError:
                return  # No parameter found

            val = f"> {val}" if val else ''
            desc = f"> {desc}" if desc else ''

            if val or desc:
                line_length = int(self._browse_box.columns / 2) - self.BUFFER
                val_lines = OutUtils.to_lines(val, line_length)
                desc_lines = OutUtils.to_lines(desc, line_length)
                self.value_box.values = [
                    "",
                    "Value: ",
                    f"---",
                ] + val_lines + [
                    "",
                    "Description: ",
                    f"---",
                ] + desc_lines

                self.value_box.update()

        # Update selection box
        if self.selected_params.get(ps_name) == self.SELECTED:
            del self.selected_params[ps_name]
        else:
            self.selected_params[ps_name] = self.SELECTED

        self.select_state_box.values = self.generate_selections()
        self.select_state_box.update()
Example #3
0
 def main(self):
     form = Form(name='Welcome to Npyscreen')
     form.edit()