Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion polymath_code_standard/checkers/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions polymath_code_standard/yaml_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -314,21 +315,21 @@ 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.
"""
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.
Expand All @@ -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
Expand Down