lice3 0.14.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.
- lice3/__init__.py +7 -0
- lice3/api/__init__.py +5 -0
- lice3/api/api.py +146 -0
- lice3/api/exceptions.py +63 -0
- lice3/config.py +56 -0
- lice3/constants.py +124 -0
- lice3/core.py +234 -0
- lice3/helpers.py +378 -0
- lice3/templates/__init__.py +1 -0
- lice3/templates/template-afl3.txt +187 -0
- lice3/templates/template-agpl3-header.txt +15 -0
- lice3/templates/template-agpl3.txt +662 -0
- lice3/templates/template-al2.txt +156 -0
- lice3/templates/template-apache-header.txt +13 -0
- lice3/templates/template-apache.txt +207 -0
- lice3/templates/template-bsd2.txt +24 -0
- lice3/templates/template-bsd3.txt +27 -0
- lice3/templates/template-cc0-header.txt +8 -0
- lice3/templates/template-cc0.txt +121 -0
- lice3/templates/template-cc_by-header.txt +7 -0
- lice3/templates/template-cc_by.txt +319 -0
- lice3/templates/template-cc_by_nc-header.txt +7 -0
- lice3/templates/template-cc_by_nc.txt +334 -0
- lice3/templates/template-cc_by_nc_nd-header.txt +7 -0
- lice3/templates/template-cc_by_nc_nd.txt +308 -0
- lice3/templates/template-cc_by_nc_sa-header.txt +7 -0
- lice3/templates/template-cc_by_nc_sa.txt +360 -0
- lice3/templates/template-cc_by_nd-header.txt +7 -0
- lice3/templates/template-cc_by_nd.txt +293 -0
- lice3/templates/template-cc_by_sa-header.txt +7 -0
- lice3/templates/template-cc_by_sa.txt +359 -0
- lice3/templates/template-cddl.txt +331 -0
- lice3/templates/template-edl.txt +28 -0
- lice3/templates/template-epl.txt +216 -0
- lice3/templates/template-eupl.txt +288 -0
- lice3/templates/template-gpl2-header.txt +17 -0
- lice3/templates/template-gpl2.txt +266 -0
- lice3/templates/template-gpl3-header.txt +15 -0
- lice3/templates/template-gpl3.txt +621 -0
- lice3/templates/template-isc.txt +16 -0
- lice3/templates/template-lgpl.txt +165 -0
- lice3/templates/template-mit.txt +20 -0
- lice3/templates/template-mpl-header.txt +3 -0
- lice3/templates/template-mpl.txt +373 -0
- lice3/templates/template-ofl.txt +96 -0
- lice3/templates/template-unlicense.txt +24 -0
- lice3/templates/template-wtfpl-header.txt +4 -0
- lice3/templates/template-wtfpl.txt +13 -0
- lice3/templates/template-zlib.txt +20 -0
- lice3/tests/__init__.py +1 -0
- lice3/tests/conftest.py +69 -0
- lice3/tests/test_api.py +187 -0
- lice3/tests/test_cli.py +140 -0
- lice3/tests/test_lice.py +549 -0
- lice3-0.14.0.dist-info/METADATA +345 -0
- lice3-0.14.0.dist-info/RECORD +59 -0
- lice3-0.14.0.dist-info/WHEEL +4 -0
- lice3-0.14.0.dist-info/entry_points.txt +2 -0
- lice3-0.14.0.dist-info/licenses/LICENSE +29 -0
lice3/__init__.py
ADDED
lice3/api/__init__.py
ADDED
lice3/api/api.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"""This defines an API that other Python code can use to interact with LICE3."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from lice3.api.exceptions import (
|
|
6
|
+
HeaderNotFoundError,
|
|
7
|
+
InvalidYearError,
|
|
8
|
+
LanguageNotFoundError,
|
|
9
|
+
LicenseNotFoundError,
|
|
10
|
+
)
|
|
11
|
+
from lice3.constants import LANGS, LICENSES
|
|
12
|
+
from lice3.helpers import (
|
|
13
|
+
format_license,
|
|
14
|
+
generate_license,
|
|
15
|
+
get_local_year,
|
|
16
|
+
load_package_template,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Lice:
|
|
21
|
+
"""List or Generate a License from many supported licenses."""
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
organization: str,
|
|
26
|
+
project: str,
|
|
27
|
+
year: str | int = get_local_year(),
|
|
28
|
+
) -> None:
|
|
29
|
+
"""Initialize the Lice object.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
organization: The name of the organization that owns the project.
|
|
33
|
+
project: The name of the project.
|
|
34
|
+
year: The year to use in the license. Defaults to the current year.
|
|
35
|
+
(can be a string or an integer)
|
|
36
|
+
|
|
37
|
+
Note that not all licenses will use the 'project' field.
|
|
38
|
+
|
|
39
|
+
Example:
|
|
40
|
+
>>> lice = Lice(organization="Awesome Co.", project="my_project")
|
|
41
|
+
"""
|
|
42
|
+
self.organization = organization
|
|
43
|
+
self.project = project
|
|
44
|
+
|
|
45
|
+
try:
|
|
46
|
+
# make sure the year can be a valid integer
|
|
47
|
+
_ = int(year)
|
|
48
|
+
except ValueError:
|
|
49
|
+
raise InvalidYearError(year) from None
|
|
50
|
+
|
|
51
|
+
self.year = str(year)
|
|
52
|
+
if len(self.year) != 4: # noqa: PLR2004
|
|
53
|
+
raise InvalidYearError(year) from None
|
|
54
|
+
|
|
55
|
+
def get_licenses(self) -> list[str]:
|
|
56
|
+
"""Return a list of all licenses in the system.
|
|
57
|
+
|
|
58
|
+
This returns a list of strings, where each string is the name of a
|
|
59
|
+
license that can then be used to generate or retrieve the text of that
|
|
60
|
+
license.
|
|
61
|
+
|
|
62
|
+
Example:
|
|
63
|
+
>>> lice = Lice(organization="Awesome Co.", project="my_project")
|
|
64
|
+
>>> lice.get_licenses()
|
|
65
|
+
['apache', 'bsd2', 'bsd3', 'gpl2', 'gpl3', ...]
|
|
66
|
+
"""
|
|
67
|
+
return LICENSES
|
|
68
|
+
|
|
69
|
+
def get_languages(self) -> list[str]:
|
|
70
|
+
"""Return a list of all supported languages.
|
|
71
|
+
|
|
72
|
+
This returns a list of strings, where each string is the name of a
|
|
73
|
+
language EXTENSION that can be used to generate a license in that
|
|
74
|
+
language format.
|
|
75
|
+
|
|
76
|
+
Example:
|
|
77
|
+
>>> lice = Lice(organization="Awesome Co.", project="my_project")
|
|
78
|
+
>>> lice.get_languages()
|
|
79
|
+
['py', 'js', 'c', 'cpp', 'java', 'rs', 'rb', 'sh', 'html', ...]
|
|
80
|
+
"""
|
|
81
|
+
return list(LANGS.keys())
|
|
82
|
+
|
|
83
|
+
def get_license(self, license_name: str, language: str = "") -> str:
|
|
84
|
+
"""Return the text of the given license.
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
license_name: The name of the license to retrieve.
|
|
88
|
+
language: [OPTIONAL] If set, comment the license for that language.
|
|
89
|
+
|
|
90
|
+
Examples:
|
|
91
|
+
>>> lice = Lice(organization="Awesome Co.", project="my_project")
|
|
92
|
+
>>> licence_txt = Lice.get_license("mit")
|
|
93
|
+
"""
|
|
94
|
+
args = {
|
|
95
|
+
"year": self.year,
|
|
96
|
+
"organization": self.organization,
|
|
97
|
+
"project": self.project,
|
|
98
|
+
}
|
|
99
|
+
try:
|
|
100
|
+
template = load_package_template(license_name)
|
|
101
|
+
except FileNotFoundError:
|
|
102
|
+
raise LicenseNotFoundError(license_name) from None
|
|
103
|
+
|
|
104
|
+
content = generate_license(template, args)
|
|
105
|
+
|
|
106
|
+
try:
|
|
107
|
+
out = format_license(content, language)
|
|
108
|
+
except KeyError:
|
|
109
|
+
raise LanguageNotFoundError(language) from None
|
|
110
|
+
return out.getvalue()
|
|
111
|
+
|
|
112
|
+
def get_header(self, license_name: str, language: str = "") -> str:
|
|
113
|
+
"""Return the header of the given license suitable for source files.
|
|
114
|
+
|
|
115
|
+
If the language is specified, the header will be formatted as a
|
|
116
|
+
commented block for that language. If not, the header will be returned
|
|
117
|
+
as a plain text block.
|
|
118
|
+
|
|
119
|
+
Note: Not all licenses have headers, if the license does not have a
|
|
120
|
+
header, this method will raise a HeaderNotFoundError.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
license_name: The name of the license to retrieve.
|
|
124
|
+
language: The language to format the header for.
|
|
125
|
+
|
|
126
|
+
Example:
|
|
127
|
+
>>> lice = Lice(organization="Awesome Co.", project="my_project")
|
|
128
|
+
>>> header_txt = Lice.get_header("mit", "py")
|
|
129
|
+
"""
|
|
130
|
+
args = {
|
|
131
|
+
"year": self.year,
|
|
132
|
+
"organization": self.organization,
|
|
133
|
+
"project": self.project,
|
|
134
|
+
}
|
|
135
|
+
try:
|
|
136
|
+
template = load_package_template(license_name, header=True)
|
|
137
|
+
except FileNotFoundError:
|
|
138
|
+
raise HeaderNotFoundError(license_name) from None
|
|
139
|
+
|
|
140
|
+
content = generate_license(template, args)
|
|
141
|
+
|
|
142
|
+
try:
|
|
143
|
+
out = format_license(content, language)
|
|
144
|
+
except KeyError:
|
|
145
|
+
raise LanguageNotFoundError(language) from None
|
|
146
|
+
return out.getvalue()
|
lice3/api/exceptions.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Define custom exceptions for the API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class LiceError(Exception):
|
|
7
|
+
"""Base class for all exceptions in the Lice API."""
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class LicenseNotFoundError(LiceError):
|
|
11
|
+
"""Raised when a license is not found in the database."""
|
|
12
|
+
|
|
13
|
+
def __init__(self, license_name: str) -> None:
|
|
14
|
+
"""Initialize the LicenseNotFoundError exception.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
license_name: The name of the license that was not found.
|
|
18
|
+
"""
|
|
19
|
+
self.license_name = license_name
|
|
20
|
+
super().__init__(f"License '{self.license_name}' is unknown.")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class LanguageNotFoundError(LiceError):
|
|
24
|
+
"""Raised when a language is not found in the database."""
|
|
25
|
+
|
|
26
|
+
def __init__(self, language_name: str) -> None:
|
|
27
|
+
"""Initialize the LanguageNotFoundError exception.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
language_name: The name of the language that was not found.
|
|
31
|
+
"""
|
|
32
|
+
self.language_name = language_name
|
|
33
|
+
super().__init__(f"Language '{self.language_name}' is unknown.")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class HeaderNotFoundError(LiceError):
|
|
37
|
+
"""Raised when a header is not found for the supplied license."""
|
|
38
|
+
|
|
39
|
+
def __init__(self, license_name: str) -> None:
|
|
40
|
+
"""Initialize the NoHeaderFoundError exception.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
license_name: The name of the license without a header.
|
|
44
|
+
"""
|
|
45
|
+
self.license_name = license_name
|
|
46
|
+
super().__init__(
|
|
47
|
+
f"License '{self.license_name}' does not have any headers."
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class InvalidYearError(LiceError):
|
|
52
|
+
"""Raised when an invalid year is supplied."""
|
|
53
|
+
|
|
54
|
+
def __init__(self, year: str | int) -> None:
|
|
55
|
+
"""Initialize the InvalidYearError exception.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
year: The year that was not valid.
|
|
59
|
+
"""
|
|
60
|
+
self.year = year
|
|
61
|
+
super().__init__(
|
|
62
|
+
f"Year '{self.year}' is not a valid year (must be 4 digits)."
|
|
63
|
+
)
|
lice3/config.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Setup configuration for lice3."""
|
|
2
|
+
|
|
3
|
+
from rich.console import Console
|
|
4
|
+
from rich.panel import Panel
|
|
5
|
+
from simple_toml_settings import TOMLSettings
|
|
6
|
+
|
|
7
|
+
from lice3.constants import LICENSES
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Settings(TOMLSettings):
|
|
11
|
+
"""Settings for lice3."""
|
|
12
|
+
|
|
13
|
+
default_license: str = "bsd3"
|
|
14
|
+
organization: str = ""
|
|
15
|
+
legacy: bool = False
|
|
16
|
+
clipboard: bool = False
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def check_default_license() -> str:
|
|
20
|
+
"""Check the default license is in the list of available licenses.
|
|
21
|
+
|
|
22
|
+
Return the default license if it is in the list, otherwise return "bsd3".
|
|
23
|
+
This is only used to ensure that the configuration file does not have an
|
|
24
|
+
invalid default license hence crashing the application, and will be called
|
|
25
|
+
automatically by 'Typer'
|
|
26
|
+
"""
|
|
27
|
+
if settings.default_license not in LICENSES:
|
|
28
|
+
console = Console(width=80)
|
|
29
|
+
error_text = (
|
|
30
|
+
f"[red]Invalid default license '[b]{settings.default_license}"
|
|
31
|
+
"'[/b] in the configuration file, falling back to '[b]bsd3[/b]', "
|
|
32
|
+
"unless specified otherwise on the command line.\n\nCheck that [b]"
|
|
33
|
+
f"{settings.get_settings_folder() / settings.settings_file_name}"
|
|
34
|
+
"[/b]' has a valid value for [b]'default_license'[/b]."
|
|
35
|
+
)
|
|
36
|
+
panel = Panel(
|
|
37
|
+
error_text,
|
|
38
|
+
title="[b]Error[/b]",
|
|
39
|
+
title_align="left",
|
|
40
|
+
expand=False,
|
|
41
|
+
style="red",
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
console.print()
|
|
45
|
+
console.print(panel)
|
|
46
|
+
settings.default_license = "bsd3"
|
|
47
|
+
return settings.default_license
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
settings = Settings.get_instance(
|
|
51
|
+
"lice",
|
|
52
|
+
xdg_config=True,
|
|
53
|
+
auto_create=False,
|
|
54
|
+
allow_missing_file=True,
|
|
55
|
+
schema_version="1",
|
|
56
|
+
)
|
lice3/constants.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""Define constants for the LICE3 package."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from importlib import resources
|
|
5
|
+
|
|
6
|
+
# To extend language formatting sopport with a new language, add an item in
|
|
7
|
+
# LANGS dict:
|
|
8
|
+
# "language_suffix":"comment_name"
|
|
9
|
+
# where "language_suffix" is the suffix of your language and "comment_name" is
|
|
10
|
+
# one of the comment types supported and listed in LANG_CMT:
|
|
11
|
+
# text : no comment
|
|
12
|
+
# c : /* * */
|
|
13
|
+
# unix : #
|
|
14
|
+
# lua : --- --
|
|
15
|
+
|
|
16
|
+
# if you want add a new comment type just add an item to LANG_CMT:
|
|
17
|
+
# "comment_name":['string', 'string', 'string']
|
|
18
|
+
# where the first string open multiline comment, second string comment every
|
|
19
|
+
# license's line and the last string close multiline comment,
|
|
20
|
+
# associate your language and source file suffix with your new comment type
|
|
21
|
+
# how explained above.
|
|
22
|
+
# EXAMPLE:
|
|
23
|
+
# LANG_CMT = {"c":['/*', '*', '*/']} # noqa: ERA001
|
|
24
|
+
# LANGS = {"cpp":"c"} # noqa: ERA001
|
|
25
|
+
# (for more examples see LANG_CMT and langs dicts below)
|
|
26
|
+
|
|
27
|
+
LANGS = {
|
|
28
|
+
"ada": "ada",
|
|
29
|
+
"adb": "ada",
|
|
30
|
+
"ads": "ada",
|
|
31
|
+
"agda": "haskell",
|
|
32
|
+
"bash": "unix",
|
|
33
|
+
"c": "c",
|
|
34
|
+
"cc": "c",
|
|
35
|
+
"clj": "lisp",
|
|
36
|
+
"cpp": "c",
|
|
37
|
+
"cs": "c",
|
|
38
|
+
"css": "c",
|
|
39
|
+
"dart": "c",
|
|
40
|
+
"el": "lisp",
|
|
41
|
+
"erl": "erlang",
|
|
42
|
+
"f": "fortran",
|
|
43
|
+
"f90": "fortran90",
|
|
44
|
+
"go": "c",
|
|
45
|
+
"h": "c",
|
|
46
|
+
"hpp": "c",
|
|
47
|
+
"hs": "haskell",
|
|
48
|
+
"html": "html",
|
|
49
|
+
"idr": "haskell",
|
|
50
|
+
"java": "java",
|
|
51
|
+
"js": "c",
|
|
52
|
+
"kt": "java",
|
|
53
|
+
"lisp": "lisp",
|
|
54
|
+
"lua": "lua",
|
|
55
|
+
"m": "c",
|
|
56
|
+
"md": "html",
|
|
57
|
+
"ml": "ml",
|
|
58
|
+
"php": "c",
|
|
59
|
+
"pl": "perl",
|
|
60
|
+
"ps": "powershell",
|
|
61
|
+
"py": "unix",
|
|
62
|
+
"rb": "ruby",
|
|
63
|
+
"r": "unix",
|
|
64
|
+
"rs": "rust",
|
|
65
|
+
"scala": "java",
|
|
66
|
+
"scm": "lisp",
|
|
67
|
+
"sh": "unix",
|
|
68
|
+
"sql": "c",
|
|
69
|
+
"swift": "c",
|
|
70
|
+
"toml": "unix",
|
|
71
|
+
"ts": "c",
|
|
72
|
+
"txt": "text",
|
|
73
|
+
"v": "c",
|
|
74
|
+
"vhdl": "ada",
|
|
75
|
+
"xml": "html",
|
|
76
|
+
"yaml": "unix",
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
LANG_CMT = {
|
|
80
|
+
"ada": ["", "--", ""],
|
|
81
|
+
"c": ["/*", " *", " */"],
|
|
82
|
+
"erlang": ["%%", "%", "%%"],
|
|
83
|
+
"fortran": ["C", "C", "C"],
|
|
84
|
+
"fortran90": ["!*", "!*", "!*"],
|
|
85
|
+
"haskell": ["{-", "", "-}"],
|
|
86
|
+
"html": ["<!--", "", "-->"],
|
|
87
|
+
"java": ["/**", " *", " */"],
|
|
88
|
+
"lisp": ["", ";;", ""],
|
|
89
|
+
"lua": ["--[[", "", "--]]"],
|
|
90
|
+
"ml": ["(*", "", "*)"],
|
|
91
|
+
"perl": ["=item", "", "=cut"],
|
|
92
|
+
"powershell": ["<#", "#", "#>"],
|
|
93
|
+
"ruby": ["=begin", "", "=end"],
|
|
94
|
+
"rust": ["", "//", ""],
|
|
95
|
+
"text": ["", "", ""],
|
|
96
|
+
"unix": ["", "#", ""],
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def get_available_licenses() -> list[str]:
|
|
101
|
+
"""Get a sorted list of available license names from template files.
|
|
102
|
+
|
|
103
|
+
Searches for templates in the current package's 'templates' directory
|
|
104
|
+
with pattern 'template-{name}.txt'.
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
List of license names sorted alphabetically
|
|
108
|
+
"""
|
|
109
|
+
# Get the current package name
|
|
110
|
+
package_name = __package__ or __name__.split(".")[0]
|
|
111
|
+
|
|
112
|
+
template_path = resources.files(package_name).joinpath("templates")
|
|
113
|
+
licenses = []
|
|
114
|
+
|
|
115
|
+
for file in template_path.iterdir():
|
|
116
|
+
if file.is_file():
|
|
117
|
+
match = re.match(r"template-([a-z0-9_]+)\.txt", file.name)
|
|
118
|
+
if match:
|
|
119
|
+
licenses.append(match.groups()[0])
|
|
120
|
+
|
|
121
|
+
return sorted(licenses)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
LICENSES = get_available_licenses()
|
lice3/core.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"""Main core of the application."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from types import SimpleNamespace
|
|
8
|
+
from typing import TYPE_CHECKING, Any
|
|
9
|
+
|
|
10
|
+
import typer
|
|
11
|
+
from rich import print as rprint
|
|
12
|
+
from rich.markup import escape
|
|
13
|
+
|
|
14
|
+
from lice3 import __version__
|
|
15
|
+
from lice3.config import check_default_license, settings
|
|
16
|
+
from lice3.constants import LANGS, LICENSES
|
|
17
|
+
from lice3.helpers import (
|
|
18
|
+
copy_to_clipboard,
|
|
19
|
+
format_license,
|
|
20
|
+
generate_header,
|
|
21
|
+
generate_license,
|
|
22
|
+
get_context,
|
|
23
|
+
get_lang,
|
|
24
|
+
get_local_year,
|
|
25
|
+
get_metadata,
|
|
26
|
+
get_suffix,
|
|
27
|
+
guess_organization,
|
|
28
|
+
list_languages,
|
|
29
|
+
list_licenses,
|
|
30
|
+
list_vars,
|
|
31
|
+
load_file_template,
|
|
32
|
+
load_package_template,
|
|
33
|
+
validate_license,
|
|
34
|
+
validate_year,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
if TYPE_CHECKING:
|
|
38
|
+
from collections.abc import Callable
|
|
39
|
+
|
|
40
|
+
app = typer.Typer(rich_markup_mode="rich")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@app.command(
|
|
44
|
+
help=(
|
|
45
|
+
"Generates a license template with context variables, and "
|
|
46
|
+
"optionally write this to a file."
|
|
47
|
+
),
|
|
48
|
+
context_settings={"help_option_names": ["-h", "--help"]},
|
|
49
|
+
)
|
|
50
|
+
def main( # noqa: PLR0913
|
|
51
|
+
license_name: str = typer.Argument(
|
|
52
|
+
default=check_default_license(),
|
|
53
|
+
help=f"The license to generate, one of: {', '.join(LICENSES)}",
|
|
54
|
+
callback=validate_license,
|
|
55
|
+
metavar="[license]",
|
|
56
|
+
),
|
|
57
|
+
organization: str = typer.Option(
|
|
58
|
+
guess_organization(),
|
|
59
|
+
"--org",
|
|
60
|
+
"-o",
|
|
61
|
+
help='Organization, defaults to .gitconfig or os.environ["USER"]',
|
|
62
|
+
),
|
|
63
|
+
project: str = typer.Option(
|
|
64
|
+
Path.cwd().name,
|
|
65
|
+
"--proj",
|
|
66
|
+
"-p",
|
|
67
|
+
help="Name of project, defaults to name of current directory",
|
|
68
|
+
),
|
|
69
|
+
template_path: str | None = typer.Option(
|
|
70
|
+
None,
|
|
71
|
+
"--template",
|
|
72
|
+
"-t",
|
|
73
|
+
help="Path to license template file",
|
|
74
|
+
),
|
|
75
|
+
year: str | None = typer.Option(
|
|
76
|
+
get_local_year(),
|
|
77
|
+
"--year",
|
|
78
|
+
"-y",
|
|
79
|
+
help="Copyright year",
|
|
80
|
+
callback=validate_year,
|
|
81
|
+
),
|
|
82
|
+
language: str | None = typer.Option(
|
|
83
|
+
None,
|
|
84
|
+
"--language",
|
|
85
|
+
"-l",
|
|
86
|
+
help=(
|
|
87
|
+
"Format output for language source file, one of: "
|
|
88
|
+
f"{', '.join(LANGS.keys())} "
|
|
89
|
+
f"[dim]{escape('[default: txt]')}[/dim]"
|
|
90
|
+
),
|
|
91
|
+
show_default=False,
|
|
92
|
+
),
|
|
93
|
+
ofile: str | None = typer.Option(
|
|
94
|
+
"stdout",
|
|
95
|
+
"--file",
|
|
96
|
+
"-f",
|
|
97
|
+
help=(
|
|
98
|
+
"Name of the output source file (with -l, extension can be omitted)"
|
|
99
|
+
),
|
|
100
|
+
),
|
|
101
|
+
*,
|
|
102
|
+
header: bool = typer.Option(
|
|
103
|
+
False,
|
|
104
|
+
"--header",
|
|
105
|
+
help="Generate source file header for specified license",
|
|
106
|
+
),
|
|
107
|
+
clipboard: bool = typer.Option(
|
|
108
|
+
False,
|
|
109
|
+
"--clipboard",
|
|
110
|
+
"-c",
|
|
111
|
+
help="Copy the generated license to the clipboard",
|
|
112
|
+
),
|
|
113
|
+
show_vars: bool | None = typer.Option(
|
|
114
|
+
None,
|
|
115
|
+
"--vars",
|
|
116
|
+
help="List template variables for specified license",
|
|
117
|
+
),
|
|
118
|
+
show_licenses: bool = typer.Option(
|
|
119
|
+
False,
|
|
120
|
+
"--licenses",
|
|
121
|
+
help="List available license templates and their parameters",
|
|
122
|
+
),
|
|
123
|
+
show_languages: bool = typer.Option(
|
|
124
|
+
False,
|
|
125
|
+
"--languages",
|
|
126
|
+
help="List available source code formatting languages",
|
|
127
|
+
),
|
|
128
|
+
legacy: bool = typer.Option(
|
|
129
|
+
False,
|
|
130
|
+
"--legacy",
|
|
131
|
+
help="Use legacy method to generate license",
|
|
132
|
+
),
|
|
133
|
+
version: bool = typer.Option(
|
|
134
|
+
False,
|
|
135
|
+
"--version",
|
|
136
|
+
"-v",
|
|
137
|
+
is_eager=True,
|
|
138
|
+
help="Show version info",
|
|
139
|
+
),
|
|
140
|
+
metadata: bool = typer.Option(
|
|
141
|
+
False,
|
|
142
|
+
"--metadata",
|
|
143
|
+
help=(
|
|
144
|
+
"Output a JSON string listing all available licenses and "
|
|
145
|
+
"languages This allows easy integration into other tools."
|
|
146
|
+
),
|
|
147
|
+
),
|
|
148
|
+
) -> None:
|
|
149
|
+
"""Generate a license file.
|
|
150
|
+
|
|
151
|
+
Can generate a license file, a source file header, or list available
|
|
152
|
+
licenses, template variables, and source code formatting.
|
|
153
|
+
"""
|
|
154
|
+
# deal with the '--version' flag first
|
|
155
|
+
if version:
|
|
156
|
+
rprint(
|
|
157
|
+
"\n[green]Lice3 - Generate license files for your projects."
|
|
158
|
+
f"\n[/green]Version: {__version__} "
|
|
159
|
+
"\u00a9 2013-2024\n"
|
|
160
|
+
)
|
|
161
|
+
raise typer.Exit(0)
|
|
162
|
+
|
|
163
|
+
# get the args into a dict to avoid refactoring all the code...
|
|
164
|
+
args_base: dict[str, str | bool | None] = {
|
|
165
|
+
"license": license_name,
|
|
166
|
+
"header": header,
|
|
167
|
+
"organization": organization,
|
|
168
|
+
"project": project,
|
|
169
|
+
"template_path": template_path,
|
|
170
|
+
"year": year,
|
|
171
|
+
"language": language,
|
|
172
|
+
"ofile": ofile,
|
|
173
|
+
"clipboard": clipboard or settings.clipboard,
|
|
174
|
+
"legacy": legacy or settings.legacy,
|
|
175
|
+
"list_vars": show_vars,
|
|
176
|
+
"list_licenses": show_licenses,
|
|
177
|
+
"list_languages": show_languages,
|
|
178
|
+
}
|
|
179
|
+
# convert to SimpleNamespace, so we can use dot notation
|
|
180
|
+
args = SimpleNamespace(**args_base)
|
|
181
|
+
|
|
182
|
+
# get the language if set
|
|
183
|
+
lang = get_lang(args)
|
|
184
|
+
|
|
185
|
+
actions: list[tuple[bool, Callable[..., None], list[Any]]] = [
|
|
186
|
+
(metadata, get_metadata, [args]),
|
|
187
|
+
(args.list_licenses, list_licenses, []),
|
|
188
|
+
(args.list_languages, list_languages, []),
|
|
189
|
+
(header, generate_header, [args, lang]),
|
|
190
|
+
(args.list_vars, list_vars, [args, license_name]),
|
|
191
|
+
]
|
|
192
|
+
|
|
193
|
+
# Iterate through the list and call the utility functions based on the
|
|
194
|
+
# conditions. All the utility functions exit the program after execution.
|
|
195
|
+
# This saves us from having to write a lot of if-else statements.
|
|
196
|
+
for condition, func, func_args in actions:
|
|
197
|
+
if condition:
|
|
198
|
+
func(*func_args)
|
|
199
|
+
|
|
200
|
+
# create context
|
|
201
|
+
if args.template_path:
|
|
202
|
+
template = load_file_template(args.template_path)
|
|
203
|
+
else:
|
|
204
|
+
template = load_package_template(license_name)
|
|
205
|
+
|
|
206
|
+
content = generate_license(template, get_context(args))
|
|
207
|
+
|
|
208
|
+
if args.ofile != "stdout":
|
|
209
|
+
ext = get_suffix(args.ofile)
|
|
210
|
+
if ext:
|
|
211
|
+
output = args.ofile
|
|
212
|
+
out = format_license(
|
|
213
|
+
content, ext, legacy=args.legacy
|
|
214
|
+
) # format license by file suffix
|
|
215
|
+
else:
|
|
216
|
+
output = f"{args.ofile}.{lang}" if lang else args.ofile
|
|
217
|
+
out = format_license(content, lang, legacy=args.legacy)
|
|
218
|
+
|
|
219
|
+
out.seek(0)
|
|
220
|
+
with Path(output).open(mode="w") as f:
|
|
221
|
+
f.write(out.getvalue())
|
|
222
|
+
else:
|
|
223
|
+
out = format_license(content, lang, legacy=args.legacy)
|
|
224
|
+
out.seek(0)
|
|
225
|
+
if not args.clipboard:
|
|
226
|
+
sys.stdout.write(out.getvalue())
|
|
227
|
+
else:
|
|
228
|
+
copy_to_clipboard(out)
|
|
229
|
+
|
|
230
|
+
out.close()
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
if __name__ == "__main__":
|
|
234
|
+
app() # pragma: no cover
|