Skip to content

Commit b2b50f4

Browse files
committed
rel 2024.2
1 parent 966703a commit b2b50f4

File tree

10 files changed

+150
-12
lines changed

10 files changed

+150
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
All major and minor version changes will be documented in this file. Details of
44
patch-level version changes can be found in [commit messages](../../commits/master).
55

6+
## 2024.2 - 2024/04/04
7+
8+
- Add html output using `markdown` lib https://github.com/FHPythonUtils/LicenseCheck/issues/77
9+
610
## 2024.1.5 - 2024/04/04
711

812
- fix critical TypeError: can only join an iterable

documentation/reference/licensecheck/formatter.md

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Formatter](#formatter)
88
- [_printLicense](#_printlicense)
99
- [ansi](#ansi)
10+
- [html](#html)
1011
- [markdown](#markdown)
1112
- [plainText](#plaintext)
1213
- [raw](#raw)
@@ -15,7 +16,7 @@
1516

1617
## _printLicense
1718

18-
[Show source in formatter.py:46](../../../licensecheck/formatter.py#L46)
19+
[Show source in formatter.py:51](../../../licensecheck/formatter.py#L51)
1920

2021
Output a license as plain text.
2122

@@ -42,7 +43,7 @@ def _printLicense(licenseEnum: License) -> str: ...
4243

4344
## ansi
4445

45-
[Show source in formatter.py:103](../../../licensecheck/formatter.py#L103)
46+
[Show source in formatter.py:108](../../../licensecheck/formatter.py#L108)
4647

4748
Format to ansi.
4849

@@ -75,9 +76,44 @@ def ansi(
7576

7677

7778

79+
## html
80+
81+
[Show source in formatter.py:253](../../../licensecheck/formatter.py#L253)
82+
83+
Format to html.
84+
85+
#### Arguments
86+
87+
----
88+
- `myLice` *License* - project license
89+
- `packages` *list[PackageInfo]* - list of PackageCompats to format.
90+
- `hide_parameters` *list[str]* - list of parameters to ignore in the output.
91+
92+
#### Returns
93+
94+
-------
95+
- `str` - string to send to specified output in html format
96+
97+
#### Signature
98+
99+
```python
100+
def html(
101+
myLice: License,
102+
packages: list[PackageInfo],
103+
hide_parameters: list[ucstr] | None = None,
104+
) -> str: ...
105+
```
106+
107+
#### See also
108+
109+
- [License](./types.md#license)
110+
- [PackageInfo](./types.md#packageinfo)
111+
112+
113+
78114
## markdown
79115

80-
[Show source in formatter.py:193](../../../licensecheck/formatter.py#L193)
116+
[Show source in formatter.py:198](../../../licensecheck/formatter.py#L198)
81117

82118
Format to markdown.
83119

@@ -112,7 +148,7 @@ def markdown(
112148

113149
## plainText
114150

115-
[Show source in formatter.py:170](../../../licensecheck/formatter.py#L170)
151+
[Show source in formatter.py:175](../../../licensecheck/formatter.py#L175)
116152

117153
Format to ansi.
118154

@@ -147,7 +183,7 @@ def plainText(
147183

148184
## raw
149185

150-
[Show source in formatter.py:248](../../../licensecheck/formatter.py#L248)
186+
[Show source in formatter.py:278](../../../licensecheck/formatter.py#L278)
151187

152188
Format to json.
153189

@@ -182,7 +218,7 @@ def raw(
182218

183219
## rawCsv
184220

185-
[Show source in formatter.py:278](../../../licensecheck/formatter.py#L278)
221+
[Show source in formatter.py:308](../../../licensecheck/formatter.py#L308)
186222

187223
Format to csv.
188224

@@ -217,7 +253,7 @@ def rawCsv(
217253

218254
## stripAnsi
219255

220-
[Show source in formatter.py:88](../../../licensecheck/formatter.py#L88)
256+
[Show source in formatter.py:93](../../../licensecheck/formatter.py#L93)
221257

222258
Strip ansi codes from a given string.
223259

licensecheck/formatter.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- ansi
1919
- plain
2020
- markdown
21+
- html
2122
- json
2223
- csv
2324
"""
@@ -30,12 +31,16 @@
3031
from collections import OrderedDict
3132
from importlib.metadata import PackageNotFoundError, version
3233
from io import StringIO
34+
from pathlib import Path
3335

36+
import markdown as markdownlib
3437
from rich.console import Console
3538
from rich.table import Table
3639

3740
from licensecheck.types import License, PackageInfo, ucstr
3841

42+
THISDIR = Path(__file__).resolve().parent
43+
3944
try:
4045
VERSION = version("licensecheck")
4146
except PackageNotFoundError:
@@ -245,6 +250,31 @@ def markdown(
245250
return "\n".join(strBuf) + "\n"
246251

247252

253+
def html(
254+
myLice: License,
255+
packages: list[PackageInfo],
256+
hide_parameters: list[ucstr] | None = None,
257+
) -> str:
258+
"""Format to html.
259+
260+
Args:
261+
----
262+
myLice (License): project license
263+
packages (list[PackageInfo]): list of PackageCompats to format.
264+
hide_parameters (list[str]): list of parameters to ignore in the output.
265+
266+
Returns:
267+
-------
268+
str: string to send to specified output in html format
269+
270+
"""
271+
html = markdownlib.markdown(
272+
markdown(myLice=myLice, packages=packages, hide_parameters=hide_parameters),
273+
extensions=["tables"],
274+
)
275+
return (THISDIR / "html.template").read_text("utf-8").replace("{html}", html)
276+
277+
248278
def raw(
249279
myLice: License,
250280
packages: list[PackageInfo],
@@ -306,6 +336,7 @@ def rawCsv(
306336
formatMap = {
307337
"json": raw,
308338
"markdown": markdown,
339+
"html": html,
309340
"csv": rawCsv,
310341
"ansi": ansi,
311342
"simple": plainText,

licensecheck/html.template

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<style>
5+
body {
6+
font-family: Arial, sans-serif;
7+
}
8+
h2 {
9+
color: #333;
10+
margin-bottom: 10px;
11+
}
12+
ul {
13+
list-style-type: none;
14+
padding: 0;
15+
}
16+
li {
17+
margin-bottom: 5px;
18+
}
19+
table {
20+
border-collapse: collapse;
21+
width: 100%;
22+
}
23+
th, td {
24+
border: 1px solid #ddd;
25+
padding: 8px;
26+
text-align: left;
27+
}
28+
th {
29+
background-color: #f2f2f2;
30+
}
31+
tr:nth-child(even) {
32+
background-color: #f2f2f2;
33+
}
34+
tr:hover {
35+
background-color: #ddd;
36+
}
37+
h3 {
38+
color: #555;
39+
}
40+
</style>
41+
</head>
42+
<body>
43+
{html}
44+
</body>

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "licensecheck"
3-
version = "2024.1.5"
3+
version = "2024.2"
44
license = "mit"
55
description = "Output the licenses used by dependencies and check if these are compatible with the project license"
66
authors = ["FredHappyface"]
@@ -36,6 +36,7 @@ requests-cache = "<2,>=1.2.0"
3636
packaging = "<25,>=24.0"
3737
loguru = "<2,>=0.7.2"
3838
appdirs = "<2,>=1.4.4"
39+
markdown = "<4,>=3.6"
3940

4041
[tool.poetry.group.dev.dependencies]
4142
pytest = "^8.1.1"

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
appdirs<2,>=1.4.4
22
fhconfparser<2026,>=2024.1
33
loguru<2,>=0.7.2
4+
markdown<4,>=3.6
45
packaging<25,>=24.0
56
requests-cache<2,>=1.2.0
67
requests<3,>=2.31.0

tests/data/test_main_tc1_expected.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
1414
┃ Compatible ┃ Package ┃ License(s) ┃
1515
┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
16+
│ ✔ │ Markdown │ BSD LICENSE │
1617
│ ✔ │ Pygments │ BSD LICENSE │
1718
│ ✔ │ aiocontextvars │ BSD LICENSE │
1819
│ ✔ │ appdirs │ MIT LICENSE │
@@ -23,6 +24,7 @@
2324
│ ✔ │ colorama │ BSD LICENSE │
2425
│ ✔ │ fhconfparser │ MIT LICENSE │
2526
│ ✔ │ idna │ BSD LICENSE │
27+
│ ✔ │ importlib-metadata │ APACHE SOFTWARE LICENSE │
2628
│ ✔ │ loguru │ MIT LICENSE │
2729
│ ✔ │ markdown-it-py │ MIT LICENSE │
2830
│ ✔ │ packaging │ APACHE SOFTWARE LICENSE;; BSD LICENSE │

tests/data/test_main_tc3_expected.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
1414
┃ Compatible ┃ Package ┃ License(s) ┃
1515
┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
16+
│ ✔ │ Markdown │ BSD LICENSE │
1617
│ ✔ │ Pygments │ BSD LICENSE │
1718
│ ✔ │ aiocontextvars │ BSD LICENSE │
1819
│ ✔ │ appdirs │ MIT LICENSE │
@@ -23,6 +24,7 @@
2324
│ ✔ │ colorama │ BSD LICENSE │
2425
│ ✔ │ fhconfparser │ MIT LICENSE │
2526
│ ✔ │ idna │ BSD LICENSE │
27+
│ ✔ │ importlib-metadata │ APACHE SOFTWARE LICENSE │
2628
│ ✔ │ loguru │ MIT LICENSE │
2729
│ ✔ │ markdown-it-py │ MIT LICENSE │
2830
│ ✔ │ packaging │ APACHE SOFTWARE LICENSE;; BSD LICENSE │

tests/data/test_main_tc4_expected.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
},
77
"project_license": "GPL LICENSE",
88
"packages": [
9+
{
10+
"name": "Markdown",
11+
"homePage": "UNKNOWN",
12+
"author": "Manfred Stienstra, Yuri Takhteyev",
13+
"license": "BSD LICENSE",
14+
"licenseCompat": true,
15+
"errorCode": 0
16+
},
917
{
1018
"name": "Pygments",
1119
"homePage": "UNKNOWN",
@@ -86,6 +94,14 @@
8694
"licenseCompat": true,
8795
"errorCode": 0
8896
},
97+
{
98+
"name": "importlib-metadata",
99+
"homePage": "https://github.com/python/importlib_metadata",
100+
"author": "Jason R. Coombs",
101+
"license": "APACHE SOFTWARE LICENSE",
102+
"licenseCompat": true,
103+
"errorCode": 0
104+
},
89105
{
90106
"name": "loguru",
91107
"homePage": "https://github.com/Delgan/loguru",

tests/test_packageinfo.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
24
from typing import Any
35

@@ -87,18 +89,17 @@ def test_getModuleSize() -> None:
8789

8890
# Define test cases
8991
@pytest.mark.parametrize(
90-
"pkg_metadata, key, expected",
92+
("pkg_metadata", "key", "expected"),
9193
[
9294
({"name": "Package Name", "version": "1.0"}, "name", "Package Name"),
9395
({"name": ["Package Name"], "version": "1.0"}, "name", "Package Name"),
9496
({"name": [1], "version": "1.0"}, "name", "1"),
9597
({"name": 1, "version": "1.0"}, "name", "1"),
9698
({"name": None, "version": "1.0"}, "name", "None"),
97-
({"name": ["Package Name"], "version": "1.0"}, "name", "Package Name"),
9899
({"name": ["Package", "Name"], "version": "1.0"}, "name", "Package;; Name"),
99100
({}, "name", types.UNKNOWN),
100101
({"name": "Package Name", "version": "1.0"}, "description", types.UNKNOWN),
101102
],
102103
)
103-
def test_pkgMetadataGet(pkg_metadata: dict[str, Any], key: str, expected: str):
104-
assert packageinfo._pkgMetadataGet(pkg_metadata, key) == expected
104+
def test_pkgMetadataGet(pkg_metadata: dict[str, Any], key: str, expected: str) -> None:
105+
assert packageinfo._pkgMetadataGet(pkg_metadata, key) == expected # noqa: SLF001

0 commit comments

Comments
 (0)