Moving from aspy.yaml to ruamel.yaml in order to preserve comments when using autoupdate - fixes #210

This commit is contained in:
Cimon Lucas (LCM) 2016-09-28 10:51:32 +02:00
parent bbf1f62ed6
commit ebaf7da989
6 changed files with 35 additions and 19 deletions

View file

@ -9,9 +9,9 @@ import sys
import jsonschema
import jsonschema.exceptions
import pkg_resources
import yaml
from pre_commit.jsonschema_extensions import apply_defaults
from pre_commit.yaml import yaml_load
def is_regex_valid(regex):
@ -36,7 +36,7 @@ def get_validator(
the object read from the file. The function should either raise
exception_type on failure.
"""
def validate(filename, load_strategy=yaml.load):
def validate(filename, load_strategy=yaml_load):
if not os.path.exists(filename):
raise exception_type('File {} does not exist'.format(filename))

View file

@ -4,9 +4,6 @@ from __future__ import unicode_literals
import logging
import sys
from aspy.yaml import ordered_dump
from aspy.yaml import ordered_load
import pre_commit.constants as C
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
from pre_commit.clientlib.validate_config import is_local_hooks
@ -18,6 +15,8 @@ from pre_commit.repository import Repository
from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from pre_commit.yaml import yaml_dump
from pre_commit.yaml import yaml_load
logger = logging.getLogger('pre_commit')
@ -79,7 +78,7 @@ def autoupdate(runner):
input_configs = load_config(
runner.config_file_path,
load_strategy=ordered_load,
load_strategy=yaml_load,
)
for repo_config in input_configs:
@ -111,7 +110,7 @@ def autoupdate(runner):
if changed:
with open(runner.config_file_path, 'w') as config_file:
config_file.write(
ordered_dump(
yaml_dump(
remove_defaults(output_configs, CONFIG_JSON_SCHEMA),
**C.YAML_DUMP_KWARGS
)

11
pre_commit/yaml.py Executable file
View file

@ -0,0 +1,11 @@
from ruamel import yaml
def yaml_dump(obj, **kwargs):
"Ensure order & comments preservation"
return yaml.dump(obj, Dumper=yaml.RoundTripDumper, **kwargs)
def yaml_load(content):
"Ensure order & comments preservation"
return yaml.load(content, Loader=yaml.RoundTripLoader)