Exemple #1
0
    def on_item_save(self, item_id: str, event_kwargs: Dict[str, Any]) -> bool:
        """ update item specified by flow key using the new values/changes given in event_kwargs. """
        self.vpo(f"LiszApp.on_item_save('{item_id}', {event_kwargs})")

        add_item = (item_id == '')
        item_idx = -1 if add_item else self.find_item_index(item_id)

        action_or_err = self.edit_validate(item_idx, **event_kwargs)

        if action_or_err:
            if action_or_err.startswith('request_delete_confirmation'):
                self.change_flow(id_of_flow('confirm', 'item_deletion', item_id),
                                 popup_kwargs=dict(node_only=action_or_err.endswith('_for_node')))
            else:
                self.show_message(action_or_err,
                                  title=get_txt("error on adding new item '{item_id}'" if add_item else
                                                "error on editing item '{item_id}'"))

            return False

        if add_item:  # new list item added (with non-empty new_id) at index 0 of the current node
            item_idx = 0

        self.play_vibrate(LOVE_VIBRATE_PATTERN)
        event_kwargs['flow_id'] = id_of_flow('focus', 'item', self.current_node_items[item_idx]['id'])

        self.save_app_states()
        return True
Exemple #2
0
    def on_clipboard_key_v(self):
        """ paste copied item or all items of the current node from the OS clipboard into the current node.
        """
        lit = Clipboard.paste()
        self.vpo(f"LiszApp.on_clipboard_key_v: pasting {lit}")
        items = node_from_literal(lit)
        err_msg = self.add_items(items)
        if err_msg:
            self.show_message(err_msg, title=get_txt("{count} error(s) in adding {len(items)} item(s)",
                                                     count=err_msg.count('\n') + 1))

        self.refresh_all()
Exemple #3
0
    def on_node_import(self, _flow_key: str, event_kwargs: Dict[str, Any]) -> bool:
        """ import single node items to the node and index specified by event_kwargs[' flow key. """
        node = event_kwargs['node_to_import']
        add_node_id = event_kwargs.get('add_node_id', '')
        parent_node = event_kwargs.get('import_items_node', self.current_node_items)
        parent_index = event_kwargs.get('import_items_index', 0)
        self.vpo(f"LiszApp.on_import_node({node}, {add_node_id}, {parent_node}, {parent_index})")

        if add_node_id:
            err_msg = self.import_node(add_node_id, node, parent=parent_node, item_index=parent_index)
        else:
            err_msg = self.import_items(node, parent=parent_node, item_index=parent_index)
        if err_msg:
            self.po(f"LiszApp.on_import_node error: {err_msg}")
            self.show_message(err_msg, title=get_txt("import error(s)", count=err_msg.count("\n") + 1))

        self.save_app_states()
        self.refresh_all()
        return True
Exemple #4
0
    def export_node(self, flow_path: List[str], file_path: str = "", node: Optional[LiszNode] = None
                    ) -> Optional[Exception]:
        """ overwritten export node for overwrite file path default and for to log call and error.

        :param flow_path:   flow path of the node to export (def=OS documents folder / app name).
        :param file_path:   path to the folder wherein to store the node data (def=OS documents folder + app name).
        :param node:        explicit/filtered node items (if not passed then all items will be exported).
        :return:            `None` if node got exported without errors, else the raised exception.
        """
        if not file_path:
            file_path = self.documents_root_path
        self.vpo(f"LiszApp.export_node({flow_path}, {file_path})")

        exception = super().export_node(flow_path, file_path=file_path, node=node)
        if exception:
            self.po(f"LiszApp.export_node({flow_path}, {file_path}) exception: {exception}")
            self.show_message(str(exception), title=get_txt("export error"))

        return exception
