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

25 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-21 12:06 +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 

44 # Use getattr to avoid static analyzers complaining about sys._MEIPASS 

45 base_path = getattr(sys, "_MEIPASS", None) 

46 if base_path is None: 

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

48 

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

50 

51def init_from_metadata(): 

52 # lobster-trace: SwRequirements.sw_req_version 

53 """Initialize dunders from importlib.metadata 

54 Requires that the package was installed. 

55 

56 Returns: 

57 list: Tool related information 

58 """ 

59 

60 my_metadata = meta.metadata('pyTRLCConverter') 

61 

62 return \ 

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

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

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

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

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

68 

69def init_from_toml(): 

70 # lobster-trace: SwRequirements.sw_req_version 

71 """Initialize dunders from pypackage.toml file 

72 

73 Tried if package wasn't installed. 

74 

75 Returns: 

76 list: Tool related information 

77 """ 

78 

79 toml_file = resource_path("pyproject.toml") 

80 data = toml.load(toml_file) 

81 

82 return \ 

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

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

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

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

87 data["project"]["license"]["text"] 

88 

89# Main ************************************************************************* 

90 

91try: 

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

93 

94except meta.PackageNotFoundError: 

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