jarbin-c-coding-style 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jarbin_c_coding_style-2.0.0.dist-info/METADATA +150 -0
- jarbin_c_coding_style-2.0.0.dist-info/RECORD +186 -0
- jarbin_c_coding_style-2.0.0.dist-info/WHEEL +4 -0
- jarbin_c_coding_style-2.0.0.dist-info/entry_points.txt +2 -0
- jarbin_c_coding_style-2.0.0.dist-info/licenses/LICENSE +674 -0
- sources/__init__.py +0 -0
- sources/__main__.py +6 -0
- sources/analysis/__init__.py +0 -0
- sources/analysis/directives.py +258 -0
- sources/analysis/lexer.py +288 -0
- sources/analysis/parser.py +1754 -0
- sources/analysis/project.py +286 -0
- sources/analysis/source.py +326 -0
- sources/analysis/syntax.py +238 -0
- sources/analysis/tokens.py +116 -0
- sources/analysis/unit.py +700 -0
- sources/application.py +118 -0
- sources/build_info.json +6 -0
- sources/cli/__init__.py +0 -0
- sources/cli/arguments.py +507 -0
- sources/cli/completion.py +610 -0
- sources/cli/context.py +258 -0
- sources/cli/converters.py +204 -0
- sources/cli/help.py +162 -0
- sources/cli/parser.py +659 -0
- sources/configs/__init__.py +0 -0
- sources/configs/editor.py +207 -0
- sources/configs/ini.py +327 -0
- sources/configs/project.py +735 -0
- sources/configs/schema/__init__.py +0 -0
- sources/configs/schema/project.py +68 -0
- sources/constants.py +801 -0
- sources/diagnostics/__init__.py +0 -0
- sources/diagnostics/fingerprint.py +82 -0
- sources/diagnostics/model.py +146 -0
- sources/engine/__init__.py +0 -0
- sources/engine/api.py +355 -0
- sources/engine/examples.py +157 -0
- sources/engine/outcome.py +114 -0
- sources/engine/plan.py +440 -0
- sources/engine/runner.py +345 -0
- sources/engine/scanner.py +179 -0
- sources/enums.py +838 -0
- sources/errors/__init__.py +0 -0
- sources/errors/exceptions.py +727 -0
- sources/fixers/__init__.py +0 -0
- sources/fixers/base.py +209 -0
- sources/fixers/conflicts.py +129 -0
- sources/fixers/diff.py +73 -0
- sources/fixers/engine.py +434 -0
- sources/fixers/journal.py +290 -0
- sources/fixers/kinds.py +30 -0
- sources/fixers/layout.py +411 -0
- sources/fixers/model.py +151 -0
- sources/fixers/registry.py +54 -0
- sources/fixers/text.py +353 -0
- sources/fixers/verify.py +123 -0
- sources/fixers/writer.py +88 -0
- sources/integrations/__init__.py +0 -0
- sources/integrations/ci/__init__.py +0 -0
- sources/integrations/ci/github.py +183 -0
- sources/integrations/editors/__init__.py +0 -0
- sources/integrations/git/__init__.py +0 -0
- sources/integrations/git/repository.py +176 -0
- sources/integrations/update/__init__.py +0 -0
- sources/integrations/update/installers.py +529 -0
- sources/integrations/update/releases.py +397 -0
- sources/integrations/update/state.py +143 -0
- sources/outputs/__init__.py +0 -0
- sources/outputs/github/__init__.py +0 -0
- sources/outputs/github/renderer.py +135 -0
- sources/outputs/html/__init__.py +0 -0
- sources/outputs/html/renderer.py +694 -0
- sources/outputs/json/__init__.py +0 -0
- sources/outputs/json/renderer.py +154 -0
- sources/outputs/json/report.schema.json +121 -0
- sources/outputs/junit/__init__.py +0 -0
- sources/outputs/junit/renderer.py +151 -0
- sources/outputs/registry.py +135 -0
- sources/outputs/report.py +224 -0
- sources/outputs/sarif/__init__.py +0 -0
- sources/outputs/sarif/renderer.py +182 -0
- sources/outputs/terminal/__init__.py +0 -0
- sources/outputs/terminal/renderer.py +184 -0
- sources/outputs/terminal/style.py +159 -0
- sources/rules/__init__.py +0 -0
- sources/rules/arguments.py +362 -0
- sources/rules/catalog.py +227 -0
- sources/rules/categories/advanced/constant_pointers.ini +46 -0
- sources/rules/categories/advanced/line_break_at_end_of_file.ini +44 -0
- sources/rules/categories/advanced/static.ini +65 -0
- sources/rules/categories/advanced/typing.ini +47 -0
- sources/rules/categories/control_structures/conditional_branching.ini +89 -0
- sources/rules/categories/control_structures/goto.ini +45 -0
- sources/rules/categories/control_structures/ternary_operators.ini +57 -0
- sources/rules/categories/extensions/language_extensions.ini +71 -0
- sources/rules/categories/files_organization/contents_of_the_repository.ini +85 -0
- sources/rules/categories/files_organization/file_coherence.ini +166 -0
- sources/rules/categories/files_organization/file_extension.ini +68 -0
- sources/rules/categories/files_organization/fixtures/program +0 -0
- sources/rules/categories/files_organization/naming_files_and_folders.ini +74 -0
- sources/rules/categories/functions/coherence_of_functions.ini +45 -0
- sources/rules/categories/functions/comments_inside_a_function.ini +45 -0
- sources/rules/categories/functions/functions_without_parameters.ini +39 -0
- sources/rules/categories/functions/naming_functions.ini +79 -0
- sources/rules/categories/functions/number_of_columns.ini +61 -0
- sources/rules/categories/functions/number_of_lines.ini +70 -0
- sources/rules/categories/functions/number_of_parameters.ini +64 -0
- sources/rules/categories/functions/structures_as_parameters.ini +44 -0
- sources/rules/categories/global_scope/constant_values.ini +49 -0
- sources/rules/categories/global_scope/file_header.ini +92 -0
- sources/rules/categories/global_scope/global_variables.ini +53 -0
- sources/rules/categories/global_scope/include.ini +39 -0
- sources/rules/categories/global_scope/indentation_of_preprocessor.ini +62 -0
- sources/rules/categories/global_scope/inline_assembly.ini +43 -0
- sources/rules/categories/global_scope/leading_trailing_lines.ini +58 -0
- sources/rules/categories/global_scope/line_endings.ini +53 -0
- sources/rules/categories/global_scope/separation_of_functions.ini +61 -0
- sources/rules/categories/global_scope/trailing_spaces.ini +45 -0
- sources/rules/categories/header_files/content.ini +104 -0
- sources/rules/categories/header_files/include_guard.ini +47 -0
- sources/rules/categories/header_files/macros.ini +57 -0
- sources/rules/categories/layout_inside_a_function_scope/blank_lines.ini +71 -0
- sources/rules/categories/layout_inside_a_function_scope/code_line_content.ini +112 -0
- sources/rules/categories/layout_inside_a_function_scope/curly_brackets.ini +94 -0
- sources/rules/categories/layout_inside_a_function_scope/indentation.ini +96 -0
- sources/rules/categories/layout_inside_a_function_scope/spaces.ini +113 -0
- sources/rules/categories/layout_inside_a_function_scope/variable_declarations.ini +65 -0
- sources/rules/categories/variables_and_types/naming_identifiers.ini +74 -0
- sources/rules/categories/variables_and_types/pointers.ini +67 -0
- sources/rules/categories/variables_and_types/structures.ini +47 -0
- sources/rules/data/verbs.txt +182 -0
- sources/rules/definition.py +430 -0
- sources/rules/loader.py +925 -0
- sources/rules/paths.py +6 -0
- sources/rules/primitives/__init__.py +0 -0
- sources/rules/primitives/base.py +297 -0
- sources/rules/primitives/braces.py +211 -0
- sources/rules/primitives/directives.py +203 -0
- sources/rules/primitives/indentation.py +376 -0
- sources/rules/primitives/layout.py +199 -0
- sources/rules/primitives/naming.py +277 -0
- sources/rules/primitives/project.py +62 -0
- sources/rules/primitives/registry.py +49 -0
- sources/rules/primitives/repository.py +129 -0
- sources/rules/primitives/review.py +87 -0
- sources/rules/primitives/semantic.py +319 -0
- sources/rules/primitives/spacing.py +210 -0
- sources/rules/primitives/structure.py +586 -0
- sources/rules/primitives/syntax.py +200 -0
- sources/rules/primitives/text.py +408 -0
- sources/rules/primitives/tokens.py +84 -0
- sources/rules/template.py +138 -0
- sources/selftest/__init__.py +0 -0
- sources/selftest/cases.py +292 -0
- sources/services.py +90 -0
- sources/subcommands/__init__.py +0 -0
- sources/subcommands/base.py +45 -0
- sources/subcommands/check/__init__.py +0 -0
- sources/subcommands/check/command.py +45 -0
- sources/subcommands/check/fixing.py +224 -0
- sources/subcommands/check/handler.py +757 -0
- sources/subcommands/info/__init__.py +0 -0
- sources/subcommands/info/command.py +45 -0
- sources/subcommands/info/handler.py +527 -0
- sources/subcommands/registry.py +37 -0
- sources/subcommands/rule/__init__.py +0 -0
- sources/subcommands/rule/command.py +45 -0
- sources/subcommands/rule/handler.py +1067 -0
- sources/subcommands/rule/render.py +273 -0
- sources/subcommands/runner.py +124 -0
- sources/subcommands/test/__init__.py +0 -0
- sources/subcommands/test/command.py +45 -0
- sources/subcommands/test/handler.py +166 -0
- sources/subcommands/test/render.py +219 -0
- sources/subcommands/test/runner.py +430 -0
- sources/subcommands/update/__init__.py +0 -0
- sources/subcommands/update/command.py +45 -0
- sources/subcommands/update/handler.py +408 -0
- sources/utils/__init__.py +0 -0
- sources/utils/build_info.py +188 -0
- sources/utils/git.py +73 -0
- sources/utils/globs.py +98 -0
- sources/utils/logging.py +154 -0
- sources/utils/schema.py +77 -0
- sources/utils/versions.py +87 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from sources.enums import SeverityLevel
|
|
4
|
+
|
|
5
|
+
# jarbin-toolkit-console prints a banner and sleeps one second at import unless this variable is "1".
|
|
6
|
+
os.environ.setdefault("JARBIN_CONSOLE_NO_BANNER", "1")
|
|
7
|
+
|
|
8
|
+
from jarbin_toolkit_console import Text # noqa: E402
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Style:
|
|
12
|
+
"""
|
|
13
|
+
Adapter over jarbin-toolkit-console (plan 01, ARCH-04): the only place where terminal colours and
|
|
14
|
+
text styles are produced. When disabled, every method returns its text unchanged.
|
|
15
|
+
|
|
16
|
+
Attributes
|
|
17
|
+
----------
|
|
18
|
+
enabled : bool
|
|
19
|
+
Whether ANSI sequences are produced.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self, enabled: bool) -> None:
|
|
23
|
+
"""
|
|
24
|
+
Create the adapter.
|
|
25
|
+
|
|
26
|
+
Parameters
|
|
27
|
+
----------
|
|
28
|
+
enabled : bool
|
|
29
|
+
Whether ANSI sequences are produced.
|
|
30
|
+
|
|
31
|
+
Returns
|
|
32
|
+
----------
|
|
33
|
+
None
|
|
34
|
+
Nothing.
|
|
35
|
+
"""
|
|
36
|
+
self.enabled: bool = enabled
|
|
37
|
+
|
|
38
|
+
def bold(self, text: str) -> str:
|
|
39
|
+
"""
|
|
40
|
+
Bold text.
|
|
41
|
+
|
|
42
|
+
Parameters
|
|
43
|
+
----------
|
|
44
|
+
text : str
|
|
45
|
+
The text.
|
|
46
|
+
|
|
47
|
+
Returns
|
|
48
|
+
----------
|
|
49
|
+
str
|
|
50
|
+
The styled text.
|
|
51
|
+
"""
|
|
52
|
+
return str(Text(text).f_style_bold()) if self.enabled else text
|
|
53
|
+
|
|
54
|
+
def dim(self, text: str) -> str:
|
|
55
|
+
"""
|
|
56
|
+
Faint text (secondary information).
|
|
57
|
+
|
|
58
|
+
Parameters
|
|
59
|
+
----------
|
|
60
|
+
text : str
|
|
61
|
+
The text.
|
|
62
|
+
|
|
63
|
+
Returns
|
|
64
|
+
----------
|
|
65
|
+
str
|
|
66
|
+
The styled text.
|
|
67
|
+
"""
|
|
68
|
+
return str(Text(text).f_style_faint()) if self.enabled else text
|
|
69
|
+
|
|
70
|
+
def success(self, text: str) -> str:
|
|
71
|
+
"""
|
|
72
|
+
Text of a successful outcome.
|
|
73
|
+
|
|
74
|
+
Parameters
|
|
75
|
+
----------
|
|
76
|
+
text : str
|
|
77
|
+
The text.
|
|
78
|
+
|
|
79
|
+
Returns
|
|
80
|
+
----------
|
|
81
|
+
str
|
|
82
|
+
The styled text.
|
|
83
|
+
"""
|
|
84
|
+
return str(Text(text).f_color_success()) if self.enabled else text
|
|
85
|
+
|
|
86
|
+
def failure(self, text: str) -> str:
|
|
87
|
+
"""
|
|
88
|
+
Text of a failed outcome.
|
|
89
|
+
|
|
90
|
+
Parameters
|
|
91
|
+
----------
|
|
92
|
+
text : str
|
|
93
|
+
The text.
|
|
94
|
+
|
|
95
|
+
Returns
|
|
96
|
+
----------
|
|
97
|
+
str
|
|
98
|
+
The styled text.
|
|
99
|
+
"""
|
|
100
|
+
return str(Text(text).f_color_failure()) if self.enabled else text
|
|
101
|
+
|
|
102
|
+
def error(self, text: str) -> str:
|
|
103
|
+
"""
|
|
104
|
+
Text of an error message.
|
|
105
|
+
|
|
106
|
+
Parameters
|
|
107
|
+
----------
|
|
108
|
+
text : str
|
|
109
|
+
The text.
|
|
110
|
+
|
|
111
|
+
Returns
|
|
112
|
+
----------
|
|
113
|
+
str
|
|
114
|
+
The styled text.
|
|
115
|
+
"""
|
|
116
|
+
return str(Text(text).f_color_error()) if self.enabled else text
|
|
117
|
+
|
|
118
|
+
def notice(self, text: str) -> str:
|
|
119
|
+
"""
|
|
120
|
+
Text of a hint or review item.
|
|
121
|
+
|
|
122
|
+
Parameters
|
|
123
|
+
----------
|
|
124
|
+
text : str
|
|
125
|
+
The text.
|
|
126
|
+
|
|
127
|
+
Returns
|
|
128
|
+
----------
|
|
129
|
+
str
|
|
130
|
+
The styled text.
|
|
131
|
+
"""
|
|
132
|
+
return str(Text(text).f_color_notice()) if self.enabled else text
|
|
133
|
+
|
|
134
|
+
def severity(self, text: str, level: SeverityLevel) -> str:
|
|
135
|
+
"""
|
|
136
|
+
Text coloured by severity (fatal / major: error, minor: warning, info: info).
|
|
137
|
+
|
|
138
|
+
Parameters
|
|
139
|
+
----------
|
|
140
|
+
text : str
|
|
141
|
+
The text.
|
|
142
|
+
level : SeverityLevel
|
|
143
|
+
The severity.
|
|
144
|
+
|
|
145
|
+
Returns
|
|
146
|
+
----------
|
|
147
|
+
str
|
|
148
|
+
The styled text.
|
|
149
|
+
"""
|
|
150
|
+
if not self.enabled:
|
|
151
|
+
return text
|
|
152
|
+
styled: Text = Text(text)
|
|
153
|
+
if level is SeverityLevel.FATAL:
|
|
154
|
+
return str(styled.f_color_critical().f_style_bold())
|
|
155
|
+
if level is SeverityLevel.MAJOR:
|
|
156
|
+
return str(styled.f_color_error())
|
|
157
|
+
if level is SeverityLevel.MINOR:
|
|
158
|
+
return str(styled.f_color_warning())
|
|
159
|
+
return str(styled.f_color_info())
|
|
File without changes
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import re
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from sources.enums import ArgumentType
|
|
8
|
+
from sources.errors.exceptions import ValueConversionError
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# Longest regular expression accepted from a rule or config file (plan 03, section 7.4).
|
|
12
|
+
MAX_REGEX_LENGTH: int = 1000
|
|
13
|
+
|
|
14
|
+
# Valid argument and parameter name: snake_case words, possibly joined by dots (`placeholders.year`).
|
|
15
|
+
ARGUMENT_NAME_PATTERN: re.Pattern[str] = re.compile(r"[a-z][a-z0-9_]*(?:\.[a-z0-9_]+)*")
|
|
16
|
+
|
|
17
|
+
# `{placeholder}` marker of a template (`{{` and `}}` stand for literal braces).
|
|
18
|
+
TEMPLATE_PLACEHOLDER_PATTERN: re.Pattern[str] = re.compile(r"\{\{|\}\}|\{([a-z][a-z0-9_]*)\}")
|
|
19
|
+
|
|
20
|
+
# Prefix of a word_set value read from a file.
|
|
21
|
+
WORD_SET_FILE_PREFIX: str = "file:"
|
|
22
|
+
|
|
23
|
+
# Literal booleans.
|
|
24
|
+
BOOLEAN_VALUES: dict[str, bool] = {"true": True, "false": False}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True, slots=True)
|
|
28
|
+
class TemplateValue:
|
|
29
|
+
"""
|
|
30
|
+
A text template: lines with `{placeholder}` markers, matched against the start of a file.
|
|
31
|
+
(dataclass)
|
|
32
|
+
|
|
33
|
+
Attributes
|
|
34
|
+
----------
|
|
35
|
+
lines : tuple[str, ...]
|
|
36
|
+
Template lines.
|
|
37
|
+
placeholders : tuple[str, ...]
|
|
38
|
+
Placeholder names, in order of first appearance.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
lines: tuple[str, ...]
|
|
42
|
+
placeholders: tuple[str, ...]
|
|
43
|
+
|
|
44
|
+
def compile(self, placeholder_patterns: dict[str, str] | None = None) -> re.Pattern[str]:
|
|
45
|
+
"""
|
|
46
|
+
Compile the template into an anchored pattern (one line of template per line of text).
|
|
47
|
+
|
|
48
|
+
Parameters
|
|
49
|
+
----------
|
|
50
|
+
placeholder_patterns : dict[str, str] | None
|
|
51
|
+
Regular expression of each placeholder (default: one or more characters).
|
|
52
|
+
|
|
53
|
+
Returns
|
|
54
|
+
----------
|
|
55
|
+
re.Pattern[str]
|
|
56
|
+
The pattern; each placeholder is a named group.
|
|
57
|
+
"""
|
|
58
|
+
patterns: dict[str, str] = placeholder_patterns or {}
|
|
59
|
+
seen: set[str] = set()
|
|
60
|
+
compiled_lines: list[str] = []
|
|
61
|
+
for line in self.lines:
|
|
62
|
+
parts: list[str] = []
|
|
63
|
+
position: int = 0
|
|
64
|
+
for match in TEMPLATE_PLACEHOLDER_PATTERN.finditer(line):
|
|
65
|
+
parts.append(re.escape(line[position:match.start()]))
|
|
66
|
+
if match.group(1) is None:
|
|
67
|
+
parts.append(re.escape(match.group()[0]))
|
|
68
|
+
elif match.group(1) in seen:
|
|
69
|
+
parts.append(f"(?P={match.group(1)})")
|
|
70
|
+
else:
|
|
71
|
+
seen.add(match.group(1))
|
|
72
|
+
parts.append(f"(?P<{match.group(1)}>{patterns.get(match.group(1), '.+?')})")
|
|
73
|
+
position = match.end()
|
|
74
|
+
parts.append(re.escape(line[position:]))
|
|
75
|
+
compiled_lines.append("".join(parts))
|
|
76
|
+
return re.compile("\n".join(compiled_lines))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def parse_template(text: str) -> TemplateValue:
|
|
80
|
+
"""
|
|
81
|
+
Build a template from its text.
|
|
82
|
+
|
|
83
|
+
Parameters
|
|
84
|
+
----------
|
|
85
|
+
text : str
|
|
86
|
+
Template lines joined with "\\n".
|
|
87
|
+
|
|
88
|
+
Returns
|
|
89
|
+
----------
|
|
90
|
+
TemplateValue
|
|
91
|
+
The template.
|
|
92
|
+
"""
|
|
93
|
+
names: list[str] = []
|
|
94
|
+
for match in TEMPLATE_PLACEHOLDER_PATTERN.finditer(text):
|
|
95
|
+
if match.group(1) and match.group(1) not in names:
|
|
96
|
+
names.append(match.group(1))
|
|
97
|
+
return TemplateValue(tuple(text.split("\n")), tuple(names))
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def split_list(text: str) -> list[str]:
|
|
101
|
+
"""
|
|
102
|
+
Split a list value: one item per line when it spans several lines, else comma-separated.
|
|
103
|
+
|
|
104
|
+
Parameters
|
|
105
|
+
----------
|
|
106
|
+
text : str
|
|
107
|
+
The raw value.
|
|
108
|
+
|
|
109
|
+
Returns
|
|
110
|
+
----------
|
|
111
|
+
list[str]
|
|
112
|
+
The non-empty, stripped items.
|
|
113
|
+
"""
|
|
114
|
+
separator: str = "\n" if "\n" in text else ","
|
|
115
|
+
return [item.strip() for item in text.split(separator) if item.strip()]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass(frozen=True, slots=True)
|
|
119
|
+
class ArgumentSpec:
|
|
120
|
+
"""
|
|
121
|
+
Typed definition of a value: a rule argument (`[argument:<name>]`), a primitive parameter or a project
|
|
122
|
+
config key. The same coercion is used for rule files, the project config and command-line overrides.
|
|
123
|
+
(dataclass)
|
|
124
|
+
|
|
125
|
+
Attributes
|
|
126
|
+
----------
|
|
127
|
+
name : str
|
|
128
|
+
Name of the value.
|
|
129
|
+
type : ArgumentType
|
|
130
|
+
Type of the value.
|
|
131
|
+
default : Any
|
|
132
|
+
Coerced default value (None when there is none).
|
|
133
|
+
minimum : float | None
|
|
134
|
+
Smallest accepted number (int, float, and their lists).
|
|
135
|
+
maximum : float | None
|
|
136
|
+
Largest accepted number (int, float, and their lists).
|
|
137
|
+
choices : tuple[str, ...]
|
|
138
|
+
Accepted values of an enum (also applied to list items).
|
|
139
|
+
item_type : ArgumentType | None
|
|
140
|
+
Type of the items of a list (str when None).
|
|
141
|
+
description : str
|
|
142
|
+
Human-readable description.
|
|
143
|
+
required : bool
|
|
144
|
+
For primitive parameters: whether a value must be given.
|
|
145
|
+
"""
|
|
146
|
+
|
|
147
|
+
name: str
|
|
148
|
+
type: ArgumentType
|
|
149
|
+
default: Any = None
|
|
150
|
+
minimum: float | None = None
|
|
151
|
+
maximum: float | None = None
|
|
152
|
+
choices: tuple[str, ...] = ()
|
|
153
|
+
item_type: ArgumentType | None = None
|
|
154
|
+
description: str = ""
|
|
155
|
+
required: bool = False
|
|
156
|
+
|
|
157
|
+
def coerce(self, raw: str, base_directory: Path | None = None) -> Any:
|
|
158
|
+
"""
|
|
159
|
+
Convert a raw text value into a typed value.
|
|
160
|
+
|
|
161
|
+
Parameters
|
|
162
|
+
----------
|
|
163
|
+
raw : str
|
|
164
|
+
The value as written (multi-line values joined with "\\n").
|
|
165
|
+
base_directory : Path | None
|
|
166
|
+
Directory of the defining file, used by `path` and `word_set` (`file:`) values.
|
|
167
|
+
|
|
168
|
+
Returns
|
|
169
|
+
----------
|
|
170
|
+
Any
|
|
171
|
+
The typed value.
|
|
172
|
+
|
|
173
|
+
Raises
|
|
174
|
+
----------
|
|
175
|
+
ValueConversionError
|
|
176
|
+
The value is not valid for this type.
|
|
177
|
+
"""
|
|
178
|
+
if self.type is ArgumentType.LIST:
|
|
179
|
+
item: ArgumentSpec = ArgumentSpec(
|
|
180
|
+
self.name, self.item_type or ArgumentType.STR,
|
|
181
|
+
minimum=self.minimum, maximum=self.maximum, choices=self.choices,
|
|
182
|
+
)
|
|
183
|
+
return tuple(item.coerce(part, base_directory) for part in split_list(raw))
|
|
184
|
+
return self._coerce_scalar(raw.strip() if self.type is not ArgumentType.TEMPLATE else raw, base_directory)
|
|
185
|
+
|
|
186
|
+
def _check_range(self, value: float, raw: str) -> None:
|
|
187
|
+
"""
|
|
188
|
+
Enforce `minimum` and `maximum`.
|
|
189
|
+
|
|
190
|
+
Parameters
|
|
191
|
+
----------
|
|
192
|
+
value : float
|
|
193
|
+
The number.
|
|
194
|
+
raw : str
|
|
195
|
+
The value as written.
|
|
196
|
+
|
|
197
|
+
Returns
|
|
198
|
+
----------
|
|
199
|
+
None
|
|
200
|
+
Nothing.
|
|
201
|
+
|
|
202
|
+
Raises
|
|
203
|
+
----------
|
|
204
|
+
ValueConversionError
|
|
205
|
+
The number is out of range.
|
|
206
|
+
"""
|
|
207
|
+
if self.minimum is not None and value < self.minimum:
|
|
208
|
+
raise ValueConversionError(f"must be >= {self.minimum:g}", raw)
|
|
209
|
+
if self.maximum is not None and value > self.maximum:
|
|
210
|
+
raise ValueConversionError(f"must be <= {self.maximum:g}", raw)
|
|
211
|
+
|
|
212
|
+
def _coerce_scalar(self, raw: str, base_directory: Path | None) -> Any:
|
|
213
|
+
"""
|
|
214
|
+
Convert a raw value of a non-list type.
|
|
215
|
+
|
|
216
|
+
Parameters
|
|
217
|
+
----------
|
|
218
|
+
raw : str
|
|
219
|
+
The value as written.
|
|
220
|
+
base_directory : Path | None
|
|
221
|
+
Directory of the defining file.
|
|
222
|
+
|
|
223
|
+
Returns
|
|
224
|
+
----------
|
|
225
|
+
Any
|
|
226
|
+
The typed value.
|
|
227
|
+
|
|
228
|
+
Raises
|
|
229
|
+
----------
|
|
230
|
+
ValueConversionError
|
|
231
|
+
The value is not valid for this type.
|
|
232
|
+
"""
|
|
233
|
+
match self.type:
|
|
234
|
+
case ArgumentType.INT:
|
|
235
|
+
if not re.fullmatch(r"[+-]?\d+", raw):
|
|
236
|
+
raise ValueConversionError("must be an integer", raw)
|
|
237
|
+
number: int = int(raw)
|
|
238
|
+
self._check_range(number, raw)
|
|
239
|
+
return number
|
|
240
|
+
case ArgumentType.FLOAT:
|
|
241
|
+
try:
|
|
242
|
+
real: float = float(raw)
|
|
243
|
+
except ValueError as error:
|
|
244
|
+
raise ValueConversionError("must be a number", raw) from error
|
|
245
|
+
if not math.isfinite(real):
|
|
246
|
+
raise ValueConversionError("must be a finite number", raw)
|
|
247
|
+
self._check_range(real, raw)
|
|
248
|
+
return real
|
|
249
|
+
case ArgumentType.BOOL:
|
|
250
|
+
if raw not in BOOLEAN_VALUES:
|
|
251
|
+
raise ValueConversionError("must be 'true' or 'false'", raw)
|
|
252
|
+
return BOOLEAN_VALUES[raw]
|
|
253
|
+
case ArgumentType.STR:
|
|
254
|
+
return raw
|
|
255
|
+
case ArgumentType.ENUM:
|
|
256
|
+
if raw not in self.choices:
|
|
257
|
+
raise ValueConversionError("must be one of: " + ", ".join(self.choices), raw)
|
|
258
|
+
return raw
|
|
259
|
+
case ArgumentType.REGEX:
|
|
260
|
+
if len(raw) > MAX_REGEX_LENGTH:
|
|
261
|
+
raise ValueConversionError(f"regular expressions are limited to {MAX_REGEX_LENGTH} characters", raw)
|
|
262
|
+
try:
|
|
263
|
+
return re.compile(raw)
|
|
264
|
+
except re.error as error:
|
|
265
|
+
raise ValueConversionError(f"invalid regular expression ({error})", raw) from error
|
|
266
|
+
case ArgumentType.GLOB:
|
|
267
|
+
if not raw:
|
|
268
|
+
raise ValueConversionError("must not be empty", raw)
|
|
269
|
+
return raw
|
|
270
|
+
case ArgumentType.PATH:
|
|
271
|
+
if not raw:
|
|
272
|
+
raise ValueConversionError("must not be empty", raw)
|
|
273
|
+
return (base_directory / raw) if base_directory is not None else Path(raw)
|
|
274
|
+
case ArgumentType.TEMPLATE:
|
|
275
|
+
return parse_template(raw)
|
|
276
|
+
case ArgumentType.WORD_SET:
|
|
277
|
+
return self._coerce_word_set(raw, base_directory)
|
|
278
|
+
raise ValueConversionError(f"unsupported type '{self.type}'", raw)
|
|
279
|
+
|
|
280
|
+
def _coerce_word_set(self, raw: str, base_directory: Path | None) -> frozenset[str]:
|
|
281
|
+
"""
|
|
282
|
+
Convert a word set: inline list, or `file:<path>` (one word per line, `#` comments).
|
|
283
|
+
|
|
284
|
+
Parameters
|
|
285
|
+
----------
|
|
286
|
+
raw : str
|
|
287
|
+
The value as written.
|
|
288
|
+
base_directory : Path | None
|
|
289
|
+
Directory of the defining file.
|
|
290
|
+
|
|
291
|
+
Returns
|
|
292
|
+
----------
|
|
293
|
+
frozenset[str]
|
|
294
|
+
The words, lower-cased.
|
|
295
|
+
|
|
296
|
+
Raises
|
|
297
|
+
----------
|
|
298
|
+
ValueConversionError
|
|
299
|
+
The word file cannot be read.
|
|
300
|
+
"""
|
|
301
|
+
if not raw.startswith(WORD_SET_FILE_PREFIX):
|
|
302
|
+
return frozenset(word.lower() for word in split_list(raw))
|
|
303
|
+
relative: str = raw[len(WORD_SET_FILE_PREFIX):].strip()
|
|
304
|
+
path: Path = (base_directory / relative) if base_directory is not None else Path(relative)
|
|
305
|
+
try:
|
|
306
|
+
content: str = path.read_text(encoding="utf-8")
|
|
307
|
+
except OSError as error:
|
|
308
|
+
raise ValueConversionError(f"cannot read word file '{path}' ({error.strerror})", raw) from error
|
|
309
|
+
return frozenset(
|
|
310
|
+
line.strip().lower() for line in content.splitlines()
|
|
311
|
+
if line.strip() and not line.strip().startswith("#")
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def types_compatible(parameter: ArgumentSpec, argument: ArgumentSpec) -> bool:
|
|
316
|
+
"""
|
|
317
|
+
Whether an argument can feed a parameter through an `@argument` reference.
|
|
318
|
+
|
|
319
|
+
Parameters
|
|
320
|
+
----------
|
|
321
|
+
parameter : ArgumentSpec
|
|
322
|
+
The primitive parameter.
|
|
323
|
+
argument : ArgumentSpec
|
|
324
|
+
The rule argument.
|
|
325
|
+
|
|
326
|
+
Returns
|
|
327
|
+
----------
|
|
328
|
+
bool
|
|
329
|
+
True when the types match (an int may feed a float; a str may feed an enum, a regex or a glob).
|
|
330
|
+
"""
|
|
331
|
+
if parameter.type is argument.type:
|
|
332
|
+
if parameter.type is ArgumentType.LIST:
|
|
333
|
+
return (parameter.item_type or ArgumentType.STR) is (argument.item_type or ArgumentType.STR)
|
|
334
|
+
return True
|
|
335
|
+
if parameter.type is ArgumentType.FLOAT and argument.type is ArgumentType.INT:
|
|
336
|
+
return True
|
|
337
|
+
if parameter.type is ArgumentType.WORD_SET and argument.type is ArgumentType.LIST:
|
|
338
|
+
return True
|
|
339
|
+
return False
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def adapt_reference_value(parameter: ArgumentSpec, value: Any) -> Any:
|
|
343
|
+
"""
|
|
344
|
+
Adapt an argument value to the parameter it feeds (int to float, list to word set).
|
|
345
|
+
|
|
346
|
+
Parameters
|
|
347
|
+
----------
|
|
348
|
+
parameter : ArgumentSpec
|
|
349
|
+
The primitive parameter.
|
|
350
|
+
value : Any
|
|
351
|
+
The argument value.
|
|
352
|
+
|
|
353
|
+
Returns
|
|
354
|
+
----------
|
|
355
|
+
Any
|
|
356
|
+
The adapted value.
|
|
357
|
+
"""
|
|
358
|
+
if parameter.type is ArgumentType.FLOAT and isinstance(value, int):
|
|
359
|
+
return float(value)
|
|
360
|
+
if parameter.type is ArgumentType.WORD_SET and isinstance(value, tuple):
|
|
361
|
+
return frozenset(str(word).lower() for word in value)
|
|
362
|
+
return value
|