Coverage for pyVersioning/Configuration.py: 98%
59 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-05-30 22:21 +0000
« 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
35from ruamel.yaml import YAML
36from pyTooling.Decorators import export
37from pyTooling.MetaClasses import ExtendedType
38from pyTooling.Versioning import SemanticVersion
41@export
42class Base(metaclass=ExtendedType):
43 """Base-class for all configuration items."""
45 root: 'Base' #: Reference to configuration root node (document node).
46 parent: Nullable['Base'] #: Reference to parent configuration node.
48 def __init__(self, root: 'Base', parent: Nullable['Base']) -> None:
49 """
50 Initialize base-class fields.
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
59class Project(Base):
60 """Configuration class describing a *project*."""
62 name: str
63 variant: Nullable[str]
64 version: Nullable[SemanticVersion]
66 def __init__(self, root: 'Base', parent: 'Base', settings: Dict) -> None:
67 super().__init__(root, parent)
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
74class Compiler(Base):
75 """Configuration class describing a *compiler*."""
77 name: str
78 version: str
79 configuration: str
80 options: str
82 def __init__(self, root: 'Base', parent: 'Base', settings: Dict) -> None:
83 super().__init__(root, parent)
85 self.name = settings["name"]
86 self.version = settings["version"]
87 self.configuration = settings["configuration"]
88 self.options = settings["options"]
91class Build(Base):
92 """Configuration class describing a *build*."""
94 compiler: Nullable[Compiler]
96 def __init__(self, root: 'Base', parent: 'Base', settings: Dict) -> None:
97 super().__init__(root, parent)
99 self.compiler = Compiler(root, self, settings["compiler"]) if "compiler" in settings else None
102@export
103class Configuration(Base):
104 """Configuration root node (document node)."""
106 version: int
107 project: Nullable[Project]
108 build: Nullable[Build]
110 def __init__(self, configFile: Nullable[Path] = None) -> None:
111 super().__init__(self, None)
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)
131 def load(self, configFile: Path):
132 # TODO: change to pyTooling.Configuration
133 yaml = YAML()
134 config = yaml.load(configFile)
136 self.version = int(config["version"]) if "version" in config else 1
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)
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