-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from openzim/enhance_metadata
- Loading branch information
Showing
7 changed files
with
129 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,16 @@ dynamic = ["authors", "classifiers", "keywords", "license", "version", "urls"] | |
additional-keywords = ["awesome"] # some additional keywords | ||
kind = "scraper" # indicate this is a scraper, so that additional keywords are added | ||
|
||
# Additional author #1 | ||
[[tool.hatch.metadata.hooks.openzim-metadata.additional-authors]] | ||
name="Bob" | ||
email="[email protected]" | ||
|
||
# Additional author #2 | ||
[[tool.hatch.metadata.hooks.openzim-metadata.additional-authors]] | ||
name="Alice" | ||
email="[email protected]" | ||
|
||
# Enable the hatch-openzim build hook to install files (e.g. JS libs) at build time. | ||
[tool.hatch.build.hooks.openzim-build] | ||
toml-config = "openzim.toml" # optional location of the configuration file | ||
|
@@ -52,13 +62,15 @@ NOTA: the `dependencies` attribute is not specific to our hook(s), it is a gener | |
|
||
| Variable | Required | Description | | ||
|---|---|---| | ||
| `additional-authors` | N | List of authors that will be appended to the automatic one | | ||
| `additional-keywords` | N | List of keywords that will be appended to the automatic ones | | ||
| `kind` | N | If set to `scraper`, scrapers keywords will be automatically added as well | | ||
| `organization` | N | Override organization (otherwise detected from Github repository to set author and keyword appropriately). Case-insentive. Supported values are `openzim`, `kiwix` and `offspot` | | ||
| `preserve-authors` | N | Boolean indicating that we do not want to set `authors` metadata but use the ones of `pyproject.toml` | | ||
| `preserve-classifiers` | N | Boolean indicating that we do not want to set `classifiers` metadata but use the ones of `pyproject.toml` | | ||
| `preserve-keywords` | N | Boolean indicating that we do not want to set `keywords` metadata but use the ones of `pyproject.toml` | | ||
| `preserve-license` | N | Boolean indicating that we do not want to set `license` metadata but use the one of `pyproject.toml` | | ||
| `preserve-urls` | N | Boolean indicating that we do not want to set `urls` metadata but use the ones of `pyproject.toml` | | ||
| `additional_keywords` | N | List of keywords that will be appended to the automatically added ones | | ||
| `kind` | N | If set to `scraper`, scrapers keywords will be automatically added as well | | ||
|
||
### Behavior | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from pathlib import Path | ||
|
||
from hatch_openzim.utils import get_github_project_homepage, get_python_versions | ||
from hatch_openzim.utils import get_github_info, get_python_versions | ||
|
||
|
||
def update(root: str, config: dict, metadata: dict): | ||
|
@@ -23,19 +23,29 @@ def update(root: str, config: dict, metadata: dict): | |
"openzim metadata hook." | ||
) | ||
|
||
github_info = get_github_info(git_config_path=Path(root) / ".git/config") | ||
|
||
organization = config.get("organization", github_info.organization) | ||
|
||
if not config.get("preserve-urls", False): | ||
metadata["urls"] = { | ||
"Donate": "https://www.kiwix.org/en/support-us/", | ||
"Homepage": get_github_project_homepage( | ||
git_config_path=Path(root) / ".git/config" | ||
), | ||
"Homepage": github_info.homepage, | ||
} | ||
|
||
if not config.get("preserve-authors", False): | ||
metadata["authors"] = [{"name": "Kiwix", "email": "[email protected]"}] | ||
if str(organization).lower() in ("kiwix", "offspot"): | ||
authors = [{"name": "Kiwix", "email": "[email protected]"}] | ||
else: | ||
authors = [{"name": "openZIM", "email": "[email protected]"}] | ||
authors.extend(config.get("additional-authors", [])) | ||
metadata["authors"] = authors | ||
|
||
if not config.get("preserve-keywords", False): | ||
keywords = ["kiwix"] | ||
if str(organization).lower() in ("kiwix", "offspot"): | ||
keywords = ["kiwix"] | ||
else: | ||
keywords = ["openzim"] | ||
if config.get("kind", "") == "scraper": | ||
keywords.extend(["zim", "offline"]) | ||
keywords.extend(config.get("additional-keywords", [])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
import logging | ||
import os | ||
import sys | ||
|
||
DEFAULT_FORMAT = "%(name)s:%(levelname)s:%(message)s" | ||
|
||
# create logger | ||
logger = logging.getLogger("hatch_openzim") | ||
logger.setLevel(logging.DEBUG) | ||
|
||
# setup console logging | ||
console_handler = logging.StreamHandler(sys.stdout) | ||
console_handler.setFormatter(logging.Formatter(DEFAULT_FORMAT)) | ||
log_level = logging.getLevelName(os.getenv("HATCH_OPENZIM_LOG_LEVEL", "INFO")) | ||
logger.setLevel(log_level) | ||
console_handler.setLevel(log_level) | ||
logger.addHandler(console_handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,14 +32,14 @@ def test_metadata_nominal(metadata): | |
metadata=metadata, | ||
) | ||
|
||
assert metadata["authors"] == [{"email": "dev@kiwix.org", "name": "Kiwix"}] | ||
assert metadata["authors"] == [{"email": "dev@openzim.org", "name": "openZIM"}] | ||
assert metadata["classifiers"] == [ | ||
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: 3.10", | ||
"Programming Language :: Python :: 3.11", | ||
] | ||
assert metadata["keywords"] == ["kiwix"] | ||
assert metadata["keywords"] == ["openzim"] | ||
assert metadata["license"] == {"text": "GPL-3.0-or-later"} | ||
assert metadata["urls"] == { | ||
"Donate": "https://www.kiwix.org/en/support-us/", | ||
|
@@ -126,7 +126,53 @@ def test_metadata_additional_keywords(metadata): | |
metadata=metadata, | ||
) | ||
# we compare sets because order is not relevant | ||
assert set(metadata["keywords"]) == {"kiwix", "keyword1", "keyword2"} | ||
assert set(metadata["keywords"]) == {"openzim", "keyword1", "keyword2"} | ||
|
||
|
||
def test_metadata_additional_authors(metadata): | ||
config = {} | ||
config["additional-authors"] = [{"email": "[email protected]", "name": "Some One"}] | ||
update( | ||
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent), | ||
config=config, | ||
metadata=metadata, | ||
) | ||
# we compare sets because order is not relevant | ||
assert metadata["authors"] == [ | ||
{"email": "[email protected]", "name": "openZIM"}, | ||
{"email": "[email protected]", "name": "Some One"}, | ||
] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"organization, expected_result", | ||
[ | ||
("kiwix", "kiwix"), | ||
("Kiwix", "kiwix"), | ||
("openzim", "openzim"), | ||
("openZIM", "openzim"), | ||
("offspot", "kiwix"), | ||
("unknown", "openzim"), | ||
(None, "openzim"), | ||
], | ||
) | ||
def test_metadata_organization(organization, expected_result, metadata): | ||
config = {} | ||
if organization: | ||
config["organization"] = organization | ||
update( | ||
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent), | ||
config=config, | ||
metadata=metadata, | ||
) | ||
if expected_result == "kiwix": | ||
assert metadata["authors"] == [{"email": "[email protected]", "name": "Kiwix"}] | ||
assert metadata["keywords"] == ["kiwix"] | ||
elif expected_result == "openzim": | ||
assert metadata["authors"] == [{"email": "[email protected]", "name": "openZIM"}] | ||
assert metadata["keywords"] == ["openzim"] | ||
else: | ||
raise Exception(f"Unexpected expected result: {expected_result}") | ||
|
||
|
||
def test_metadata_is_scraper(metadata): | ||
|
@@ -138,4 +184,4 @@ def test_metadata_is_scraper(metadata): | |
metadata=metadata, | ||
) | ||
# we compare sets because order is not relevant | ||
assert set(metadata["keywords"]) == {"kiwix", "offline", "zim"} | ||
assert set(metadata["keywords"]) == {"openzim", "offline", "zim"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
import pytest | ||
|
||
from hatch_openzim.utils import get_github_project_homepage, get_python_versions | ||
from hatch_openzim.utils import GithubInfo, get_github_info, get_python_versions | ||
|
||
|
||
@pytest.fixture | ||
|
@@ -32,46 +32,55 @@ def _mock_git_config(git_origin_url: str, remote_name: str = "origin"): | |
|
||
|
||
@pytest.mark.parametrize( | ||
"git_url, expected_homepage_url", | ||
"git_url, expected_homepage_url, expected_organization, expected_repository", | ||
[ | ||
( | ||
"https://github.com/oneuser/onerepo.git", | ||
"https://github.com/oneuser/onerepo", | ||
"oneuser", | ||
"onerepo", | ||
), | ||
( | ||
"https://github.com/oneuser/onerepo", | ||
"https://github.com/oneuser/onerepo", | ||
"oneuser", | ||
"onerepo", | ||
), | ||
( | ||
"[email protected]:oneuser/one-repo.git", | ||
"https://github.com/oneuser/one-repo", | ||
"oneuser", | ||
"one-repo", | ||
), | ||
], | ||
) | ||
def test_get_github_project_homepage_valid_url( | ||
mock_git_config, git_url, expected_homepage_url | ||
mock_git_config, | ||
git_url, | ||
expected_homepage_url, | ||
expected_organization, | ||
expected_repository, | ||
): | ||
with mock_git_config(git_url) as git_config_path: | ||
assert ( | ||
get_github_project_homepage(git_config_path=git_config_path) | ||
== expected_homepage_url | ||
assert get_github_info(git_config_path=git_config_path) == GithubInfo( | ||
homepage=expected_homepage_url, | ||
organization=expected_organization, | ||
repository=expected_repository, | ||
) | ||
|
||
|
||
def test_get_github_project_homepage_invalid_url(mock_git_config): | ||
# Test the function with an invalid URL | ||
with mock_git_config("http://github.com/oneuser/onerepo.git") as git_config_path: | ||
assert ( | ||
get_github_project_homepage(git_config_path=git_config_path) | ||
== "https://www.kiwix.org" | ||
assert get_github_info(git_config_path=git_config_path) == GithubInfo( | ||
homepage="https://www.kiwix.org", organization=None, repository=None | ||
) | ||
|
||
|
||
def test_get_github_project_missing_git_config(): | ||
# Test the function with an invalid URL | ||
assert ( | ||
get_github_project_homepage(git_config_path=Path("i_m_not_here.config")) | ||
== "https://www.kiwix.org" | ||
assert get_github_info(git_config_path=Path("i_m_not_here.config")) == GithubInfo( | ||
homepage="https://www.kiwix.org", organization=None, repository=None | ||
) | ||
|
||
|
||
|
@@ -80,9 +89,8 @@ def test_get_github_project_homepage_invalid_remote(mock_git_config): | |
with mock_git_config( | ||
"https://github.com/oneuser/onerepo.git", remote_name="origin2" | ||
) as git_config_path: | ||
assert ( | ||
get_github_project_homepage(git_config_path=git_config_path) | ||
== "https://www.kiwix.org" | ||
assert get_github_info(git_config_path=git_config_path) == GithubInfo( | ||
homepage="https://www.kiwix.org", organization=None, repository=None | ||
) | ||
|
||
|
||
|