Skip to content

logseq_doctor.cli

cli

Module that contains the command line app.

Why does this file exist, and why not put this in main?

You might be tempted to import things from main later, but that will cause problems: the code will get executed twice:

  • When you run python -m logseq_doctor python will execute __main__.py as a script. That means there won't be any logseq_doctor.__main__ in sys.modules.
  • When you import main it will get executed again (as a module) because there's no logseq_doctor.__main__ in sys.modules.

Also see (1) from https://click.pocoo.org/5/setuptools/#setuptools-integration

GlobalOptions dataclass

Global options for every sub-command.

Source code in src/logseq_doctor/cli.py
Python
@dataclass
class GlobalOptions:
    """Global options for every sub-command."""

    logseq_graph_path: Path

TaskFormat

Bases: str, Enum

Task format.

Source code in src/logseq_doctor/cli.py
Python
class TaskFormat(str, Enum):
    """Task format."""

    text = "text"

lqdpy(ctx, logseq_graph_path=typer.Option(..., '--graph', '-g', help='Logseq graph', envvar='LOGSEQ_GRAPH_PATH', dir_okay=True, file_okay=False))

Logseq Doctor: heal your flat old Markdown files before importing them.

Source code in src/logseq_doctor/cli.py
Python
@app.callback()
def lqdpy(
    ctx: typer.Context,
    logseq_graph_path: Path = typer.Option(  # noqa: B008
        ...,
        "--graph",
        "-g",
        help="Logseq graph",
        envvar="LOGSEQ_GRAPH_PATH",
        dir_okay=True,
        file_okay=False,
    ),
) -> None:
    """Logseq Doctor: heal your flat old Markdown files before importing them."""
    ctx.obj = GlobalOptions(logseq_graph_path)

outline(text_file)

Convert flat Markdown to outline.

Source code in src/logseq_doctor/cli.py
Python
@app.command(no_args_is_help=True)
def outline(text_file: typer.FileText) -> None:
    """Convert flat Markdown to outline."""
    typer.echo(flat_markdown_to_outline(text_file.read()))

tasks(ctx, tag_or_page=typer.Argument(None, metavar='TAG', help='Tags or pages to query'), logseq_host_url=typer.Option(..., '--host', '-h', help='Logseq host', envvar='LOGSEQ_HOST_URL'), logseq_api_token=typer.Option(..., '--token', '-t', help='Logseq API token', envvar='LOGSEQ_API_TOKEN'), json_=typer.Option(False, '--json', help='Output in JSON format'), verbose=typer.Option(False, '--verbose', '-v', help='Verbose output'), completed=typer.Option(False, '--completed', '-c', help='Include canceled and done tasks'))

List tasks in Logseq.

Source code in src/logseq_doctor/cli.py
Python
@app.command()
def tasks(  # noqa: PLR0913
    ctx: typer.Context,
    tag_or_page: list[str] = typer.Argument(None, metavar="TAG", help="Tags or pages to query"),  # noqa: B008
    logseq_host_url: str = typer.Option(..., "--host", "-h", help="Logseq host", envvar="LOGSEQ_HOST_URL"),
    logseq_api_token: str = typer.Option(..., "--token", "-t", help="Logseq API token", envvar="LOGSEQ_API_TOKEN"),
    json_: bool = typer.Option(False, "--json", help="Output in JSON format"),
    verbose: bool = typer.Option(False, "--verbose", "-v", help="Verbose output"),
    completed: bool = typer.Option(False, "--completed", "-c", help="Include canceled and done tasks"),
) -> None:
    """List tasks in Logseq."""
    logseq = Logseq(logseq_host_url, logseq_api_token, cast("GlobalOptions", ctx.obj).logseq_graph_path)
    condition = ""
    if tag_or_page:
        if len(tag_or_page) == 1:
            condition = f" [[{tag_or_page[0]}]]"
        else:
            pages = " ".join([f"[[{tp}]]" for tp in tag_or_page])
            condition = f" (or {pages})"

    task_statuses = "TODO DOING WAITING NOW LATER"
    if completed:
        task_statuses += " CANCELED DONE"

    query = f"(and{condition} (task {task_statuses}))"
    if verbose:
        typer.echo(f"Query: {query}")

    if json_:
        typer.echo(logseq.query_json(query))
        return

    blocks_sorted_by_date = Block.sort_by_date(logseq.query_blocks(query))
    for block in blocks_sorted_by_date:
        typer.secho(f"{block.page_title}§", fg=typer.colors.GREEN, nl=False)
        typer.secho(block.url(logseq.graph_name), fg=typer.colors.BLUE, bold=True, nl=False)
        typer.echo(f{block.raw_content}")