Exemple #5
0
        def add_extract_options(node_flow_path: List[str], pre: str = ""):
            """ add extract options for the node specified by node_flow_path.

            :param node_flow_path:  flow path of node for to add extract options to the child_maps list.
            :param pre:             info button text prefix (for to mark the focused item).
            """
            node = self.flow_path_node(node_flow_path, strict=True)
            if not node or node_flow_path in added_nodes:
                return      # skip empty and already added nodes
            added_nodes.append(node_flow_path)
            parent_node_id = flow_key(self.flow_path[-1]) if self.flow_path else FLOW_PATH_ROOT_ID

            if child_maps:
                child_maps.append(dict(              # add separator widget
                    cls='Widget',
                    kwargs=dict(size_hint_y=None, height=self.font_size / 3)))

            copy_icon = id_of_flow('copy', 'node')
            image_size = (self.font_size * 1.29, self.font_size * 1.29)
            node_info = self.node_info(node,
                                       what=() if self.verbose else ('selected_leaf_count', 'unselected_leaf_count'))
            if parent_node_id:
                node_info['name'] = parent_node_id

            info_text = (f"{self.flow_path_text(node_flow_path, display_root=True)}"
                         f"   {node_info[('un' if sel_status is False else '') + 'selected_leaf_count']}"
                         f"/{node_info['selected_leaf_count'] + node_info['unselected_leaf_count']}")
            child_maps.append(dict(
                kwargs=dict(
                    text=pre + info_text,
                    tap_flow_id=id_of_flow('open', 'node_info', repr(node_flow_path)),
                    tap_kwargs=dict(popup_kwargs=dict(node_info=node_info)),
                    image_size=image_size,
                    circle_fill_color=self.flow_path_ink, circle_fill_size=image_size,
                    square_fill_color=self.flow_id_ink[:3] + (0.39, ))))

            attr_tpl = dict(tap_flow_id=id_of_flow('extract', 'node', repr(node_flow_path)),
                            tap_kwargs=dict(popups_to_close=('replace_with_data_map_container', ), extract_type=''),
                            icon_name=id_of_flow('export', 'node'), image_size=image_size)

            attrs = deepcopy(attr_tpl)
            attrs.update(text=get_txt("copy all node items"), icon_name=copy_icon)
            attrs['tap_kwargs']['extract_type'] = 'copy'
            child_maps.append(dict(kwargs=attrs))

            attrs = deepcopy(attr_tpl)
            attrs.update(text=get_txt("export all node items"))
            attrs['tap_kwargs']['extract_type'] = 'export'
            child_maps.append(dict(kwargs=attrs))

            if self.debug:
                attrs = deepcopy(attr_tpl)
                attrs.update(text=get_txt("cut all node items"), icon_name=id_of_flow('cut', 'node'))
                attrs['tap_kwargs']['extract_type'] = 'cut'
                child_maps.append(dict(kwargs=attrs))

                attrs = deepcopy(attr_tpl)
                attrs.update(text=get_txt("delete all node items"), icon_name=id_of_flow('delete', 'item'))
                attrs['tap_kwargs']['extract_type'] = 'delete'
                child_maps.append(dict(kwargs=attrs))

            if sel_status is not None:
                extract_filter = 'sel' if sel_status else 'unsel'
                ink = self.selected_item_ink if sel_status else self.unselected_item_ink

                attrs = deepcopy(attr_tpl)
                attrs.update(text=get_txt("copy {'' if sel_status else 'un'}selected node items"), icon_name=copy_icon,
                             circle_fill_color=ink, circle_fill_size=image_size)
                attrs['tap_kwargs']['extract_type'] = 'copy_' + extract_filter
                child_maps.append(dict(kwargs=attrs))

                attrs = deepcopy(attr_tpl)
                attrs.update(text=get_txt("export {'' if sel_status else 'un'}selected node items"),
                             circle_fill_color=ink, circle_fill_size=image_size)
                attrs['tap_kwargs']['extract_type'] = 'export_' + extract_filter
                child_maps.append(dict(kwargs=attrs))
