Skip to content

Commands

sereto.cli.commands

WorkingDir

Bases: SeretoBaseModel

Helper class for REPL implementing the cd command.

Attributes:

Name Type Description
old_cwd Path

The previous working directory.

Source code in sereto/cli/commands.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class WorkingDir(SeretoBaseModel):
    """Helper class for REPL implementing the `cd` command.

    Attributes:
        old_cwd: The previous working directory.
    """

    old_cwd: Path = Field(default=Path.cwd())

    def change(self, dst: Path, /) -> None:
        """Change the current working directory to the new location.

        Also saves the previous location for future reference.

        Args:
            dst: The new working directory

        Raises:
            SeretoPathError: If the provided path is not an existing directory.
        """
        if not dst.is_dir():
            raise SeretoPathError(f"Directory '{dst}' does not exist.")

        cwd = Path.cwd()
        os.chdir(dst)
        self.old_cwd = cwd

    def go_back(self) -> None:
        """Change the current working directory to the previous location."""
        self.change(self.old_cwd)

change(dst)

Change the current working directory to the new location.

Also saves the previous location for future reference.

Parameters:

Name Type Description Default
dst Path

The new working directory

required

Raises:

Type Description
SeretoPathError

If the provided path is not an existing directory.

Source code in sereto/cli/commands.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def change(self, dst: Path, /) -> None:
    """Change the current working directory to the new location.

    Also saves the previous location for future reference.

    Args:
        dst: The new working directory

    Raises:
        SeretoPathError: If the provided path is not an existing directory.
    """
    if not dst.is_dir():
        raise SeretoPathError(f"Directory '{dst}' does not exist.")

    cwd = Path.cwd()
    os.chdir(dst)
    self.old_cwd = cwd

go_back()

Change the current working directory to the previous location.

Source code in sereto/cli/commands.py
71
72
73
def go_back(self) -> None:
    """Change the current working directory to the previous location."""
    self.change(self.old_cwd)

sereto_ls(settings)

List all reports in the user's reports directory.

Parameters:

Name Type Description Default
settings Settings

The Settings object.

required
Source code in sereto/cli/commands.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@validate_call
def sereto_ls(settings: Settings) -> None:
    """List all reports in the user's reports directory.

    Args:
        settings: The Settings object.
    """
    report_paths: list[Path] = [d for d in settings.reports_path.iterdir() if Report.is_report_dir(d)]
    table = Table("ID", "Name", "Location", title="Reports", box=box.MINIMAL)

    for dir in report_paths:
        try:
            report = load_report_function(settings=settings, report_path=dir)
            report_name: str = report.config.name
        except (RuntimeError, SeretoValueError):
            report_name = "n/a"

        table.add_row(dir.name, report_name, f"[link {dir.as_uri()}]{dir}")

    Console().print(table, justify="center")

sereto_repl(cli, settings)

Start an interactive Read-Eval-Print Loop (REPL) session.

Parameters:

Name Type Description Default
cli Group

The main CLI group.

required
Source code in sereto/cli/commands.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
def sereto_repl(cli: Group, settings: Settings) -> None:
    """Start an interactive Read-Eval-Print Loop (REPL) session.

    Args:
        cli: The main CLI group.
    """
    Console().log("Starting interactive mode. Type 'exit' to quit and 'cd ID' to change active project.")

    # Enable command history using readline
    readline.parse_and_bind("tab: complete")
    readline.set_auto_history(True)

    # Load command history from previous sessions
    _load_repl_history()

    prompt = _get_repl_prompt(settings=settings)
    wd = WorkingDir()

    while True:
        try:
            # TODO navigating the history (up/down keys) breaks the rich's prompt, no colors for now
            # cmd = Console().input(prompt).strip()

            # Get user input
            cmd = input(prompt).strip()

            match cmd:
                case "exit" | "quit":
                    break
                case "help" | "h" | "?":
                    cli.main(prog_name="sereto", args="--help", standalone_mode=False)
                case s if s.startswith("cd "):
                    _change_repl_dir(settings=settings, cmd=cmd, wd=wd)
                    prompt = _get_repl_prompt(settings=settings)
                case s if len(s) > 0:
                    cli.main(prog_name="sereto", args=cmd.split(), standalone_mode=False)
                case _:
                    continue
        except (KeyboardInterrupt, EOFError):
            # Allow graceful exit with Ctrl+C or Ctrl+D
            Console().log("Exiting interactive mode.")
            break
        except SystemExit:
            pass  # Click raises SystemExit on success
        except Exception as e:
            Console().log(f"[red]Error:[/red] {escape(str(e))}")

    # Save command history for future sessions
    _save_repl_history()