diff --git a/polymath_code_standard/checkers/yaml.py b/polymath_code_standard/checkers/yaml.py index 87c6342..8329a34 100644 --- a/polymath_code_standard/checkers/yaml.py +++ b/polymath_code_standard/checkers/yaml.py @@ -10,11 +10,20 @@ class YamlGroup(CheckerGroup): name = 'yaml' + def register_args(self, subparser: argparse.ArgumentParser) -> None: + super().register_args(subparser) + subparser.add_argument( + '--no-explicit-start', + action='store_true', + help='Do not add --- header to YAML files', + ) + def run(self, args: argparse.Namespace) -> list[Result]: if not args.files: return [Result(name='yamlfix', passed=True, skipped=True)] errors, changed = [], [] - for filepath, was_changed, error in format_yaml_files(args.files): + explicit_start = not args.no_explicit_start + for filepath, was_changed, error in format_yaml_files(args.files, explicit_start=explicit_start): if error: errors.append(f'{filepath}: {error}') elif was_changed: diff --git a/polymath_code_standard/yaml_format.py b/polymath_code_standard/yaml_format.py index eea8564..0eec21a 100644 --- a/polymath_code_standard/yaml_format.py +++ b/polymath_code_standard/yaml_format.py @@ -43,11 +43,12 @@ def _make_yaml() -> YAML: return yaml -def _yamlfix_config() -> YamlfixConfig: +def _yamlfix_config(explicit_start: bool = True) -> YamlfixConfig: config = YamlfixConfig() config.sequence_style = YamlNodeStyle.KEEP_STYLE config.line_length = 256 config.whitelines = 1 + config.explicit_start = explicit_start return config @@ -314,7 +315,7 @@ def _strip_trailing_whitespace(text: str) -> str: return '\n'.join(line.rstrip() for line in text.split('\n')) -def format_yaml(source: str) -> str: +def format_yaml(source: str, explicit_start: bool = True) -> str: """Format YAML source, preserving matrix-structured flow sequences. Raises ValueError for irregular multi-line flow sequences. @@ -322,13 +323,13 @@ def format_yaml(source: str) -> str: if not source: return source matrices = detect_matrices(source) - fixed = fix_code(source, _yamlfix_config()) + fixed = fix_code(source, _yamlfix_config(explicit_start=explicit_start)) if matrices: fixed = apply_matrices(fixed, matrices) return _strip_trailing_whitespace(fixed) -def format_yaml_files(files: list[str]) -> list[tuple[str, bool, str]]: +def format_yaml_files(files: list[str], explicit_start: bool = True) -> list[tuple[str, bool, str]]: """Format YAML files in place. Returns [(filepath, was_changed, error_message)] for each file. @@ -339,7 +340,7 @@ def format_yaml_files(files: list[str]) -> list[tuple[str, bool, str]]: path = Path(filepath) try: original = path.read_text(encoding='utf-8') - formatted = format_yaml(original) + formatted = format_yaml(original, explicit_start=explicit_start) except ValueError as exc: results.append((filepath, False, str(exc))) continue