Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if optional license is in available values #215

Merged
merged 9 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions geotribu_cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ class YamlHeaderMandatoryKeys(ExtendedEnum):
TITLE = "title"


class YamlHeaderAvailableLicense(ExtendedEnum):
"""Licences disponibles pour les contenus publiés sur Geotribu."""

BEERWARE = "beerware"
CC4_BY_SA = "cc4_by-sa"
CC4_BY_BC_SA = "cc4_by-nc-sa"
DEFAULT = "default"


@dataclass
class GeotribuDefaults:
"""Defaults settings for Geotribu."""
Expand Down
30 changes: 29 additions & 1 deletion geotribu_cli/content/header_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import frontmatter

from geotribu_cli.constants import GeotribuDefaults, YamlHeaderMandatoryKeys
from geotribu_cli.constants import (
GeotribuDefaults,
YamlHeaderAvailableLicense,
YamlHeaderMandatoryKeys,
)
from geotribu_cli.json.json_client import JsonFeedClient
from geotribu_cli.utils.check_image_size import get_image_dimensions_by_url
from geotribu_cli.utils.check_path import check_path
Expand All @@ -14,6 +18,7 @@
logger = logging.getLogger(__name__)
defaults_settings = GeotribuDefaults()


# ############################################################################
# ########## CLI #################
# ################################
Expand Down Expand Up @@ -144,6 +149,18 @@ def check_missing_mandatory_keys(keys: list[str]) -> tuple[bool, set[str]]:
return len(missing) == 0, missing


def check_license(license_id: str) -> bool:
"""Vérifie que la licence choisie fait partie de celles disponibles.

Args:
license: identifiant de la licence.

Returns:
True si la licence est l'une de celles disponibles.
"""
return YamlHeaderAvailableLicense.has_value(license_id)


def run(args: argparse.Namespace) -> None:
"""Run the sub command logic.

Expand Down Expand Up @@ -232,3 +249,14 @@ def run(args: argparse.Namespace) -> None:
raise ValueError(msg)
else:
logger.info("Clés de l'entête ok")

# check that license (if present) is in available licenses
if "license" in yaml_meta:
license_ok = check_license(yaml_meta["license"])
if not license_ok:
msg = f"La licence ('{yaml_meta['license']}') n'est pas dans celles disponibles ({','.join([l.value for l in YamlHeaderAvailableLicense])})"
logger.error(msg)
if args.raise_exceptions:
raise ValueError(msg)
else:
logger.info("licence ok")
1 change: 1 addition & 0 deletions tests/fixtures/content/2044-04-01_article_futur.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ categories:
comments: true
date: 2044-04-01
icon: octicons/server-16
license: gnu-gpl-3
robots: index, follow
tags:
- Fromage
Expand Down
10 changes: 8 additions & 2 deletions tests/test_yaml_header_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from geotribu_cli.content.header_check import (
check_author_md,
check_existing_tags,
check_license,
check_missing_mandatory_keys,
check_tags_order,
)
Expand Down Expand Up @@ -66,8 +67,7 @@ def test_future_mandatory_keys(self):
self.future_yaml_meta.keys()
)
self.assertFalse(all_present)
self.assertEqual(len(missing), 2)
self.assertIn("license", missing)
self.assertEqual(len(missing), 1)
self.assertIn("description", missing)

def test_author_md_ok(self):
Expand All @@ -78,3 +78,9 @@ def test_author_md_ok(self):
self.assertTrue(check_author_md("Jàne Döé", TEAM_FOLDER))
self.assertTrue(check_author_md("Jàne D'öé", TEAM_FOLDER))
self.assertFalse(check_author_md("JaneDoe", TEAM_FOLDER))

def test_license_ok(self):
self.assertTrue(check_license(self.past_yaml_meta["license"]))

def test_license_nok(self):
self.assertFalse(check_license(self.future_yaml_meta["license"]))
Loading