Skip to content

Commit

Permalink
Handle markdown and plain readme text too and include content type in…
Browse files Browse the repository at this point in the history
… metadata
  • Loading branch information
vincent-hatakeyama committed Aug 29, 2024
1 parent 95a7e62 commit b80a148
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
19 changes: 15 additions & 4 deletions src/manifestoo_core/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ def _set(key: str, value: Union[None, str, List[str]]) -> None:
_set("Author", _no_nl(manifest.author))
_set("Author-email", _author_email(manifest.author))
_set("Classifier", _make_classifiers(odoo_series, manifest))
long_description = _long_description(addon)
long_description, long_description_content_type = _long_description(addon)
if long_description:
meta.set_payload(long_description)
if long_description_content_type:
_set("Description-Content-Type", long_description_content_type)

return meta

Expand Down Expand Up @@ -368,11 +370,20 @@ def _no_nl(s: Optional[str]) -> Optional[str]:
return " ".join(s.split())


def _long_description(addon: Addon) -> Optional[str]:
def _long_description(addon: Addon) -> Tuple[Optional[str], Optional[str]]:
"""
:return: a tuple with long description and its content type
"""
readme_path = addon.path / "README.rst"
if readme_path.is_file():
return readme_path.read_text(encoding="utf-8")
return addon.manifest.description
return readme_path.read_text(encoding="utf-8"), "text/x-rst"
readme_path = addon.path / "README.md"
if readme_path.is_file():
return readme_path.read_text(encoding="utf-8"), "text/markdown"
readme_path = addon.path / "README.txt"
if readme_path.is_file():
return readme_path.read_text(encoding="utf-8"), "text/plain"
return addon.manifest.description, "text/x-rst"


def _make_classifiers(odoo_series: OdooSeries, manifest: Manifest) -> List[str]:
Expand Down
25 changes: 20 additions & 5 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def _m( # noqa: PLR0913 too many arguments
summary: Optional[str] = None,
description: Optional[str] = None,
readme_rst: Optional[str] = None,
readme_md: Optional[str] = None,
depends: Optional[List[str]] = None,
external_dependencies: Optional[Dict[str, List[str]]] = None,
website: Optional[str] = None,
Expand Down Expand Up @@ -82,6 +83,9 @@ def _m( # noqa: PLR0913 too many arguments
if readme_rst:
readme_path = addon_dir / "README.rst"
readme_path.write_text(readme_rst)
if readme_md:
readme_path = addon_dir / "README.md"
readme_path.write_text(readme_md)
return msg_to_json(
metadata_from_addon_dir(
addon_dir,
Expand Down Expand Up @@ -111,6 +115,7 @@ def test_basic(tmp_path: Path) -> None:
"Framework :: Odoo :: 14.0",
],
"metadata_version": "2.1",
"description_content_type": "text/x-rst",
}


Expand Down Expand Up @@ -333,18 +338,28 @@ def test_description_from_readme(
tmp_path: Path,
readme_rst: str = "A readme\n\nwith two lines",
) -> None:
assert _m(tmp_path, readme_rst=readme_rst)["description"] == readme_rst
m = _m(tmp_path, readme_rst=readme_rst)
assert m["description"] == readme_rst
assert m["description_content_type"] == "text/x-rst"


def test_description_from_readme_md(
tmp_path: Path,
readme_md: str = "A readme\n\nwith two lines",
) -> None:
m = _m(tmp_path, readme_md=readme_md)
assert m["description"] == readme_md
assert m["description_content_type"] == "text/markdown"


def test_description_from_description_and_readme(
tmp_path: Path,
description: str = "A description",
readme_rst: str = "A readme\n\nwith two lines",
) -> None:
assert (
_m(tmp_path, description=description, readme_rst=readme_rst)["description"]
== readme_rst
)
m = _m(tmp_path, description=description, readme_rst=readme_rst)
assert m["description"] == readme_rst
assert m["description_content_type"] == "text/x-rst"


def test_author(tmp_path: Path) -> None:
Expand Down

0 comments on commit b80a148

Please sign in to comment.