Skip to content

Commit

Permalink
fix compatiblity with packaging>=22
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Dec 23, 2022
1 parent a857a30 commit 4c8b99a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
@@ -1,3 +1,11 @@
Version 2.0.3
-------------

Released 2022-12-23

- Fix compatibility with ``packaging>=22``.


Version 2.0.2
-------------

Expand Down
52 changes: 26 additions & 26 deletions src/pallets_sphinx_themes/versions.py
Expand Up @@ -53,7 +53,7 @@ def local_versions(app):
if version.slug == slug:
versions[i] = version._replace(current=True)

if not seen_latest and _is_version(version.slug):
if not seen_latest and version.version is not None:
seen_latest = True
versions[i] = version._replace(latest=True)

Expand All @@ -63,47 +63,45 @@ def local_versions(app):
def readthedocs_versions(app):
config_versions = app.config.html_context["versions"]
current_slug = app.config.html_context["current_version"]
versions = []
number_versions = []
name_versions = []

for slug, _ in config_versions:
dev = slug in {"main", "master", "default", "latest"}
version = _parse_version(slug)

if dev:
name = "Development"
elif not _is_version(slug):
name = slug.title()
else:
if version is not None:
name = slug
append_to = number_versions
else:
name = "Development" if dev else slug.title()
append_to = name_versions

versions.append(
append_to.append(
DocVersion(name=name, slug=slug, dev=dev, current=slug == current_slug)
)

versions.sort(key=lambda x: x.version, reverse=True)
versions.sort(key=lambda x: not x.dev)
# put the newest numbered version first
number_versions.sort(key=lambda x: x.version, reverse=True)
# put non-dev named versions first
name_versions.sort(key=lambda x: x.dev, reverse=True)
versions = number_versions + name_versions

for i, version in enumerate(versions):
if _is_version(version.slug):
versions[i] = version._replace(latest=True)
break
else:
for i, version in enumerate(versions):
if version.slug == "stable":
versions[i] = version._replace(latest=True)
break
# if there are non-dev versions, mark the newest one as the latest
if versions and not versions[0].dev:
versions[0] = versions[0]._replace(latest=True)

return versions


def _is_version(value, placeholder="x"):
def _parse_version(value: str, placeholder: str = "x"):
if value.endswith(f".{placeholder}"):
value = value[: -(len(placeholder) + 1)]

try:
pv.Version(value)
return True
return pv.Version(value)
except pv.InvalidVersion:
return False
return None


class DocVersion(
Expand All @@ -113,9 +111,9 @@ class DocVersion(

def __new__(cls, name, slug=None, latest=False, dev=False, current=False):
slug = slug or name
version = pv.parse(slug)
version = _parse_version(slug)

if _is_version(slug):
if version is not None:
name = "Version " + name

return super().__new__(cls, name, slug, version, latest, dev, current)
Expand All @@ -138,7 +136,9 @@ def banner(self, context):

latest = context["latest_version"]

if latest is None:
# Don't show a banner if the latest version couldn't be determined, or if this
# is the "stable" version.
if latest is None or self.name == "stable":
return

if self.dev:
Expand Down

0 comments on commit 4c8b99a

Please sign in to comment.