Skip to content

Commit

Permalink
rel 2024.2
Browse files Browse the repository at this point in the history
  • Loading branch information
FredHappyface committed Apr 4, 2024
1 parent 966703a commit b2b50f4
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All major and minor version changes will be documented in this file. Details of
patch-level version changes can be found in [commit messages](../../commits/master).

## 2024.2 - 2024/04/04

- Add html output using `markdown` lib https://github.com/FHPythonUtils/LicenseCheck/issues/77

## 2024.1.5 - 2024/04/04

- fix critical TypeError: can only join an iterable
Expand Down
50 changes: 43 additions & 7 deletions documentation/reference/licensecheck/formatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Formatter](#formatter)
- [_printLicense](#_printlicense)
- [ansi](#ansi)
- [html](#html)
- [markdown](#markdown)
- [plainText](#plaintext)
- [raw](#raw)
Expand All @@ -15,7 +16,7 @@

## _printLicense

[Show source in formatter.py:46](../../../licensecheck/formatter.py#L46)
[Show source in formatter.py:51](../../../licensecheck/formatter.py#L51)

Output a license as plain text.

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

## ansi

[Show source in formatter.py:103](../../../licensecheck/formatter.py#L103)
[Show source in formatter.py:108](../../../licensecheck/formatter.py#L108)

Format to ansi.

Expand Down Expand Up @@ -75,9 +76,44 @@ def ansi(



## html

[Show source in formatter.py:253](../../../licensecheck/formatter.py#L253)

Format to html.

#### Arguments

----
- `myLice` *License* - project license
- `packages` *list[PackageInfo]* - list of PackageCompats to format.
- `hide_parameters` *list[str]* - list of parameters to ignore in the output.

#### Returns

-------
- `str` - string to send to specified output in html format

#### Signature

```python
def html(
myLice: License,
packages: list[PackageInfo],
hide_parameters: list[ucstr] | None = None,
) -> str: ...
```

#### See also

- [License](./types.md#license)
- [PackageInfo](./types.md#packageinfo)



## markdown

[Show source in formatter.py:193](../../../licensecheck/formatter.py#L193)
[Show source in formatter.py:198](../../../licensecheck/formatter.py#L198)

Format to markdown.

Expand Down Expand Up @@ -112,7 +148,7 @@ def markdown(

## plainText

[Show source in formatter.py:170](../../../licensecheck/formatter.py#L170)
[Show source in formatter.py:175](../../../licensecheck/formatter.py#L175)

Format to ansi.

Expand Down Expand Up @@ -147,7 +183,7 @@ def plainText(

## raw

[Show source in formatter.py:248](../../../licensecheck/formatter.py#L248)
[Show source in formatter.py:278](../../../licensecheck/formatter.py#L278)

Format to json.

Expand Down Expand Up @@ -182,7 +218,7 @@ def raw(

## rawCsv

[Show source in formatter.py:278](../../../licensecheck/formatter.py#L278)
[Show source in formatter.py:308](../../../licensecheck/formatter.py#L308)

Format to csv.

Expand Down Expand Up @@ -217,7 +253,7 @@ def rawCsv(

## stripAnsi

[Show source in formatter.py:88](../../../licensecheck/formatter.py#L88)
[Show source in formatter.py:93](../../../licensecheck/formatter.py#L93)

Strip ansi codes from a given string.

Expand Down
31 changes: 31 additions & 0 deletions licensecheck/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- ansi
- plain
- markdown
- html
- json
- csv
"""
Expand All @@ -30,12 +31,16 @@
from collections import OrderedDict
from importlib.metadata import PackageNotFoundError, version
from io import StringIO
from pathlib import Path

import markdown as markdownlib
from rich.console import Console
from rich.table import Table

from licensecheck.types import License, PackageInfo, ucstr

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

try:
VERSION = version("licensecheck")
except PackageNotFoundError:
Expand Down Expand Up @@ -245,6 +250,31 @@ def markdown(
return "\n".join(strBuf) + "\n"


def html(
myLice: License,
packages: list[PackageInfo],
hide_parameters: list[ucstr] | None = None,
) -> str:
"""Format to html.
Args:
----
myLice (License): project license
packages (list[PackageInfo]): list of PackageCompats to format.
hide_parameters (list[str]): list of parameters to ignore in the output.
Returns:
-------
str: string to send to specified output in html format
"""
html = markdownlib.markdown(
markdown(myLice=myLice, packages=packages, hide_parameters=hide_parameters),
extensions=["tables"],
)
return (THISDIR / "html.template").read_text("utf-8").replace("{html}", html)


def raw(
myLice: License,
packages: list[PackageInfo],
Expand Down Expand Up @@ -306,6 +336,7 @@ def rawCsv(
formatMap = {
"json": raw,
"markdown": markdown,
"html": html,
"csv": rawCsv,
"ansi": ansi,
"simple": plainText,
Expand Down
44 changes: 44 additions & 0 deletions licensecheck/html.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
}
h2 {
color: #333;
margin-bottom: 10px;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 5px;
}
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:hover {
background-color: #ddd;
}
h3 {
color: #555;
}
</style>
</head>
<body>
{html}
</body>
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "licensecheck"
version = "2024.1.5"
version = "2024.2"
license = "mit"
description = "Output the licenses used by dependencies and check if these are compatible with the project license"
authors = ["FredHappyface"]
Expand Down Expand Up @@ -36,6 +36,7 @@ requests-cache = "<2,>=1.2.0"
packaging = "<25,>=24.0"
loguru = "<2,>=0.7.2"
appdirs = "<2,>=1.4.4"
markdown = "<4,>=3.6"

[tool.poetry.group.dev.dependencies]
pytest = "^8.1.1"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
appdirs<2,>=1.4.4
fhconfparser<2026,>=2024.1
loguru<2,>=0.7.2
markdown<4,>=3.6
packaging<25,>=24.0
requests-cache<2,>=1.2.0
requests<3,>=2.31.0
Expand Down
2 changes: 2 additions & 0 deletions tests/data/test_main_tc1_expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Compatible ┃ Package ┃ License(s) ┃
┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ ✔ │ Markdown │ BSD LICENSE │
│ ✔ │ Pygments │ BSD LICENSE │
│ ✔ │ aiocontextvars │ BSD LICENSE │
│ ✔ │ appdirs │ MIT LICENSE │
Expand All @@ -23,6 +24,7 @@
│ ✔ │ colorama │ BSD LICENSE │
│ ✔ │ fhconfparser │ MIT LICENSE │
│ ✔ │ idna │ BSD LICENSE │
│ ✔ │ importlib-metadata │ APACHE SOFTWARE LICENSE │
│ ✔ │ loguru │ MIT LICENSE │
│ ✔ │ markdown-it-py │ MIT LICENSE │
│ ✔ │ packaging │ APACHE SOFTWARE LICENSE;; BSD LICENSE │
Expand Down
2 changes: 2 additions & 0 deletions tests/data/test_main_tc3_expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Compatible ┃ Package ┃ License(s) ┃
┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ ✔ │ Markdown │ BSD LICENSE │
│ ✔ │ Pygments │ BSD LICENSE │
│ ✔ │ aiocontextvars │ BSD LICENSE │
│ ✔ │ appdirs │ MIT LICENSE │
Expand All @@ -23,6 +24,7 @@
│ ✔ │ colorama │ BSD LICENSE │
│ ✔ │ fhconfparser │ MIT LICENSE │
│ ✔ │ idna │ BSD LICENSE │
│ ✔ │ importlib-metadata │ APACHE SOFTWARE LICENSE │
│ ✔ │ loguru │ MIT LICENSE │
│ ✔ │ markdown-it-py │ MIT LICENSE │
│ ✔ │ packaging │ APACHE SOFTWARE LICENSE;; BSD LICENSE │
Expand Down
16 changes: 16 additions & 0 deletions tests/data/test_main_tc4_expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
},
"project_license": "GPL LICENSE",
"packages": [
{
"name": "Markdown",
"homePage": "UNKNOWN",
"author": "Manfred Stienstra, Yuri Takhteyev",
"license": "BSD LICENSE",
"licenseCompat": true,
"errorCode": 0
},
{
"name": "Pygments",
"homePage": "UNKNOWN",
Expand Down Expand Up @@ -86,6 +94,14 @@
"licenseCompat": true,
"errorCode": 0
},
{
"name": "importlib-metadata",
"homePage": "https://github.com/python/importlib_metadata",
"author": "Jason R. Coombs",
"license": "APACHE SOFTWARE LICENSE",
"licenseCompat": true,
"errorCode": 0
},
{
"name": "loguru",
"homePage": "https://github.com/Delgan/loguru",
Expand Down
9 changes: 5 additions & 4 deletions tests/test_packageinfo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -87,18 +89,17 @@ def test_getModuleSize() -> None:

# Define test cases
@pytest.mark.parametrize(
"pkg_metadata, key, expected",
("pkg_metadata", "key", "expected"),
[
({"name": "Package Name", "version": "1.0"}, "name", "Package Name"),
({"name": ["Package Name"], "version": "1.0"}, "name", "Package Name"),
({"name": [1], "version": "1.0"}, "name", "1"),
({"name": 1, "version": "1.0"}, "name", "1"),
({"name": None, "version": "1.0"}, "name", "None"),
({"name": ["Package Name"], "version": "1.0"}, "name", "Package Name"),
({"name": ["Package", "Name"], "version": "1.0"}, "name", "Package;; Name"),
({}, "name", types.UNKNOWN),
({"name": "Package Name", "version": "1.0"}, "description", types.UNKNOWN),
],
)
def test_pkgMetadataGet(pkg_metadata: dict[str, Any], key: str, expected: str):
assert packageinfo._pkgMetadataGet(pkg_metadata, key) == expected
def test_pkgMetadataGet(pkg_metadata: dict[str, Any], key: str, expected: str) -> None:
assert packageinfo._pkgMetadataGet(pkg_metadata, key) == expected # noqa: SLF001

0 comments on commit b2b50f4

Please sign in to comment.