Exemple #6
0
    def show_extract_options(self, widget: Widget, flow_path_text: str = '',
                             item: Optional[LiszItem] = None, sel_status: Optional[bool] = None) -> bool:
        """ open context menu with the available extract actions of the current context/flow/app-status (focus, debug).

        :param widget:          widget to show extract options for.
        :param flow_path_text:  flow path text for to identify the node to extract (def=self.flow_path).
        :param item:            data dict of the item represented by :paramref:`~show_extract_options.widget`.
        :param sel_status:      pass True / False to add option for to export selected / unselected items.
        :return:                True if extract options are available and got displayed else False.
        """
        def add_extract_options(node_flow_path: List[str], pre: str = ""):
            """ add extract options for the node specified by node_flow_path.

            :param node_flow_path:  flow path of node for to add extract options to the child_maps list.
            :param pre:             info button text prefix (for to mark the focused item).
            """
            node = self.flow_path_node(node_flow_path, strict=True)
            if not node or node_flow_path in added_nodes:
                return      # skip empty and already added nodes
            added_nodes.append(node_flow_path)
            parent_node_id = flow_key(self.flow_path[-1]) if self.flow_path else FLOW_PATH_ROOT_ID

            if child_maps:
                child_maps.append(dict(              # add separator widget
                    cls='Widget',
                    kwargs=dict(size_hint_y=None, height=self.font_size / 3)))

            copy_icon = id_of_flow('copy', 'node')
            image_size = (self.font_size * 1.29, self.font_size * 1.29)
            node_info = self.node_info(node,
                                       what=() if self.verbose else ('selected_leaf_count', 'unselected_leaf_count'))
            if parent_node_id:
                node_info['name'] = parent_node_id

            info_text = (f"{self.flow_path_text(node_flow_path, display_root=True)}"
                         f"   {node_info[('un' if sel_status is False else '') + 'selected_leaf_count']}"
                         f"/{node_info['selected_leaf_count'] + node_info['unselected_leaf_count']}")
            child_maps.append(dict(
                kwargs=dict(
                    text=pre + info_text,
                    tap_flow_id=id_of_flow('open', 'node_info', repr(node_flow_path)),
                    tap_kwargs=dict(popup_kwargs=dict(node_info=node_info)),
                    image_size=image_size,
                    circle_fill_color=self.flow_path_ink, circle_fill_size=image_size,
                    square_fill_color=self.flow_id_ink[:3] + (0.39, ))))

            attr_tpl = dict(tap_flow_id=id_of_flow('extract', 'node', repr(node_flow_path)),
                            tap_kwargs=dict(popups_to_close=('replace_with_data_map_container', ), extract_type=''),
                            icon_name=id_of_flow('export', 'node'), image_size=image_size)

            attrs = deepcopy(attr_tpl)
            attrs.update(text=get_txt("copy all node items"), icon_name=copy_icon)
            attrs['tap_kwargs']['extract_type'] = 'copy'
            child_maps.append(dict(kwargs=attrs))

            attrs = deepcopy(attr_tpl)
            attrs.update(text=get_txt("export all node items"))
            attrs['tap_kwargs']['extract_type'] = 'export'
            child_maps.append(dict(kwargs=attrs))

            if self.debug:
                attrs = deepcopy(attr_tpl)
                attrs.update(text=get_txt("cut all node items"), icon_name=id_of_flow('cut', 'node'))
                attrs['tap_kwargs']['extract_type'] = 'cut'
                child_maps.append(dict(kwargs=attrs))

                attrs = deepcopy(attr_tpl)
                attrs.update(text=get_txt("delete all node items"), icon_name=id_of_flow('delete', 'item'))
                attrs['tap_kwargs']['extract_type'] = 'delete'
                child_maps.append(dict(kwargs=attrs))

            if sel_status is not None:
                extract_filter = 'sel' if sel_status else 'unsel'
                ink = self.selected_item_ink if sel_status else self.unselected_item_ink

                attrs = deepcopy(attr_tpl)
                attrs.update(text=get_txt("copy {'' if sel_status else 'un'}selected node items"), icon_name=copy_icon,
                             circle_fill_color=ink, circle_fill_size=image_size)
                attrs['tap_kwargs']['extract_type'] = 'copy_' + extract_filter
                child_maps.append(dict(kwargs=attrs))

                attrs = deepcopy(attr_tpl)
                attrs.update(text=get_txt("export {'' if sel_status else 'un'}selected node items"),
                             circle_fill_color=ink, circle_fill_size=image_size)
                attrs['tap_kwargs']['extract_type'] = 'export_' + extract_filter
                child_maps.append(dict(kwargs=attrs))

        self.vpo(f"LiszApp.show_extract_options({widget}, {flow_path_text}, {item}, {sel_status})")
        flow_path = self.flow_path_from_text(flow_path_text) if flow_path_text else self.flow_path
        has_focus = flow_action(self.flow_id) == 'focus'
        flo_key = flow_key(self.flow_id)
        child_maps = list()
        added_nodes = list()

        node_id = item['id'] if item else ''
        if node_id and 'node' in item:
            add_extract_options(flow_path + [id_of_flow('enter', 'item', node_id)],
                                pre=FOCUS_FLOW_PREFIX if has_focus and node_id == flo_key else "")

        add_extract_options(flow_path)

        if has_focus and flo_key != node_id:
            item = self.item_by_id(flo_key)
            if 'node' in item:
                add_extract_options(flow_path + [id_of_flow('enter', 'item', flo_key)], pre=FOCUS_FLOW_PREFIX)

        if not child_maps:
            self.show_message(get_txt("no extract options available for this item/node"))
            return False

        popup_kwargs = dict(parent=widget, child_data_maps=child_maps, auto_width_child_padding=self.font_size * 3)
        return self.change_flow(id_of_flow('open', 'extract'), popup_kwargs=popup_kwargs)
