Coverage for pyVersioning/Configuration.py: 98%

59 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-05-30 22:21 +0000

1# ==================================================================================================================== # 

2# __ __ _ _ # 

3# _ __ _ \ \ / /__ _ __ ___(_) ___ _ __ (_)_ __ __ _ # 

4# | '_ \| | | \ \ / / _ \ '__/ __| |/ _ \| '_ \| | '_ \ / _` | # 

5# | |_) | |_| |\ V / __/ | \__ \ | (_) | | | | | | | | (_| | # 

6# | .__/ \__, | \_/ \___|_| |___/_|\___/|_| |_|_|_| |_|\__, | # 

7# |_| |___/ |___/ # 

8# ==================================================================================================================== # 

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

13# ==================================================================================================================== # 

14# Copyright 2020-2025 Patrick Lehmann - Bötzingen, Germany # 

15# # 

16# Licensed under the Apache License, Version 2.0 (the "License"); # 

17# you may not use this file except in compliance with the License. # 

18# You may obtain a copy of the License at # 

19# # 

20# http://www.apache.org/licenses/LICENSE-2.0 # 

21# # 

22# Unless required by applicable law or agreed to in writing, software # 

23# distributed under the License is distributed on an "AS IS" BASIS, # 

24# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

25# See the License for the specific language governing permissions and # 

26# limitations under the License. # 

27# # 

28# SPDX-License-Identifier: Apache-2.0 # 

29# ==================================================================================================================== # 

30# 

31"""pyVersioning configuration file in YAML format.""" 

32from pathlib import Path 

33from typing import Dict, Optional as Nullable 

34 

35from ruamel.yaml import YAML 

36from pyTooling.Decorators import export 

37from pyTooling.MetaClasses import ExtendedType 

38from pyTooling.Versioning import SemanticVersion 

39 

40 

41@export 

42class Base(metaclass=ExtendedType): 

43 """Base-class for all configuration items.""" 

44 

45 root: 'Base' #: Reference to configuration root node (document node). 

46 parent: Nullable['Base'] #: Reference to parent configuration node. 

47 

48 def __init__(self, root: 'Base', parent: Nullable['Base']) -> None: 

49 """ 

50 Initialize base-class fields. 

51 

52 :param root: Reference to configuration root node (document node). 

53 :param parent: Reference to parent configuration node. 

54 """ 

55 self.root = root 

56 self.parent = parent 

57 

58 

59class Project(Base): 

60 """Configuration class describing a *project*.""" 

61 

62 name: str 

63 variant: Nullable[str] 

64 version: Nullable[SemanticVersion] 

65 

66 def __init__(self, root: 'Base', parent: 'Base', settings: Dict) -> None: 

67 super().__init__(root, parent) 

68 

69 self.name = settings["name"] 

70 self.variant = settings["variant"] if "variant" in settings else None 

71 self.version = SemanticVersion.Parse(settings["version"]) if "version" in settings else None 

72 

73 

74class Compiler(Base): 

75 """Configuration class describing a *compiler*.""" 

76 

77 name: str 

78 version: str 

79 configuration: str 

80 options: str 

81 

82 def __init__(self, root: 'Base', parent: 'Base', settings: Dict) -> None: 

83 super().__init__(root, parent) 

84 

85 self.name = settings["name"] 

86 self.version = settings["version"] 

87 self.configuration = settings["configuration"] 

88 self.options = settings["options"] 

89 

90 

91class Build(Base): 

92 """Configuration class describing a *build*.""" 

93 

94 compiler: Nullable[Compiler] 

95 

96 def __init__(self, root: 'Base', parent: 'Base', settings: Dict) -> None: 

97 super().__init__(root, parent) 

98 

99 self.compiler = Compiler(root, self, settings["compiler"]) if "compiler" in settings else None 

100 

101 

102@export 

103class Configuration(Base): 

104 """Configuration root node (document node).""" 

105 

106 version: int 

107 project: Nullable[Project] 

108 build: Nullable[Build] 

109 

110 def __init__(self, configFile: Nullable[Path] = None) -> None: 

111 super().__init__(self, None) 

112 

113 if configFile is None: 

114 self.version = 1 

115 self.project = Project(self, self, { 

116 "name": "", 

117 "variant": "", 

118 "version": "v0.0.0" 

119 }) 

120 self.build = Build(self, self, { 

121 "compiler": { 

122 "name": "", 

123 "version": "v0.0.0", 

124 "configuration": "", 

125 "options": "" 

126 } 

127 }) 

128 else: 

129 self.load(configFile) 

130 

131 def load(self, configFile: Path): 

132 # TODO: change to pyTooling.Configuration 

133 yaml = YAML() 

134 config = yaml.load(configFile) 

135 

136 self.version = int(config["version"]) if "version" in config else 1 

137 

138 if self.version == 1: 138 ↛ exitline 138 didn't return from function 'load' because the condition on line 138 was always true

139 self.loadVersion1(config) 

140 

141 def loadVersion1(self, config): 

142 self.project = Project(self, self, config["project"]) if "project" in config else None 

143 self.build = Build(self, self, config["build"]) if "build" in config else None