Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/selkies/__init__.py: 90%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

21 statements  

1# This Source Code Form is subject to the terms of the Mozilla Public 

2# License, v. 2.0. If a copy of the MPL was not distributed with this 

3# file, You can obtain one at https://mozilla.org/MPL/2.0/. 

4 

5"""Selkies package root. 

6 

7Appends the package directory to `sys.path` so vendored modules that use 

8bare (top-level) imports resolve whether the code runs as an installed 

9package or straight from a source checkout, and publishes `__version__`. 

10""" 

11 

12import os 

13import re 

14import sys 

15from importlib.metadata import PackageNotFoundError 

16from importlib.metadata import version as _distribution_version 

17 

18sys.path.append(os.path.dirname(os.path.abspath(__file__))) 

19 

20 

21def _read_version() -> str: 

22 """Resolve the version of the running package from its one source. 

23 

24 The installed distribution's metadata is authoritative: every channel 

25 (wheel, native package, conda package, AppImage) stamps the version of 

26 pyproject.toml into it at build time. A source checkout that is not 

27 installed has no metadata, so its pyproject.toml is read instead; 

28 anything else reports "unknown". 

29 """ 

30 try: 

31 return _distribution_version("selkies") 

32 except PackageNotFoundError: 

33 pass 

34 checkout = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 

35 try: 

36 with open(os.path.join(checkout, "pyproject.toml"), encoding="utf-8") as f: 

37 match = re.search(r'^version\s*=\s*"([^"]+)"', f.read(), re.MULTILINE) 

38 except OSError: 

39 match = None 

40 return match.group(1) if match else "unknown" 

41 

42 

43__version__ = _read_version()