Coverage for src/pyTRLCConverter/version.py: 62%

26 statements  

« prev     ^ index     » next       coverage.py v7.10.3, created at 2025-08-14 10:59 +0000

1"""This module provides version and author information. 

2 

3 Author: Andreas Merkle (andreas.merkle@newtec.de) 

4""" 

5 

6# pyTRLCConverter - A tool to convert TRLC files to specific formats. 

7# Copyright (c) 2024 - 2025 NewTec GmbH 

8# 

9# This file is part of pyTRLCConverter program. 

10# 

11# The pyTRLCConverter program is free software: you can redistribute it and/or modify it under 

12# the terms of the GNU General Public License as published by the Free Software Foundation, 

13# either version 3 of the License, or (at your option) any later version. 

14# 

15# The pyTRLCConverter program is distributed in the hope that it will be useful, but 

16# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

17# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License along with pyTRLCConverter. 

20# If not, see <https://www.gnu.org/licenses/>. 

21 

22# Imports ********************************************************************** 

23import importlib.metadata as meta 

24import os 

25import sys 

26import toml 

27 

28# Variables ******************************************************************** 

29 

30__version__ = "???" 

31__author__ = "???" 

32__email__ = "???" 

33__repository__ = "???" 

34__license__ = "???" 

35 

36# Classes ********************************************************************** 

37 

38# Functions ******************************************************************** 

39 

40def resource_path(relative_path): 

41 # lobster-trace: SwRequirements.sw_req_version 

42 """ Get the absolute path to the resource, works for dev and for PyInstaller """ 

43 try: 

44 # PyInstaller creates a temp folder and stores path in _MEIPASS 

45 # pylint: disable=protected-access 

46 # pylint: disable=no-member 

47 base_path = sys._MEIPASS 

48 except Exception: # pylint: disable=broad-except 

49 base_path = os.path.abspath(".") 

50 

51 return os.path.join(base_path, relative_path) 

52 

53def init_from_metadata(): 

54 # lobster-trace: SwRequirements.sw_req_version 

55 """Initialize dunders from importlib.metadata 

56 Requires that the package was installed. 

57 

58 Returns: 

59 list: Tool related information 

60 """ 

61 

62 my_metadata = meta.metadata('pyTRLCConverter') 

63 

64 return \ 

65 my_metadata.get('Version', '???'), \ 

66 my_metadata.get('Author', '???'), \ 

67 my_metadata.get('Author-email', '???'), \ 

68 my_metadata.get('Project-URL', 'repository, ???').replace("repository, ", ""), \ 

69 my_metadata.get('License', '???') 

70 

71def init_from_toml(): 

72 # lobster-trace: SwRequirements.sw_req_version 

73 """Initialize dunders from pypackage.toml file 

74 

75 Tried if package wasn't installed. 

76 

77 Returns: 

78 list: Tool related information 

79 """ 

80 

81 toml_file = resource_path("pyproject.toml") 

82 data = toml.load(toml_file) 

83 

84 return \ 

85 data["project"]["version"], \ 

86 data["project"]["authors"][0]["name"], \ 

87 data["project"]["authors"][0]["email"], \ 

88 data["project"]["urls"]["repository"], \ 

89 data["project"]["license"]["text"] 

90 

91# Main ************************************************************************* 

92 

93try: 

94 __version__, __author__, __email__, __repository__, __license__ = init_from_metadata() 

95 

96except meta.PackageNotFoundError: 

97 __version__, __author__, __email__, __repository__, __license__ = init_from_toml()