Exemple #7
0
        def add_import_options(info_text: str):
            """ add menu options for the current node. """
            info = self.node_info(node, what=() if self.verbose else ('selected_leaf_count', 'unselected_leaf_count'))
            if len(node) == 1:
                info['content'] = node[0]['id']
            info_text = str(info['selected_leaf_count'] + info['unselected_leaf_count']) + " items from " + info_text
            if node_id:
                info['name'] = node_id
                info_text = get_txt("node '{node_id}' with ") + info_text
            info_text += " to"
            child_maps.append(dict(kwargs=dict(
                text=info_text, tap_flow_id=id_of_flow('open', 'node_info'),
                tap_kwargs=dict(popup_kwargs=dict(node_info=info)),
                image_size=image_size, circle_fill_color=self.flow_path_ink, circle_fill_size=image_size,
                square_fill_color=self.flow_id_ink[:3] + (0.39,))))

            args_tpl: Dict[str, Any] = dict(
                tap_flow_id=id_of_flow('import', 'node'),
                tap_kwargs=dict(node_to_import=node, popups_to_close=('replace_with_data_map_container',)),
                image_size=image_size)

            if self.flow_path:
                kwargs = deepcopy(args_tpl)
                kwargs['text'] = get_txt("current list begin")
                if node_id:
                    kwargs['tap_kwargs']['add_node_id'] = node_id
                child_maps.append(dict(kwargs=kwargs))

                item_index = len(self.current_node_items)
                if item_index:
                    kwargs = deepcopy(args_tpl)
                    kwargs['text'] = get_txt("current list end")
                    kwargs['tap_kwargs']['import_items_index'] = item_index
                    if node_id:
                        kwargs['tap_kwargs']['add_node_id'] = node_id
                    child_maps.append(dict(kwargs=kwargs))

            kwargs = deepcopy(args_tpl)
            kwargs['text'] = get_txt("{FLOW_PATH_ROOT_ID} list begin")
            kwargs['tap_kwargs']['import_items_node'] = self.root_node
            if node_id:
                kwargs['tap_kwargs']['add_node_id'] = node_id
            child_maps.append(dict(kwargs=kwargs))

            item_index = len(self.root_node)
            if item_index:
                kwargs = deepcopy(args_tpl)
                kwargs['text'] = get_txt("{FLOW_PATH_ROOT_ID} list end")
                kwargs['tap_kwargs']['import_items_node'] = self.root_node
                kwargs['tap_kwargs']['import_items_index'] = item_index
                if node_id:
                    kwargs['tap_kwargs']['add_node_id'] = node_id
                child_maps.append(dict(kwargs=kwargs))

            if focused_id:
                item_index = self.find_item_index(focused_id)

                kwargs = deepcopy(args_tpl)
                kwargs['text'] = get_txt("before focused item '{focused_id}'")
                kwargs['tap_kwargs']['import_items_index'] = item_index
                if node_id:
                    kwargs['tap_kwargs']['add_node_id'] = node_id
                child_maps.append(dict(kwargs=kwargs))

                focused_item = self.item_by_id(focused_id)
                if 'node' in focused_item:
                    kwargs = deepcopy(args_tpl)
                    kwargs['text'] = get_txt("into focused sub-list '{focused_id}'")
                    kwargs['tap_kwargs']['import_items_node'] = focused_item['node']
                    if node_id:
                        kwargs['tap_kwargs']['add_node_id'] = node_id
                    child_maps.append(dict(kwargs=kwargs))

                kwargs = deepcopy(args_tpl)
                kwargs['text'] = get_txt("after focused '{focused_id}'")
                kwargs['tap_kwargs']['import_items_index'] = item_index + 1
                if node_id:
                    kwargs['tap_kwargs']['add_node_id'] = node_id
                child_maps.append(dict(kwargs=kwargs))
