mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 18:11:48 +04:00
Moving from aspy.yaml to ruamel.yaml in order to preserve comments when using autoupdate - fixes #210
This commit is contained in:
parent
bbf1f62ed6
commit
ebaf7da989
6 changed files with 35 additions and 19 deletions
|
|
@ -9,9 +9,9 @@ import sys
|
||||||
import jsonschema
|
import jsonschema
|
||||||
import jsonschema.exceptions
|
import jsonschema.exceptions
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
import yaml
|
|
||||||
|
|
||||||
from pre_commit.jsonschema_extensions import apply_defaults
|
from pre_commit.jsonschema_extensions import apply_defaults
|
||||||
|
from pre_commit.yaml import yaml_load
|
||||||
|
|
||||||
|
|
||||||
def is_regex_valid(regex):
|
def is_regex_valid(regex):
|
||||||
|
|
@ -36,7 +36,7 @@ def get_validator(
|
||||||
the object read from the file. The function should either raise
|
the object read from the file. The function should either raise
|
||||||
exception_type on failure.
|
exception_type on failure.
|
||||||
"""
|
"""
|
||||||
def validate(filename, load_strategy=yaml.load):
|
def validate(filename, load_strategy=yaml_load):
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
raise exception_type('File {} does not exist'.format(filename))
|
raise exception_type('File {} does not exist'.format(filename))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,6 @@ from __future__ import unicode_literals
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from aspy.yaml import ordered_dump
|
|
||||||
from aspy.yaml import ordered_load
|
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
||||||
from pre_commit.clientlib.validate_config import is_local_hooks
|
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 CalledProcessError
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.util import cwd
|
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')
|
logger = logging.getLogger('pre_commit')
|
||||||
|
|
@ -79,7 +78,7 @@ def autoupdate(runner):
|
||||||
|
|
||||||
input_configs = load_config(
|
input_configs = load_config(
|
||||||
runner.config_file_path,
|
runner.config_file_path,
|
||||||
load_strategy=ordered_load,
|
load_strategy=yaml_load,
|
||||||
)
|
)
|
||||||
|
|
||||||
for repo_config in input_configs:
|
for repo_config in input_configs:
|
||||||
|
|
@ -111,7 +110,7 @@ def autoupdate(runner):
|
||||||
if changed:
|
if changed:
|
||||||
with open(runner.config_file_path, 'w') as config_file:
|
with open(runner.config_file_path, 'w') as config_file:
|
||||||
config_file.write(
|
config_file.write(
|
||||||
ordered_dump(
|
yaml_dump(
|
||||||
remove_defaults(output_configs, CONFIG_JSON_SCHEMA),
|
remove_defaults(output_configs, CONFIG_JSON_SCHEMA),
|
||||||
**C.YAML_DUMP_KWARGS
|
**C.YAML_DUMP_KWARGS
|
||||||
)
|
)
|
||||||
|
|
|
||||||
11
pre_commit/yaml.py
Executable file
11
pre_commit/yaml.py
Executable 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)
|
||||||
2
setup.py
2
setup.py
|
|
@ -37,12 +37,12 @@ setup(
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'aspy.yaml',
|
|
||||||
'cached-property',
|
'cached-property',
|
||||||
'jsonschema',
|
'jsonschema',
|
||||||
'nodeenv>=0.11.1',
|
'nodeenv>=0.11.1',
|
||||||
'pyterminalsize',
|
'pyterminalsize',
|
||||||
'pyyaml',
|
'pyyaml',
|
||||||
|
'ruamel.yaml',
|
||||||
'virtualenv',
|
'virtualenv',
|
||||||
],
|
],
|
||||||
entry_points={
|
entry_points={
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,6 @@ import contextlib
|
||||||
import io
|
import io
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
from aspy.yaml import ordered_dump
|
|
||||||
from aspy.yaml import ordered_load
|
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
||||||
from pre_commit.clientlib.validate_config import validate_config_extra
|
from pre_commit.clientlib.validate_config import validate_config_extra
|
||||||
|
|
@ -16,6 +13,8 @@ from pre_commit.jsonschema_extensions import apply_defaults
|
||||||
from pre_commit.ordereddict import OrderedDict
|
from pre_commit.ordereddict import OrderedDict
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.util import cwd
|
from pre_commit.util import cwd
|
||||||
|
from pre_commit.yaml import yaml_dump
|
||||||
|
from pre_commit.yaml import yaml_load
|
||||||
from testing.util import copy_tree_to_path
|
from testing.util import copy_tree_to_path
|
||||||
from testing.util import get_head_sha
|
from testing.util import get_head_sha
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
|
|
@ -41,10 +40,10 @@ def make_repo(tempdir_factory, repo_source):
|
||||||
def modify_manifest(path):
|
def modify_manifest(path):
|
||||||
"""Modify the manifest yielded by this context to write to hooks.yaml."""
|
"""Modify the manifest yielded by this context to write to hooks.yaml."""
|
||||||
manifest_path = os.path.join(path, C.MANIFEST_FILE)
|
manifest_path = os.path.join(path, C.MANIFEST_FILE)
|
||||||
manifest = ordered_load(io.open(manifest_path).read())
|
manifest = yaml_load(io.open(manifest_path).read())
|
||||||
yield manifest
|
yield manifest
|
||||||
with io.open(manifest_path, 'w') as manifest_file:
|
with io.open(manifest_path, 'w') as manifest_file:
|
||||||
manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS))
|
manifest_file.write(yaml_dump(manifest, **C.YAML_DUMP_KWARGS))
|
||||||
cmd_output('git', 'commit', '-am', 'update hooks.yaml', cwd=path)
|
cmd_output('git', 'commit', '-am', 'update hooks.yaml', cwd=path)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -54,10 +53,10 @@ def modify_config(path='.', commit=True):
|
||||||
.pre-commit-config.yaml
|
.pre-commit-config.yaml
|
||||||
"""
|
"""
|
||||||
config_path = os.path.join(path, C.CONFIG_FILE)
|
config_path = os.path.join(path, C.CONFIG_FILE)
|
||||||
config = ordered_load(io.open(config_path).read())
|
config = yaml_load(io.open(config_path).read())
|
||||||
yield config
|
yield config
|
||||||
with io.open(config_path, 'w', encoding='UTF-8') as config_file:
|
with io.open(config_path, 'w', encoding='UTF-8') as config_file:
|
||||||
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
|
config_file.write(yaml_dump(config, **C.YAML_DUMP_KWARGS))
|
||||||
if commit:
|
if commit:
|
||||||
cmd_output('git', 'commit', '-am', 'update config', cwd=path)
|
cmd_output('git', 'commit', '-am', 'update config', cwd=path)
|
||||||
|
|
||||||
|
|
@ -99,7 +98,7 @@ def write_config(directory, config):
|
||||||
assert type(config) is OrderedDict
|
assert type(config) is OrderedDict
|
||||||
config = [config]
|
config = [config]
|
||||||
with io.open(os.path.join(directory, C.CONFIG_FILE), 'w') as config_file:
|
with io.open(os.path.join(directory, C.CONFIG_FILE), 'w') as config_file:
|
||||||
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
|
config_file.write(yaml_dump(config, **C.YAML_DUMP_KWARGS))
|
||||||
|
|
||||||
|
|
||||||
def add_config_to_repo(git_path, config):
|
def add_config_to_repo(git_path, config):
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from aspy.yaml import ordered_load
|
|
||||||
|
|
||||||
from pre_commit.clientlib.validate_base import get_validator
|
from pre_commit.clientlib.validate_base import get_validator
|
||||||
from pre_commit.ordereddict import OrderedDict
|
from pre_commit.ordereddict import OrderedDict
|
||||||
|
from pre_commit.yaml import yaml_load
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -68,6 +70,11 @@ def test_returns_object_after_validating(noop_validator):
|
||||||
def test_load_strategy(noop_validator):
|
def test_load_strategy(noop_validator):
|
||||||
ret = noop_validator(
|
ret = noop_validator(
|
||||||
get_resource_path('ordering_data_test.yaml'),
|
get_resource_path('ordering_data_test.yaml'),
|
||||||
load_strategy=ordered_load,
|
load_strategy=yaml_load,
|
||||||
)
|
)
|
||||||
assert type(ret) is OrderedDict
|
if sys.version_info[0] < 3:
|
||||||
|
# ruamel.yaml uses a different implementation for Python < 3
|
||||||
|
from ruamel.yaml.compat import ordereddict
|
||||||
|
assert isinstance(ret, ordereddict)
|
||||||
|
else:
|
||||||
|
assert isinstance(ret, OrderedDict)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue