Skip to content

Commit

Permalink
Generate __version__ at build to avoid slow importlib.metadata im…
Browse files Browse the repository at this point in the history
…port (#218)
  • Loading branch information
hugovk authored Aug 6, 2024
2 parents 35d42e5 + 93448f1 commit 191b1d5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@ venv.bak/
.mypy_cache/
.dmypy.json
dmypy.json

# hatch-vcs
src/norwegianblue/_version.py
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ scripts.norwegianblue = "norwegianblue.cli:main"
[tool.hatch]
version.source = "vcs"

[tool.hatch.build.hooks.vcs]
version-file = "src/norwegianblue/_version.py"

[tool.hatch.version.raw-options]
local_scheme = "no-local-version"

Expand Down
13 changes: 7 additions & 6 deletions src/norwegianblue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@
from __future__ import annotations

import datetime as dt
import importlib.metadata
import json
import logging
from functools import lru_cache

from dateutil.relativedelta import relativedelta
from termcolor import colored

from norwegianblue import _cache

__version__ = importlib.metadata.version(__name__)
from norwegianblue import _cache, _version

__version__ = _version.__version__

__all__ = ["__version__"]

BASE_URL = "https://endoflife.date/api/"
USER_AGENT = f"norwegianblue/{__version__}"


def error_404_text(product: str, suggestion: str) -> str:
Expand Down Expand Up @@ -63,7 +60,11 @@ def norwegianblue(
# No cache, or couldn't load cache
import httpx

r = httpx.get(url, follow_redirects=True, headers={"User-Agent": USER_AGENT})
r = httpx.get(
url,
follow_redirects=True,
headers={"User-Agent": f"norwegianblue/{__version__}"},
)

logging.info("HTTP status code: %d", r.status_code)
if r.status_code == 404:
Expand Down

0 comments on commit 191b1d5

Please sign in to comment.