Exemple #8
0
    def show_add_options(self, widget: Widget) -> bool:
        """ open context menu with all available actions for to add/import new node/item(s).

        :param widget:          FlowButton to open this context menu.
        :return:                True if any add options are available and got displayed else False.
        """
        def add_import_options(info_text: str):
            """ add menu options for the current node. """
            info = self.node_info(node, what=() if self.verbose else ('selected_leaf_count', 'unselected_leaf_count'))
            if len(node) == 1:
                info['content'] = node[0]['id']
            info_text = str(info['selected_leaf_count'] + info['unselected_leaf_count']) + " items from " + info_text
            if node_id:
                info['name'] = node_id
                info_text = get_txt("node '{node_id}' with ") + info_text
            info_text += " to"
            child_maps.append(dict(kwargs=dict(
                text=info_text, tap_flow_id=id_of_flow('open', 'node_info'),
                tap_kwargs=dict(popup_kwargs=dict(node_info=info)),
                image_size=image_size, circle_fill_color=self.flow_path_ink, circle_fill_size=image_size,
                square_fill_color=self.flow_id_ink[:3] + (0.39,))))

            args_tpl: Dict[str, Any] = dict(
                tap_flow_id=id_of_flow('import', 'node'),
                tap_kwargs=dict(node_to_import=node, popups_to_close=('replace_with_data_map_container',)),
                image_size=image_size)

            if self.flow_path:
                kwargs = deepcopy(args_tpl)
                kwargs['text'] = get_txt("current list begin")
                if node_id:
                    kwargs['tap_kwargs']['add_node_id'] = node_id
                child_maps.append(dict(kwargs=kwargs))

                item_index = len(self.current_node_items)
                if item_index:
                    kwargs = deepcopy(args_tpl)
                    kwargs['text'] = get_txt("current list end")
                    kwargs['tap_kwargs']['import_items_index'] = item_index
                    if node_id:
                        kwargs['tap_kwargs']['add_node_id'] = node_id
                    child_maps.append(dict(kwargs=kwargs))

            kwargs = deepcopy(args_tpl)
            kwargs['text'] = get_txt("{FLOW_PATH_ROOT_ID} list begin")
            kwargs['tap_kwargs']['import_items_node'] = self.root_node
            if node_id:
                kwargs['tap_kwargs']['add_node_id'] = node_id
            child_maps.append(dict(kwargs=kwargs))

            item_index = len(self.root_node)
            if item_index:
                kwargs = deepcopy(args_tpl)
                kwargs['text'] = get_txt("{FLOW_PATH_ROOT_ID} list end")
                kwargs['tap_kwargs']['import_items_node'] = self.root_node
                kwargs['tap_kwargs']['import_items_index'] = item_index
                if node_id:
                    kwargs['tap_kwargs']['add_node_id'] = node_id
                child_maps.append(dict(kwargs=kwargs))

            if focused_id:
                item_index = self.find_item_index(focused_id)

                kwargs = deepcopy(args_tpl)
                kwargs['text'] = get_txt("before focused item '{focused_id}'")
                kwargs['tap_kwargs']['import_items_index'] = item_index
                if node_id:
                    kwargs['tap_kwargs']['add_node_id'] = node_id
                child_maps.append(dict(kwargs=kwargs))

                focused_item = self.item_by_id(focused_id)
                if 'node' in focused_item:
                    kwargs = deepcopy(args_tpl)
                    kwargs['text'] = get_txt("into focused sub-list '{focused_id}'")
                    kwargs['tap_kwargs']['import_items_node'] = focused_item['node']
                    if node_id:
                        kwargs['tap_kwargs']['add_node_id'] = node_id
                    child_maps.append(dict(kwargs=kwargs))

                kwargs = deepcopy(args_tpl)
                kwargs['text'] = get_txt("after focused '{focused_id}'")
                kwargs['tap_kwargs']['import_items_index'] = item_index + 1
                if node_id:
                    kwargs['tap_kwargs']['add_node_id'] = node_id
                child_maps.append(dict(kwargs=kwargs))

        self.vpo(f"LiszApp.show_add_options({widget})")
        image_size = (self.font_size * 1.35, self.font_size * 1.35)
        focused_id = flow_key(self.flow_id) if flow_action(self.flow_id) == 'focus' else ''
        child_maps = list()

        node = node_from_literal(Clipboard.paste())
        if node:
            node_id = ''
            add_import_options(get_txt("clipboard"))

        node_files = self.importable_node_files(folder_path=self.documents_root_path)
        for node_file_info in node_files:
            if child_maps:                      # add separator widget
                child_maps.append(dict(cls='Widget', kwargs=dict(size_hint_y=None, height=self.font_size / 3)))

            node_id, node, file_path, err_msg = node_file_info
            if not err_msg:
                add_import_options(get_txt("export file"))

                child_maps.append(dict(cls='Widget', kwargs=dict(size_hint_y=None, height=self.font_size / 3)))
                node_id = ''
                add_import_options(get_txt("export file"))

            elif self.verbose:
                child_maps.append(dict(kwargs=dict(
                    cls='ImageLabel',
                    text=node_id + " error: " + err_msg,
                    square_fill_color=(1, 0, 0, 0.39,))))

        if not child_maps:
            self.show_message(get_txt("neither clipboard nor '{self.documents_root_path}' contains items to import"),
                              title=get_txt("import error(s)", count=1))
            return False

        popup_kwargs = dict(parent=widget, child_data_maps=child_maps, auto_width_child_padding=self.font_size * 3)
        return self.change_flow(id_of_flow('open', 'alt_add'), popup_kwargs=popup_kwargs)