Skip to content

Commit e60c24d

Browse files
committed
refactor
1 parent f90ab50 commit e60c24d

49 files changed

Lines changed: 666 additions & 1357 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

licensecheck/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Entry point for python -m licensecheck."""
22

3-
from licensecheck.cli import cli, main
3+
from licensecheck.io.cli import cli, main
44

55
_ = (cli, main)

licensecheck/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
from __future__ import annotations
44

5-
from licensecheck import cli
5+
from licensecheck.io.cli import cli
66

77
cli()

licensecheck/checker.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
from fnmatch import fnmatch
66

77
from licensecheck import license_matrix
8+
from licensecheck.models.constants import JOINS
9+
from licensecheck.models.license import License
10+
from licensecheck.models.packageinfo import PackageInfo
811
from licensecheck.packageinfo import PackageInfoManager
9-
from licensecheck.types import JOINS, License, PackageInfo, ucstr
1012

1113

1214
def check(
@@ -15,12 +17,12 @@ def check(
1517
extras: set[str],
1618
this_license: License,
1719
package_info_manager: PackageInfoManager,
18-
ignore_packages: set[ucstr] | None = None,
19-
fail_packages: set[ucstr] | None = None,
20-
ignore_licenses: set[ucstr] | None = None,
21-
fail_licenses: set[ucstr] | None = None,
22-
only_licenses: set[ucstr] | None = None,
23-
skip_dependencies: set[ucstr] | None = None,
20+
ignore_packages: set[str] | None = None,
21+
fail_packages: set[str] | None = None,
22+
ignore_licenses: set[str] | None = None,
23+
fail_licenses: set[str] | None = None,
24+
only_licenses: set[str] | None = None,
25+
skip_dependencies: set[str] | None = None,
2426
) -> tuple[bool, set[PackageInfo]]:
2527
# Def values
2628
ignore_packages = ignore_packages or set()
@@ -38,10 +40,10 @@ def check(
3840
)
3941

4042
ignoreLicensesType = license_matrix.licenseType(
41-
ucstr(JOINS.join(ignore_licenses)), ignore_licenses
43+
str(JOINS.join(ignore_licenses)), ignore_licenses
4244
)
43-
failLicensesType = license_matrix.licenseType(ucstr(JOINS.join(fail_licenses)), ignore_licenses)
44-
onlyLicensesType = license_matrix.licenseType(ucstr(JOINS.join(only_licenses)), ignore_licenses)
45+
failLicensesType = license_matrix.licenseType(str(JOINS.join(fail_licenses)), ignore_licenses)
46+
onlyLicensesType = license_matrix.licenseType(str(JOINS.join(only_licenses)), ignore_licenses)
4547
# licenseType will always return NO_LICENSE when onlyLicenses is empty
4648
if License.NO_LICENSE in onlyLicensesType:
4749
onlyLicensesType.remove(License.NO_LICENSE)
@@ -60,7 +62,7 @@ def check(
6062
else:
6163
package.licenseCompat = license_matrix.depCompatWMyLice(
6264
this_license,
63-
license_matrix.licenseType(ucstr(package.license), ignore_licenses),
65+
license_matrix.licenseType(str(package.license), ignore_licenses),
6466
ignoreLicensesType,
6567
failLicensesType,
6668
onlyLicensesType,
Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""Output the licenses used by dependencies and check if these are compatible with the project
2-
license.
3-
"""
1+
"""Output the licenses used by dependencies and check if these are compatible with the project license."""
42

53
from __future__ import annotations
64

@@ -9,12 +7,14 @@
97
from pathlib import Path
108
from sys import exit as sysexit
119
from sys import stdin, stdout
12-
from typing import Any
1310

1411
from configurator import Config
1512
from configurator.node import ConfigNode
1613

17-
from licensecheck import checker, fmt, license_matrix, packageinfo, types
14+
from licensecheck import checker, license_matrix, packageinfo
15+
from licensecheck.io import fmt
16+
from licensecheck.models.config import LC_Config
17+
from licensecheck.models.packageinfo import PackageInfo
1818

