hinagata 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hinagata/__init__.py ADDED
@@ -0,0 +1,8 @@
1
+ """Lightweight license generator."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from .cli import app
6
+
7
+
8
+ __all__ = ["app"]
hinagata/__main__.py ADDED
@@ -0,0 +1,7 @@
1
+ from __future__ import annotations
2
+
3
+ from .cli import app
4
+
5
+
6
+ if __name__ == "__main__":
7
+ app()
hinagata/cli.py ADDED
@@ -0,0 +1,129 @@
1
+ """Command-line interface for hinagata."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import difflib
6
+ import getpass
7
+ import subprocess # noqa: S404
8
+ import sys
9
+ from datetime import UTC, datetime
10
+ from importlib.metadata import version
11
+ from pathlib import Path
12
+ from typing import Annotated
13
+
14
+ import typer
15
+ from rich.console import Console
16
+ from rich.table import Table
17
+
18
+ from . import prompts
19
+ from .licenses import LICENSES, License, find, is_valid_year
20
+
21
+
22
+ STDOUT = "-"
23
+
24
+ app = typer.Typer(add_completion=False, help="Generate a license file for your project.")
25
+ console = Console(stderr=True)
26
+
27
+
28
+ @app.command()
29
+ def generate(
30
+ license_id: Annotated[
31
+ str | None,
32
+ typer.Argument(metavar="[LICENSE]", help="SPDX identifier such as MIT. Asked interactively when omitted."),
33
+ ] = None,
34
+ author: Annotated[str | None, typer.Option("--author", "-a", help="Copyright holder.")] = None,
35
+ year: Annotated[str | None, typer.Option("--year", "-y", help="Copyright year or range.")] = None,
36
+ output: Annotated[Path, typer.Option("--output", "-o", help="File to write, or - for stdout.")] = Path("LICENSE"),
37
+ force: Annotated[bool, typer.Option("--force", "-f", help="Overwrite an existing file.")] = False,
38
+ list_licenses: Annotated[bool, typer.Option("--list", "-l", help="List the available licenses.")] = False,
39
+ show_version: Annotated[bool, typer.Option("--version", help="Show the version.", is_eager=True)] = False,
40
+ ) -> None:
41
+ """Generate a license file for your project."""
42
+ if show_version:
43
+ typer.echo(version("hinagata"))
44
+ raise typer.Exit
45
+ if list_licenses:
46
+ _print_catalogue()
47
+ raise typer.Exit
48
+ if year is not None and not is_valid_year(year):
49
+ msg = "use a year like 2026 or a range like 2020-2026"
50
+ raise typer.BadParameter(msg, param_hint="--year")
51
+
52
+ interactive = is_interactive()
53
+ lic = _resolve_license(license_id, interactive=interactive)
54
+ holder, years = _resolve_holder(lic, author, year, interactive=interactive)
55
+ text = lic.render(holder, years)
56
+
57
+ if str(output) == STDOUT:
58
+ typer.echo(text, nl=False)
59
+ return
60
+ _check_overwrite(output, force=force, interactive=interactive)
61
+ output.write_text(text, encoding="utf-8")
62
+ console.print(f"[green]✓[/] Wrote [bold]{lic.name}[/] to [cyan]{output}[/]")
63
+
64
+
65
+ def is_interactive() -> bool:
66
+ """Whether prompts can be shown."""
67
+ return sys.stdin.isatty() and sys.stdout.isatty()
68
+
69
+
70
+ def default_author() -> str:
71
+ """Best guess at the copyright holder: the git user, else the login name."""
72
+ try:
73
+ result = subprocess.run(
74
+ ["git", "config", "user.name"], # noqa: S607
75
+ capture_output=True,
76
+ check=False,
77
+ text=True,
78
+ timeout=5,
79
+ )
80
+ except (OSError, subprocess.SubprocessError):
81
+ return getpass.getuser()
82
+ return result.stdout.strip() or getpass.getuser()
83
+
84
+
85
+ def _resolve_license(license_id: str | None, *, interactive: bool) -> License:
86
+ if license_id is None:
87
+ if not interactive:
88
+ msg = "no license given; pass an SPDX identifier or run in a terminal (see --list)"
89
+ raise typer.BadParameter(msg, param_hint="LICENSE")
90
+ return prompts.select_license(LICENSES)
91
+ lic = find(license_id)
92
+ if lic is None:
93
+ known = {known.spdx_id.casefold(): known.spdx_id for known in LICENSES}
94
+ close = difflib.get_close_matches(license_id.casefold(), known, n=1, cutoff=0.5)
95
+ hint = f" Did you mean {known[close[0]]}?" if close else " See --list."
96
+ msg = f"unknown license {license_id!r}.{hint}"
97
+ raise typer.BadParameter(msg, param_hint="LICENSE")
98
+ return lic
99
+
100
+
101
+ def _resolve_holder(lic: License, author: str | None, year: str | None, *, interactive: bool) -> tuple[str, str]:
102
+ if not lic.has_holder:
103
+ return "", ""
104
+ default_year = str(datetime.now(UTC).year)
105
+ if interactive:
106
+ author = author or prompts.ask_author(default_author())
107
+ year = year or prompts.ask_year(default_year)
108
+ return author or default_author(), year or default_year
109
+
110
+
111
+ def _check_overwrite(output: Path, *, force: bool, interactive: bool) -> None:
112
+ if force or not output.exists():
113
+ return
114
+ if interactive and prompts.confirm_overwrite(output):
115
+ return
116
+ if interactive:
117
+ raise typer.Abort
118
+ console.print(f"[red]error:[/] {output} already exists, use --force to overwrite")
119
+ raise typer.Exit(1)
120
+
121
+
122
+ def _print_catalogue() -> None:
123
+ table = Table(box=None, pad_edge=False, header_style="bold")
124
+ table.add_column("ID", style="cyan", no_wrap=True)
125
+ table.add_column("Name")
126
+ table.add_column("Summary", style="dim")
127
+ for lic in LICENSES:
128
+ table.add_row(lic.spdx_id, lic.name, lic.summary)
129
+ Console().print(table)
hinagata/data/0bsd.txt ADDED
@@ -0,0 +1,14 @@
1
+ BSD Zero Clause License
2
+
3
+ Copyright (c) [year] [fullname]
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14
+ PERFORMANCE OF THIS SOFTWARE.