Skip to content

Commit 5a69ec5

Browse files
authored
Check if optional license is in available values (#215)
2 parents b2ef10b + 7bcabca commit 5a69ec5

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

geotribu_cli/constants.py

+9
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ class YamlHeaderMandatoryKeys(ExtendedEnum):
7575
TITLE = "title"
7676

7777

78+
class YamlHeaderAvailableLicense(ExtendedEnum):
79+
"""Licences disponibles pour les contenus publiés sur Geotribu."""
80+
81+
BEERWARE = "beerware"
82+
CC4_BY_SA = "cc4_by-sa"
83+
CC4_BY_BC_SA = "cc4_by-nc-sa"
84+
DEFAULT = "default"
85+
86+
7887
@dataclass
7988
class GeotribuDefaults:
8089
"""Defaults settings for Geotribu."""

geotribu_cli/content/header_check.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
import frontmatter
77

8-
from geotribu_cli.constants import GeotribuDefaults, YamlHeaderMandatoryKeys
8+
from geotribu_cli.constants import (
9+
GeotribuDefaults,
10+
YamlHeaderAvailableLicense,
11+
YamlHeaderMandatoryKeys,
12+
)
913
from geotribu_cli.json.json_client import JsonFeedClient
1014
from geotribu_cli.utils.check_image_size import get_image_dimensions_by_url
1115
from geotribu_cli.utils.check_path import check_path
@@ -14,6 +18,7 @@
1418
logger = logging.getLogger(__name__)
1519
defaults_settings = GeotribuDefaults()
1620

21+
1722
# ############################################################################
1823
# ########## CLI #################
1924
# ################################
@@ -144,6 +149,18 @@ def check_missing_mandatory_keys(keys: list[str]) -> tuple[bool, set[str]]:
144149
return len(missing) == 0, missing
145150

146151

152+
def check_license(license_id: str) -> bool:
153+
"""Vérifie que la licence choisie fait partie de celles disponibles.
154+
155+
Args:
156+
license: identifiant de la licence.
157+
158+
Returns:
159+
True si la licence est l'une de celles disponibles.
160+
"""
161+
return YamlHeaderAvailableLicense.has_value(license_id)
162+
163+
147164
def run(args: argparse.Namespace) -> None:
148165
"""Run the sub command logic.
149166
@@ -232,3 +249,14 @@ def run(args: argparse.Namespace) -> None:
232249
raise ValueError(msg)
233250
else:
234251
logger.info("Clés de l'entête ok")
252+
253+
# check that license (if present) is in available licenses
254+
if "license" in yaml_meta:
255+
license_ok = check_license(yaml_meta["license"])
256+
if not license_ok:
257+
msg = f"La licence ('{yaml_meta['license']}') n'est pas dans celles disponibles ({','.join([l.value for l in YamlHeaderAvailableLicense])})"
258+
logger.error(msg)
259+
if args.raise_exceptions:
260+
raise ValueError(msg)
261+
else:
262+
logger.info("licence ok")

tests/fixtures/content/2044-04-01_article_futur.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ categories:
88
comments: true
99
date: 2044-04-01
1010
icon: octicons/server-16
11+
license: gnu-gpl-3
1112
robots: index, follow
1213
tags:
1314
- Fromage

tests/test_yaml_header_check.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from geotribu_cli.content.header_check import (
88
check_author_md,
99
check_existing_tags,
10+
check_license,
1011
check_missing_mandatory_keys,
1112
check_tags_order,
1213
)
@@ -66,8 +67,7 @@ def test_future_mandatory_keys(self):
6667
self.future_yaml_meta.keys()
6768
)
6869
self.assertFalse(all_present)
69-
self.assertEqual(len(missing), 2)
70-
self.assertIn("license", missing)
70+
self.assertEqual(len(missing), 1)
7171
self.assertIn("description", missing)
7272

7373
def test_author_md_ok(self):
@@ -78,3 +78,9 @@ def test_author_md_ok(self):
7878
self.assertTrue(check_author_md("Jàne Döé", TEAM_FOLDER))
7979
self.assertTrue(check_author_md("Jàne D'öé", TEAM_FOLDER))
8080
self.assertFalse(check_author_md("JaneDoe", TEAM_FOLDER))
81+
82+
def test_license_ok(self):
83+
self.assertTrue(check_license(self.past_yaml_meta["license"]))
84+
85+
def test_license_nok(self):
86+
self.assertFalse(check_license(self.future_yaml_meta["license"]))

0 commit comments

Comments
 (0)