1919
stdout.reconfigure(encoding="utf-8") # type: ignore[general-type-issues]
2020

@@ -115,23 +115,11 @@ def cli() -> None: # pragma: no cover
115115
else:
116116
stdin_path.write_text("\n".join(stdin.readlines()), encoding="utf-8")
117117

118-
ec = main(args)
119-
stdin_path.unlink(missing_ok=True)
120-
121-
sysexit(ec)
122-
123-
124-
def main(args: dict[str, Any]) -> int:
125-
"""Test entry point.
126-
127-
Note: FHConfParser (Parses in the following order: `pyproject.toml`,
128-
`setup.cfg`, `licensecheck.toml`, `licensecheck.json`,
129-
`licensecheck.ini`, `~/licensecheck.toml`, `~/licensecheck.json`, `~/licensecheck.ini`)
130-
"""
131-
exitCode = 0
132-
133118
config: ConfigNode = Config()
134119

120+
# (Parses in the following order: `pyproject.toml`,
121+
# `setup.cfg`, `licensecheck.toml`, `licensecheck.json`,
122+
# `licensecheck.ini`, `~/licensecheck.toml`, `~/licensecheck.json`, `~/licensecheck.ini`)
135123
config_files = [
136124
"~/licensecheck.json",
137125
"~/licensecheck.toml",
@@ -145,71 +133,79 @@ def main(args: dict[str, Any]) -> int:
145133
config += Config.from_path(file, optional=True)
146134

147135
scopedData: ConfigNode = config.get("tool", {}).get("licensecheck", ConfigNode())
148-
scopedConfig: dict[str, Any] = {**scopedData.data, **args}
136+
licensecheckConf: LC_Config = LC_Config.from_mapping(**scopedData.data, **args)
137+
138+
ec = main(licensecheckConf)
139+
stdin_path.unlink(missing_ok=True)
140+
141+
sysexit(ec)
142+
143+
144+
def main(licensecheckConf: LC_Config) -> int:
145+
"""Test entry point."""
146+
exitCode = 0
149147

150148
# File
151-
requirements_paths = scopedConfig.get("requirements_paths") or ["__stdin__"]
149+
requirements_paths = licensecheckConf.requirements_paths or {"__stdin__"}
152150
output_file = (
153151
stdout
154-
if scopedConfig.get("file") in [None, ""]
155-
else Path(scopedConfig.get("file", "")).open("w", encoding="utf-8")
152+
if licensecheckConf.file in [None, ""]
153+
else Path(licensecheckConf.file or "").open("w", encoding="utf-8")
156154
)
157155

158156
# Get my license
159-
this_license_text = scopedConfig.get("license") or packageinfo.ProjectMetadata.get_license()
157+
this_license_text = licensecheckConf.license or packageinfo.ProjectMetadata.get_license()
160158
this_license = license_matrix.licenseType(this_license_text).pop()
161159

162-
def getFromConfig(key: str) -> set[types.ucstr]:
163-
return set(map(types.ucstr, scopedConfig.get(key, [])))
164-
165160
package_info_manager = packageinfo.PackageInfoManager(
166-
scopedConfig.get("pypi_api", "https://pypi.org")
161+
licensecheckConf.pypi_api or "https://pypi.org"
167162
)
168163

169164
incompatible, depsWithLicenses = checker.check(
170165
requirements_paths=set(requirements_paths),
171-
groups=set(scopedConfig.get("groups", [])),
172-
extras=set(scopedConfig.get("extras", [])),
166+
groups=licensecheckConf.groups,
167+
extras=licensecheckConf.extras,
173168
this_license=this_license,
174169
package_info_manager=package_info_manager,
175-
ignore_packages=getFromConfig("ignore_packages"),
176-
fail_packages=getFromConfig("fail_packages"),
177-
ignore_licenses=getFromConfig("ignore_licenses"),
178-
fail_licenses=getFromConfig("fail_licenses"),
179-
only_licenses=getFromConfig("only_licenses"),
180-
skip_dependencies=getFromConfig("skip_dependencies"),
170+
ignore_packages=licensecheckConf.ignore_packages,
171+
fail_packages=licensecheckConf.fail_packages,
172+
ignore_licenses=licensecheckConf.ignore_licenses,
173+
fail_licenses=licensecheckConf.fail_licenses,
174+
only_licenses=licensecheckConf.only_licenses,
175+
skip_dependencies=licensecheckConf.skip_dependencies,
181176
)
182177

183178
# Format the results
184-
hide_output_parameters = [
185-
types.ucstr(x) for x in scopedConfig.get("hide_output_parameters", [])
186-
]
187-
available_params = [param.name.upper() for param in fields(types.PackageInfo)]
179+
hide_output_parameters = licensecheckConf.hide_output_parameters
180+
181+
available_params = [param.name.upper() for param in fields(PackageInfo)]
188182
if not all(hop in available_params for hop in hide_output_parameters):
189183
msg = (
190184
f"Invalid parameter(s) in `hide_output_parameters`. "
191185
f"Valid parameters are: {', '.join(available_params)}"
192186
)
193187
raise ValueError(msg)
194-
if scopedConfig.get("format", "simple") in fmt.formatMap:
188+
189+
format_ = licensecheckConf.format or "simple"
190+
if licensecheckConf.format in fmt.formatMap:
195191
print(
196192
fmt.fmt(
197-
scopedConfig.get("format", "simple"),
193+
format_,
198194
this_license,
199195
sorted(depsWithLicenses),
200196
hide_output_parameters,
201-
show_only_failing=scopedConfig.get("show_only_failing", False),
197+
show_only_failing=licensecheckConf.show_only_failing,
202198
),
203199
file=output_file,
204200
)
205201
else:
206202
exitCode = 2
207203

208204
# Exit code of 1 if args.zero
209-
if scopedConfig.get("zero", False) and incompatible:
205+
if licensecheckConf.zero and incompatible:
210206
exitCode = 1
211207

212208
# Cleanup + exit
213-
if scopedConfig.get("file") not in [None, ""]:
209+
if licensecheckConf.file not in [None, ""]:
214210
output_file.close()
215211
return exitCode
Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
"""The formatter is reponsible for outputting the list of PackageInfo[s].
1+
"""
2+
The formatter is reponsible for outputting the list of PackageInfo[s].
23
34
Note the PackageInfo has the following attributes, that we can use to build each output format:
45
- name: str
@@ -7,7 +8,7 @@
78
- size: int = -1
89
- homePage: str
910
- author: str
10-
- license: ucstr
11+
- license: str
1112
- licenseCompat: bool
1213
- errorCode: int = 0
1314
@@ -51,7 +52,8 @@
5152
from rich.console import Console
5253
from rich.table import Table
5354

54-
from licensecheck.types import License, PackageInfo, ucstr
55+
from licensecheck.models.license import License
56+
from licensecheck.models.packageinfo import PackageInfo
5557

5658
THISDIR = Path(__file__).resolve().parent
5759

@@ -63,12 +65,12 @@
6365

6466

6567
def _printLicense(licenseEnum: License) -> str:
66-
"""Output a license as plain text.
68+
"""
69+
Output a license as plain text.
6770
6871
:param License licenseEnum: License
6972
:return str: license of plain text
7073
"""
71-
7274
licenseMap = {
7375
License.PUBLIC: "PUBLIC DOMAIN/ CC-PDDC/ CC0-1.0",
7476
License.UNLICENSE: "UNLICENSE/ WTFPL",
@@ -105,7 +107,8 @@ def _printLicense(licenseEnum: License) -> str:
105107

106108

107109
def stripAnsi(string: str) -> str:
108-
"""Strip ansi codes from a given string.
110+
"""
111+
Strip ansi codes from a given string.
109112
110113
Args:
111114
----
@@ -123,13 +126,13 @@ def ansi(
123126
myLice: License,
124127
packages: list[dict[str, Any]],
125128
) -> str:
126-
"""Format to ansi.
129+
"""
130+
Format to ansi.
127131
128132
:param License myLice: project license
129133
:param list[dict[str, Any]] packages: list of PackageCompats to format.
130134
:return str: string to send to specified output in ansi format
131135
"""
132-
133136
string = StringIO()
134137

135138
console = Console(file=string, color_system="truecolor", safe_box=False)
@@ -181,7 +184,8 @@ def plainText(
181184
myLice: License,
182185
packages: list[dict[str, Any]],
183186
) -> str:
184-
"""Format to plain text.
187+
"""
188+
Format to plain text.
185189
186190
:param License myLice: project license
187191
:param list[dict[str, Any]] packages: list of PackageCompats to format.
@@ -195,13 +199,13 @@ def markdown(
195199
myLice: License,
196200
packages: list[dict[str, Any]],
197201
) -> str:
198-
"""Format to markdown.
202+
"""
203+
Format to markdown.
199204
200205
:param License myLice: project license
201206
:param list[dict[str, Any]] packages: list of PackageCompats to format.
202207
:return str: string to send to specified output in markdown format
203208
"""
204-
205209
info = "\n".join(f"- {k}: {v}" for k, v in INFO.items())
206210
strBuf = [f"## Info\n\n{info}\n\n## Project License\n\n{_printLicense(myLice)}\n"]
207211

@@ -242,7 +246,8 @@ def html(
242246
myLice: License,
243247
packages: list[dict[str, Any]],
244248
) -> str:
245-
"""Format to html.
249+
"""
250+
Format to html.
246251
247252
:param License myLice: project license
248253
:param list[dict[str, Any]] packages: list of PackageCompats to format.
@@ -256,13 +261,13 @@ def html(
256261

257262

258263
def raw(myLice: License, packages: list[dict[str, Any]]) -> str:
259-
"""Format to json.
264+
"""
265+
Format to json.
260266
261267
:param License myLice: project license
262268
:param list[dict[str, Any]] packages: list of PackageCompats to format.
263269
:return str: string to send to specified output in json format
264270
"""
265-
266271
return json.dumps(
267272
{
268273
"info": INFO,
@@ -277,13 +282,13 @@ def rawCsv(
277282
myLice: License,
278283
packages: list[dict[str, Any]],
279284
) -> str:
280-
"""Format to csv.
285+
"""
286+
Format to csv.
281287
282288
:param License myLice: project license
283289
:param list[dict[str, Any]] packages: list of PackageCompats to format.
284290
:return str: string to send to specified output in csv format
285291
"""
286-
287292
if len(packages) == 0:
288293
return ""
289294

@@ -299,19 +304,20 @@ def fmt(
299304
format_: str,
300305
myLice: License,
301306
packages: list[PackageInfo],
302-
hide_parameters: list[ucstr] | None = None,
307+
hide_parameters: set[str] | None = None,
303308
*,
304309
show_only_failing: bool = False,
305310
) -> str:
306-
"""Format to a given format by `format_`.
311+
"""
312+
Format to a given format by `format_`.
307313
308314
:param License myLice: project license
309315
:param list[PackageInfo] packages: list of PackageCompats to format.
310-
:param list[ucstr] hide_parameters: list of parameters to ignore in the output.
316+
:param set[str] hide_parameters: set of parameters to ignore in the output.
311317
:param bool show_only_failing: output only failing packages, defaults to False.
312318
:return str: string to send to specified output in ansi format
313319
"""
314-
hide_parameters = hide_parameters or []
320+
hide_parameters = hide_parameters or set()
315321
if show_only_failing:
316322
packages = [x for x in packages if not x.licenseCompat]
317323

0 commit comments

Comments
 (0)