(Added) sync group function.

dev
KKlochko 2 years ago
parent e9e4162cac
commit bb9851057f

@ -17,3 +17,5 @@
** 0.5.2 <2023-03-04 Sat> ** 0.5.2 <2023-03-04 Sat>
Added *sync recover* function. Added *sync recover* function.
Fixed label_prompt returning list. Fixed label_prompt returning list.
** 0.5.3 <2023-03-05 Sun>
Added *sync group* function.

@ -0,0 +1,31 @@
################################################################################
# Copyright (C) 2023 Kostiantyn Klochko <kostya_klochko@ukr.net> #
# #
# 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 <https://www.gnu.org/licenses/>. #
################################################################################
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]

@ -22,9 +22,10 @@ from rich.prompt import Prompt
from typing import List, Optional from typing import List, Optional
import typer 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 models.models import all_labels
from cli.label_prompt import LabelPrompt from cli.label_prompt import LabelPrompt
from cli.group_prompt import GroupPrompt
from cli.path_prompt import PathPrompt from cli.path_prompt import PathPrompt
from cli.rsync import Rsync from cli.rsync import Rsync
@ -44,11 +45,29 @@ def one(
""" """
if label is None: if label is None:
label = LabelPrompt.get_label_fzf() label = LabelPrompt.get_label_fzf()
src = Source.get(Source.label == label) src = Source.get_source(label)
rsync = Rsync(str(src.args)) rsync = Rsync(str(src.args))
for dest in src.destinations: for dest in src.destinations:
rsync.run_one(str(src.source), str(dest)) 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() @sync.command()
def all(): def all():
""" """

@ -109,6 +109,16 @@ class Destination(BaseModel):
class Group(BaseModel): class Group(BaseModel):
label = CharField(unique=True) 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 @staticmethod
def create_save(label:str, source_labels:list[str]): def create_save(label:str, source_labels:list[str]):
group = Group.create( group = Group.create(
@ -122,6 +132,12 @@ class Group(BaseModel):
group.save() group.save()
return group 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: def __str__(self) -> str:
return f"{self.label}" return f"{self.label}"
@ -144,6 +160,10 @@ def create_tables():
] ]
db.create_tables(tables, safe=True) db.create_tables(tables, safe=True)
def all_group_labels():
with db:
return Group.select(Group.label)
def all_labels(): def all_labels():
with db: with db:
return Source.select(Source.label) return Source.select(Source.label)

Loading…
Cancel
Save