mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #2673 from pre-commit/split-util
add pre_commit.yaml module
This commit is contained in:
commit
83e64e2071
9 changed files with 28 additions and 26 deletions
|
|
@ -14,7 +14,7 @@ from identify.identify import ALL_TAGS
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit.errors import FatalError
|
from pre_commit.errors import FatalError
|
||||||
from pre_commit.languages.all import all_languages
|
from pre_commit.languages.all import all_languages
|
||||||
from pre_commit.util import yaml_load
|
from pre_commit.yaml import yaml_load
|
||||||
|
|
||||||
logger = logging.getLogger('pre_commit')
|
logger = logging.getLogger('pre_commit')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,8 @@ from pre_commit.store import Store
|
||||||
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 cmd_output_b
|
from pre_commit.util import cmd_output_b
|
||||||
from pre_commit.util import yaml_dump
|
from pre_commit.yaml import yaml_dump
|
||||||
from pre_commit.util import yaml_load
|
from pre_commit.yaml import yaml_load
|
||||||
|
|
||||||
|
|
||||||
class RevInfo(NamedTuple):
|
class RevInfo(NamedTuple):
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import textwrap
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from pre_commit.util import yaml_load
|
from pre_commit.yaml import yaml_load
|
||||||
|
|
||||||
|
|
||||||
def _is_header_line(line: str) -> bool:
|
def _is_header_line(line: str) -> bool:
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,8 @@ from pre_commit.clientlib import load_manifest
|
||||||
from pre_commit.commands.run import run
|
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 yaml_dump
|
|
||||||
from pre_commit.xargs import xargs
|
from pre_commit.xargs import xargs
|
||||||
|
from pre_commit.yaml import yaml_dump
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ from pre_commit.hook import Hook
|
||||||
from pre_commit.languages import helpers
|
from pre_commit.languages import helpers
|
||||||
from pre_commit.prefix import Prefix
|
from pre_commit.prefix import Prefix
|
||||||
from pre_commit.util import win_exe
|
from pre_commit.util import win_exe
|
||||||
from pre_commit.util import yaml_load
|
from pre_commit.yaml import yaml_load
|
||||||
|
|
||||||
ENVIRONMENT_DIR = 'dartenv'
|
ENVIRONMENT_DIR = 'dartenv'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ from __future__ import annotations
|
||||||
|
|
||||||
import contextlib
|
import contextlib
|
||||||
import errno
|
import errno
|
||||||
import functools
|
|
||||||
import importlib.resources
|
import importlib.resources
|
||||||
import os.path
|
import os.path
|
||||||
import shutil
|
import shutil
|
||||||
|
|
@ -15,22 +14,8 @@ from typing import Callable
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
from typing import IO
|
from typing import IO
|
||||||
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
from pre_commit import parse_shebang
|
from pre_commit import parse_shebang
|
||||||
|
|
||||||
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, **kwargs: 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,
|
|
||||||
**kwargs,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def force_bytes(exc: Any) -> bytes:
|
def force_bytes(exc: Any) -> bytes:
|
||||||
with contextlib.suppress(TypeError):
|
with contextlib.suppress(TypeError):
|
||||||
|
|
|
||||||
18
pre_commit/yaml.py
Normal file
18
pre_commit/yaml.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import functools
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
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, **kwargs: 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,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
@ -12,8 +12,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.yaml import yaml_dump
|
||||||
from pre_commit.util import yaml_load
|
from pre_commit.yaml 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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,11 @@ import shlex
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import yaml
|
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit import envcontext
|
from pre_commit import envcontext
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
from pre_commit import util
|
from pre_commit import yaml
|
||||||
from pre_commit.commands.autoupdate import _check_hooks_still_exist_at_rev
|
from pre_commit.commands.autoupdate import _check_hooks_still_exist_at_rev
|
||||||
from pre_commit.commands.autoupdate import autoupdate
|
from pre_commit.commands.autoupdate import autoupdate
|
||||||
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
|
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
|
||||||
|
|
@ -206,7 +205,7 @@ def test_autoupdate_with_core_useBuiltinFSMonitor(out_of_date, tmpdir, store):
|
||||||
|
|
||||||
|
|
||||||
def test_autoupdate_pure_yaml(out_of_date, tmpdir, store):
|
def test_autoupdate_pure_yaml(out_of_date, tmpdir, store):
|
||||||
with mock.patch.object(util, 'Dumper', yaml.SafeDumper):
|
with mock.patch.object(yaml, 'Dumper', yaml.yaml.SafeDumper):
|
||||||
test_autoupdate_out_of_date_repo(out_of_date, tmpdir, store)
|
test_autoupdate_out_of_date_repo(out_of_date, tmpdir, store)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue