From 38b299d4a4fcf6f7c7c9f8ccc18b18bdee3ec9f1 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Sun, 28 Nov 2021 15:04:39 +0100 Subject: [PATCH] Use importlib.metadata to find the version in Sphinx docs --- docs/conf.py | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 42af10f8..aa42845b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,30 +1,4 @@ -import codecs -import os -import re - - -def read(*parts): - """ - Build an absolute path from *parts* and and return the contents of the - resulting file. Assume UTF-8 encoding. - """ - here = os.path.abspath(os.path.dirname(__file__)) - with codecs.open(os.path.join(here, *parts), "rb", "utf-8") as f: - return f.read() - - -def find_version(*file_paths): - """ - Build a path from *file_paths* and search for a ``__version__`` - string inside. - """ - version_file = read(*file_paths) - version_match = re.search( - r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M - ) - if version_match: - return version_match.group(1) - raise RuntimeError("Unable to find version string.") +from importlib import metadata # -- General configuration ------------------------------------------------ @@ -75,11 +49,11 @@ copyright = f"2015, {author}" # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. -# -# The short X.Y version. -release = find_version("../src/attr/__init__.py") -version = release.rsplit(".", 1)[0] + # The full version, including alpha/beta/rc tags. +release = metadata.version("attrs") +# The short X.Y version. +version = release.rsplit(".", 1)[0] # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files.