From bb9851057fd5e5e23604368c2a5f15db7b442e8f Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 5 Mar 2023 18:26:59 +0200 Subject: [PATCH] (Added) sync group function. --- CHANGELOG.org | 2 ++ tui_rsync/cli/group_prompt.py | 31 +++++++++++++++++++++++++++++++ tui_rsync/cli/sync.py | 23 +++++++++++++++++++++-- tui_rsync/models/models.py | 20 ++++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 tui_rsync/cli/group_prompt.py diff --git a/CHANGELOG.org b/CHANGELOG.org index 11574bd..c1ba2c1 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -17,3 +17,5 @@ ** 0.5.2 <2023-03-04 Sat> Added *sync recover* function. Fixed label_prompt returning list. +** 0.5.3 <2023-03-05 Sun> + Added *sync group* function. diff --git a/tui_rsync/cli/group_prompt.py b/tui_rsync/cli/group_prompt.py new file mode 100644 index 0000000..4062e80 --- /dev/null +++ b/tui_rsync/cli/group_prompt.py @@ -0,0 +1,31 @@ +################################################################################ +# Copyright (C) 2023 Kostiantyn Klochko # +# # +# This file is part of tui-rsync. # +# # +# tui-rsync is free software: you can redistribute it and/or modify it under # +# uthe terms of the GNU General Public License as published by the Free # +# Software Foundation, either version 3 of the License, or (at your option) # +# any later version. # +# # +# tui-rsync is distributed in the hope that it will be useful, but WITHOUT ANY # +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # +# details. # +# # +# You should have received a copy of the GNU General Public License along with # +# tui-rsync. If not, see . # +################################################################################ + +from rich.console import Console +from pyfzf import FzfPrompt +from models.models import all_group_labels +from cli.label_prompt import LabelPrompt + +console = Console() + +class GroupPrompt(LabelPrompt): + @staticmethod + def get_label_fzf() -> str: + fzf = FzfPrompt() + return fzf.prompt(all_group_labels().iterator())[0] diff --git a/tui_rsync/cli/sync.py b/tui_rsync/cli/sync.py index c7323d2..2750149 100644 --- a/tui_rsync/cli/sync.py +++ b/tui_rsync/cli/sync.py @@ -22,9 +22,10 @@ from rich.prompt import Prompt from typing import List, Optional import typer -from models.models import Source, Destination, SyncCommand, Path +from models.models import Source, Group, Destination, SyncCommand, Path from models.models import all_labels from cli.label_prompt import LabelPrompt +from cli.group_prompt import GroupPrompt from cli.path_prompt import PathPrompt from cli.rsync import Rsync @@ -44,11 +45,29 @@ def one( """ if label is None: label = LabelPrompt.get_label_fzf() - src = Source.get(Source.label == label) + src = Source.get_source(label) rsync = Rsync(str(src.args)) for dest in src.destinations: rsync.run_one(str(src.source), str(dest)) +@sync.command() +def group( + group_label: str = typer.Option( + None, "--group-label", "-g", + help="[b]The label[/] is a uniq identification of a [b]group[/].", + show_default=False + ), +): + """ + [green b]Sync[/] a [yellow]group[/] with the [yellow b] label[/]. + """ + if group_label is None: + group_label = GroupPrompt.get_label_fzf() + group = Group.get_group(group_label) + + for src in group.get_sources(): + one(src.label) + @sync.command() def all(): """ diff --git a/tui_rsync/models/models.py b/tui_rsync/models/models.py index 5e138f1..b98a9cf 100644 --- a/tui_rsync/models/models.py +++ b/tui_rsync/models/models.py @@ -109,6 +109,16 @@ class Destination(BaseModel): class Group(BaseModel): label = CharField(unique=True) + @staticmethod + def is_exist(label) -> bool: + return Group.select().where(Group.label == label).exists() + + @staticmethod + def get_group(label): + if not Group.is_exist(label): + return None + return Group.select().where(Group.label == label).get() + @staticmethod def create_save(label:str, source_labels:list[str]): group = Group.create( @@ -122,6 +132,12 @@ class Group(BaseModel): group.save() return group + def get_sources(self): + """ + Return iterator of the group sources. + """ + return (group_src.source for group_src in self.sources) + def __str__(self) -> str: return f"{self.label}" @@ -144,6 +160,10 @@ def create_tables(): ] db.create_tables(tables, safe=True) +def all_group_labels(): + with db: + return Group.select(Group.label) + def all_labels(): with db: return Source.select(Source.label)