mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Replace aspy.yaml with sort_keys=False
This commit is contained in:
parent
f0ee93c5a7
commit
a64fa6d478
8 changed files with 36 additions and 26 deletions
|
|
@ -9,13 +9,13 @@ from typing import Optional
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
|
|
||||||
import cfgv
|
import cfgv
|
||||||
from aspy.yaml import ordered_load
|
|
||||||
from identify.identify import ALL_TAGS
|
from identify.identify import ALL_TAGS
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit.error_handler import FatalError
|
from pre_commit.error_handler import FatalError
|
||||||
from pre_commit.languages.all import all_languages
|
from pre_commit.languages.all import all_languages
|
||||||
from pre_commit.util import parse_version
|
from pre_commit.util import parse_version
|
||||||
|
from pre_commit.util import yaml_load
|
||||||
|
|
||||||
logger = logging.getLogger('pre_commit')
|
logger = logging.getLogger('pre_commit')
|
||||||
|
|
||||||
|
|
@ -84,7 +84,7 @@ class InvalidManifestError(FatalError):
|
||||||
load_manifest = functools.partial(
|
load_manifest = functools.partial(
|
||||||
cfgv.load_from_filename,
|
cfgv.load_from_filename,
|
||||||
schema=MANIFEST_SCHEMA,
|
schema=MANIFEST_SCHEMA,
|
||||||
load_strategy=ordered_load,
|
load_strategy=yaml_load,
|
||||||
exc_tp=InvalidManifestError,
|
exc_tp=InvalidManifestError,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -288,7 +288,7 @@ class InvalidConfigError(FatalError):
|
||||||
|
|
||||||
|
|
||||||
def ordered_load_normalize_legacy_config(contents: str) -> Dict[str, Any]:
|
def ordered_load_normalize_legacy_config(contents: str) -> Dict[str, Any]:
|
||||||
data = ordered_load(contents)
|
data = yaml_load(contents)
|
||||||
if isinstance(data, list):
|
if isinstance(data, list):
|
||||||
# TODO: Once happy, issue a deprecation warning and instructions
|
# TODO: Once happy, issue a deprecation warning and instructions
|
||||||
return {'repos': data}
|
return {'repos': data}
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,6 @@ from typing import Optional
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
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 import git
|
from pre_commit import git
|
||||||
from pre_commit import output
|
from pre_commit import output
|
||||||
|
|
@ -25,6 +22,8 @@ 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 cmd_output_b
|
from pre_commit.util import cmd_output_b
|
||||||
from pre_commit.util import tmpdir
|
from pre_commit.util import tmpdir
|
||||||
|
from pre_commit.util import yaml_dump
|
||||||
|
from pre_commit.util import yaml_load
|
||||||
|
|
||||||
|
|
||||||
class RevInfo(NamedTuple):
|
class RevInfo(NamedTuple):
|
||||||
|
|
@ -105,7 +104,7 @@ def _original_lines(
|
||||||
raise AssertionError('could not find rev lines')
|
raise AssertionError('could not find rev lines')
|
||||||
else:
|
else:
|
||||||
with open(path, 'w') as f:
|
with open(path, 'w') as f:
|
||||||
f.write(ordered_dump(ordered_load(original), **C.YAML_DUMP_KWARGS))
|
f.write(yaml_dump(yaml_load(original)))
|
||||||
return _original_lines(path, rev_infos, retry=True)
|
return _original_lines(path, rev_infos, retry=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -117,7 +116,7 @@ def _write_new_config(path: str, rev_infos: List[Optional[RevInfo]]) -> None:
|
||||||
continue
|
continue
|
||||||
match = REV_LINE_RE.match(lines[idx])
|
match = REV_LINE_RE.match(lines[idx])
|
||||||
assert match is not None
|
assert match is not None
|
||||||
new_rev_s = ordered_dump({'rev': rev_info.rev}, **C.YAML_DUMP_KWARGS)
|
new_rev_s = yaml_dump({'rev': rev_info.rev})
|
||||||
new_rev = new_rev_s.split(':', 1)[1].strip()
|
new_rev = new_rev_s.split(':', 1)[1].strip()
|
||||||
if rev_info.frozen is not None:
|
if rev_info.frozen is not None:
|
||||||
comment = f' # frozen: {rev_info.frozen}'
|
comment = f' # frozen: {rev_info.frozen}'
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
from aspy.yaml import ordered_load
|
|
||||||
|
from pre_commit.util import yaml_load
|
||||||
|
|
||||||
|
|
||||||
def _indent(s: str) -> str:
|
def _indent(s: str) -> str:
|
||||||
|
|
@ -24,12 +25,12 @@ def _migrate_map(contents: str) -> str:
|
||||||
header = ''.join(lines[:i])
|
header = ''.join(lines[:i])
|
||||||
rest = ''.join(lines[i:])
|
rest = ''.join(lines[i:])
|
||||||
|
|
||||||
if isinstance(ordered_load(contents), list):
|
if isinstance(yaml_load(contents), list):
|
||||||
# If they are using the "default" flow style of yaml, this operation
|
# If they are using the "default" flow style of yaml, this operation
|
||||||
# will yield a valid configuration
|
# will yield a valid configuration
|
||||||
try:
|
try:
|
||||||
trial_contents = f'{header}repos:\n{rest}'
|
trial_contents = f'{header}repos:\n{rest}'
|
||||||
ordered_load(trial_contents)
|
yaml_load(trial_contents)
|
||||||
contents = trial_contents
|
contents = trial_contents
|
||||||
except yaml.YAMLError:
|
except yaml.YAMLError:
|
||||||
contents = f'{header}repos:\n{_indent(rest)}'
|
contents = f'{header}repos:\n{_indent(rest)}'
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@ import os.path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
from aspy.yaml import ordered_dump
|
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
from pre_commit import output
|
from pre_commit import output
|
||||||
|
|
@ -14,6 +12,7 @@ from pre_commit.commands.run import run
|
||||||
from pre_commit.store import Store
|
from pre_commit.store import Store
|
||||||
from pre_commit.util import cmd_output_b
|
from pre_commit.util import cmd_output_b
|
||||||
from pre_commit.util import tmpdir
|
from pre_commit.util import tmpdir
|
||||||
|
from pre_commit.util import yaml_dump
|
||||||
from pre_commit.xargs import xargs
|
from pre_commit.xargs import xargs
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
@ -63,7 +62,7 @@ def try_repo(args: argparse.Namespace) -> int:
|
||||||
hooks = [{'id': hook['id']} for hook in manifest]
|
hooks = [{'id': hook['id']} for hook in manifest]
|
||||||
|
|
||||||
config = {'repos': [{'repo': repo, 'rev': ref, 'hooks': hooks}]}
|
config = {'repos': [{'repo': repo, 'rev': ref, 'hooks': hooks}]}
|
||||||
config_s = ordered_dump(config, **C.YAML_DUMP_KWARGS)
|
config_s = yaml_dump(config)
|
||||||
|
|
||||||
config_filename = os.path.join(tempdir, C.CONFIG_FILE)
|
config_filename = os.path.join(tempdir, C.CONFIG_FILE)
|
||||||
with open(config_filename, 'w') as cfg:
|
with open(config_filename, 'w') as cfg:
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,6 @@ else: # pragma: no cover (PY38+)
|
||||||
CONFIG_FILE = '.pre-commit-config.yaml'
|
CONFIG_FILE = '.pre-commit-config.yaml'
|
||||||
MANIFEST_FILE = '.pre-commit-hooks.yaml'
|
MANIFEST_FILE = '.pre-commit-hooks.yaml'
|
||||||
|
|
||||||
YAML_DUMP_KWARGS = {'default_flow_style': False, 'indent': 4}
|
|
||||||
|
|
||||||
# Bump when installation changes in a backwards / forwards incompatible way
|
# Bump when installation changes in a backwards / forwards incompatible way
|
||||||
INSTALLED_STATE_VERSION = '1'
|
INSTALLED_STATE_VERSION = '1'
|
||||||
# Bump when modifying `empty_template`
|
# Bump when modifying `empty_template`
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import contextlib
|
import contextlib
|
||||||
import errno
|
import errno
|
||||||
|
import functools
|
||||||
import os.path
|
import os.path
|
||||||
import shutil
|
import shutil
|
||||||
import stat
|
import stat
|
||||||
|
|
@ -17,6 +18,8 @@ from typing import Tuple
|
||||||
from typing import Type
|
from typing import Type
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
from pre_commit import parse_shebang
|
from pre_commit import parse_shebang
|
||||||
|
|
||||||
if sys.version_info >= (3, 7): # pragma: no cover (PY37+)
|
if sys.version_info >= (3, 7): # pragma: no cover (PY37+)
|
||||||
|
|
@ -28,6 +31,17 @@ else: # pragma: no cover (<PY37)
|
||||||
|
|
||||||
EnvironT = Union[Dict[str, str], 'os._Environ']
|
EnvironT = Union[Dict[str, str], 'os._Environ']
|
||||||
|
|
||||||
|
Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
|
||||||
|
yaml_load = functools.partial(yaml.load, Loader=Loader)
|
||||||
|
Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
|
||||||
|
|
||||||
|
|
||||||
|
def yaml_dump(o: Any) -> str:
|
||||||
|
# when python/mypy#1484 is solved, this can be `functools.partial`
|
||||||
|
return yaml.dump(
|
||||||
|
o, Dumper=Dumper, default_flow_style=False, indent=4, sort_keys=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def clean_path_on_failure(path: str) -> Generator[None, None, None]:
|
def clean_path_on_failure(path: str) -> Generator[None, None, None]:
|
||||||
|
|
|
||||||
|
|
@ -22,11 +22,10 @@ classifiers =
|
||||||
[options]
|
[options]
|
||||||
packages = find:
|
packages = find:
|
||||||
install_requires =
|
install_requires =
|
||||||
aspy.yaml
|
|
||||||
cfgv>=2.0.0
|
cfgv>=2.0.0
|
||||||
identify>=1.0.0
|
identify>=1.0.0
|
||||||
nodeenv>=0.11.1
|
nodeenv>=0.11.1
|
||||||
pyyaml
|
pyyaml>=5.1
|
||||||
toml
|
toml
|
||||||
virtualenv>=15.2
|
virtualenv>=15.2
|
||||||
importlib-metadata;python_version<"3.8"
|
importlib-metadata;python_version<"3.8"
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ import contextlib
|
||||||
import os.path
|
import os.path
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from aspy.yaml import ordered_dump
|
|
||||||
from aspy.yaml import ordered_load
|
|
||||||
from cfgv import apply_defaults
|
from cfgv import apply_defaults
|
||||||
from cfgv import validate
|
from cfgv import validate
|
||||||
|
|
||||||
|
|
@ -12,6 +10,8 @@ from pre_commit import git
|
||||||
from pre_commit.clientlib import CONFIG_SCHEMA
|
from pre_commit.clientlib import CONFIG_SCHEMA
|
||||||
from pre_commit.clientlib import load_manifest
|
from pre_commit.clientlib import load_manifest
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
|
from pre_commit.util import yaml_dump
|
||||||
|
from pre_commit.util import yaml_load
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
from testing.util import git_commit
|
from testing.util import git_commit
|
||||||
|
|
||||||
|
|
@ -55,10 +55,10 @@ def modify_manifest(path, commit=True):
|
||||||
"""
|
"""
|
||||||
manifest_path = os.path.join(path, C.MANIFEST_FILE)
|
manifest_path = os.path.join(path, C.MANIFEST_FILE)
|
||||||
with open(manifest_path) as f:
|
with open(manifest_path) as f:
|
||||||
manifest = ordered_load(f.read())
|
manifest = yaml_load(f.read())
|
||||||
yield manifest
|
yield manifest
|
||||||
with open(manifest_path, 'w') as manifest_file:
|
with open(manifest_path, 'w') as manifest_file:
|
||||||
manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS))
|
manifest_file.write(yaml_dump(manifest))
|
||||||
if commit:
|
if commit:
|
||||||
git_commit(msg=modify_manifest.__name__, cwd=path)
|
git_commit(msg=modify_manifest.__name__, cwd=path)
|
||||||
|
|
||||||
|
|
@ -70,10 +70,10 @@ def modify_config(path='.', commit=True):
|
||||||
"""
|
"""
|
||||||
config_path = os.path.join(path, C.CONFIG_FILE)
|
config_path = os.path.join(path, C.CONFIG_FILE)
|
||||||
with open(config_path) as f:
|
with open(config_path) as f:
|
||||||
config = ordered_load(f.read())
|
config = yaml_load(f.read())
|
||||||
yield config
|
yield config
|
||||||
with open(config_path, 'w', encoding='UTF-8') as config_file:
|
with 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))
|
||||||
if commit:
|
if commit:
|
||||||
git_commit(msg=modify_config.__name__, cwd=path)
|
git_commit(msg=modify_config.__name__, cwd=path)
|
||||||
|
|
||||||
|
|
@ -114,7 +114,7 @@ def make_config_from_repo(repo_path, rev=None, hooks=None, check=True):
|
||||||
def read_config(directory, config_file=C.CONFIG_FILE):
|
def read_config(directory, config_file=C.CONFIG_FILE):
|
||||||
config_path = os.path.join(directory, config_file)
|
config_path = os.path.join(directory, config_file)
|
||||||
with open(config_path) as f:
|
with open(config_path) as f:
|
||||||
config = ordered_load(f.read())
|
config = yaml_load(f.read())
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -123,7 +123,7 @@ def write_config(directory, config, config_file=C.CONFIG_FILE):
|
||||||
assert isinstance(config, dict), config
|
assert isinstance(config, dict), config
|
||||||
config = {'repos': [config]}
|
config = {'repos': [config]}
|
||||||
with open(os.path.join(directory, config_file), 'w') as outfile:
|
with open(os.path.join(directory, config_file), 'w') as outfile:
|
||||||
outfile.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
|
outfile.write(yaml_dump(config))
|
||||||
|
|
||||||
|
|
||||||
def add_config_to_repo(git_path, config, config_file=C.CONFIG_FILE):
|
def add_config_to_repo(git_path, config, config_file=C.CONFIG_FILE):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue