dycw-pre-commit-hooks 0.14.39__py3-none-any.whl → 0.14.53__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,179 @@
1
+ from __future__ import annotations
2
+
3
+ from functools import partial
4
+ from pathlib import Path
5
+ from typing import TYPE_CHECKING
6
+
7
+ from click import command
8
+ from tomlkit import table
9
+ from utilities.click import CONTEXT_SETTINGS
10
+ from utilities.iterables import always_iterable
11
+ from utilities.os import is_pytest
12
+ from utilities.text import kebab_case, snake_case
13
+
14
+ from pre_commit_hooks.constants import (
15
+ DEFAULT_PYTHON_VERSION,
16
+ PYPROJECT_TOML,
17
+ README_MD,
18
+ description_option,
19
+ paths_argument,
20
+ python_package_name_external_option,
21
+ python_package_name_internal_option,
22
+ python_uv_index_option,
23
+ python_version_option,
24
+ )
25
+ from pre_commit_hooks.utilities import (
26
+ ensure_contains,
27
+ ensure_contains_partial_str,
28
+ get_set_aot,
29
+ get_set_array,
30
+ get_set_table,
31
+ get_table,
32
+ run_all_maybe_raise,
33
+ yield_toml_doc,
34
+ )
35
+
36
+ if TYPE_CHECKING:
37
+ from collections.abc import Callable, MutableSet
38
+
39
+ from tomlkit import TOMLDocument
40
+ from tomlkit.items import Table
41
+ from utilities.types import MaybeSequenceStr, PathLike
42
+
43
+
44
+ @command(**CONTEXT_SETTINGS)
45
+ @paths_argument
46
+ @python_version_option
47
+ @description_option
48
+ @python_package_name_external_option
49
+ @python_package_name_internal_option
50
+ @python_uv_index_option
51
+ def _main(
52
+ *,
53
+ paths: tuple[Path, ...],
54
+ python_version: str = DEFAULT_PYTHON_VERSION,
55
+ description: str | None = None,
56
+ python_package_name_external: str | None = None,
57
+ python_package_name_internal: str | None = None,
58
+ python_uv_index: MaybeSequenceStr | None = None,
59
+ ) -> None:
60
+ if is_pytest():
61
+ return
62
+ funcs: list[Callable[[], bool]] = [
63
+ partial(
64
+ _run,
65
+ path=p.parent / PYPROJECT_TOML,
66
+ python_version=python_version,
67
+ description=description,
68
+ python_package_name_external=python_package_name_external,
69
+ python_package_name_internal=python_package_name_internal,
70
+ python_uv_index=python_uv_index,
71
+ )
72
+ for p in paths
73
+ ]
74
+ run_all_maybe_raise(*funcs)
75
+
76
+
77
+ def _run(
78
+ *,
79
+ path: PathLike = PYPROJECT_TOML,
80
+ python_version: str = DEFAULT_PYTHON_VERSION,
81
+ description: str | None = None,
82
+ python_package_name_external: str | None = None,
83
+ python_package_name_internal: str | None = None,
84
+ python_uv_index: MaybeSequenceStr | None = None,
85
+ ) -> bool:
86
+ path = Path(path)
87
+ modifications: set[Path] = set()
88
+ with yield_toml_doc(path, modifications=modifications) as doc:
89
+ build_system = get_set_table(doc, "build-system")
90
+ build_system["build-backend"] = "uv_build"
91
+ build_system["requires"] = ["uv_build"]
92
+ project = get_set_table(doc, "project")
93
+ project["readme"] = str(path.parent / README_MD)
94
+ project["requires-python"] = f">= {python_version}"
95
+ project.setdefault("version", "0.1.0")
96
+ dependency_groups = get_set_table(doc, "dependency-groups")
97
+ dev = get_set_array(dependency_groups, "dev")
98
+ _ = ensure_contains_partial_str(dev, "dycw-utilities[test]")
99
+ _ = ensure_contains_partial_str(dev, "pyright")
100
+ _ = ensure_contains_partial_str(dev, "rich")
101
+ if description is not None:
102
+ _add_description(description, path=path, modifications=modifications)
103
+ if python_package_name_external is not None:
104
+ _add_external_name(
105
+ python_package_name_external, path=path, modifications=modifications
106
+ )
107
+ if python_package_name_internal is not None:
108
+ _add_internal_name(
109
+ python_package_name_internal, path=path, modifications=modifications
110
+ )
111
+ if python_uv_index is not None:
112
+ for name_and_url in always_iterable(python_uv_index):
113
+ _add_index(name_and_url, path=path, modifications=modifications)
114
+ return len(modifications) == 0
115
+
116
+
117
+ def _add_description(
118
+ description: str,
119
+ /,
120
+ *,
121
+ path: PathLike = PYPROJECT_TOML,
122
+ modifications: MutableSet[Path] | None = None,
123
+ ) -> None:
124
+ with yield_toml_doc(path, modifications=modifications) as doc:
125
+ project = get_table(doc, "project")
126
+ project["description"] = description
127
+
128
+
129
+ def _add_external_name(
130
+ name: str,
131
+ /,
132
+ *,
133
+ path: PathLike = PYPROJECT_TOML,
134
+ modifications: MutableSet[Path] | None = None,
135
+ ) -> None:
136
+ with yield_toml_doc(path, modifications=modifications) as doc:
137
+ project = get_table(doc, "project")
138
+ project["name"] = kebab_case(name)
139
+
140
+
141
+ def _add_internal_name(
142
+ name: str,
143
+ /,
144
+ *,
145
+ path: PathLike = PYPROJECT_TOML,
146
+ modifications: MutableSet[Path] | None = None,
147
+ ) -> None:
148
+ with yield_toml_doc(path, modifications=modifications) as doc:
149
+ uv = _get_tool_uv(doc)
150
+ build_backend = get_set_table(uv, "build-backend")
151
+ build_backend["module-name"] = snake_case(name)
152
+ build_backend["module-root"] = "src"
153
+
154
+
155
+ def _add_index(
156
+ name_and_url: str,
157
+ /,
158
+ *,
159
+ path: PathLike = PYPROJECT_TOML,
160
+ modifications: MutableSet[Path] | None = None,
161
+ ) -> None:
162
+ with yield_toml_doc(path, modifications=modifications) as doc:
163
+ uv = _get_tool_uv(doc)
164
+ indexes = get_set_aot(uv, "index")
165
+ tab = table()
166
+ tab["explicit"] = True
167
+ name, url = name_and_url
168
+ tab["name"] = name
169
+ tab["url"] = url
170
+ ensure_contains(indexes, tab)
171
+
172
+
173
+ def _get_tool_uv(doc: TOMLDocument, /) -> Table:
174
+ tool = get_set_table(doc, "tool")
175
+ return get_set_table(tool, "uv")
176
+
177
+
178
+ if __name__ == "__main__":
179
+ _main()
@@ -21,6 +21,7 @@ from pre_commit_hooks.utilities import (
21
21
  )
22
22
 
23
23
  if TYPE_CHECKING:
24
+ from collections.abc import Callable
24
25
  from pathlib import Path
25
26
 
26
27
  from utilities.types import PathLike
@@ -34,14 +35,11 @@ def _main(
34
35
  ) -> None:
35
36
  if is_pytest():
36
37
  return
37
- run_all_maybe_raise(
38
- *(
39
- partial(
40
- _run, path=p.parent / PYRIGHTCONFIG_JSON, python_version=python_version
41
- )
42
- for p in paths
43
- )
44
- )
38
+ funcs: list[Callable[[], bool]] = [
39
+ partial(_run, path=p.parent / PYRIGHTCONFIG_JSON, python_version=python_version)
40
+ for p in paths
41
+ ]
42
+ run_all_maybe_raise(*funcs)
45
43
 
46
44
 
47
45
  def _run(
@@ -0,0 +1,76 @@
1
+ from __future__ import annotations
2
+
3
+ from functools import partial
4
+ from re import MULTILINE, escape, search
5
+ from typing import TYPE_CHECKING
6
+
7
+ from click import command
8
+ from utilities.click import CONTEXT_SETTINGS
9
+ from utilities.os import is_pytest
10
+
11
+ from pre_commit_hooks.constants import (
12
+ README_MD,
13
+ description_option,
14
+ paths_argument,
15
+ repo_name_option,
16
+ )
17
+ from pre_commit_hooks.utilities import run_all_maybe_raise, yield_text_file
18
+
19
+ if TYPE_CHECKING:
20
+ from collections.abc import Callable, MutableSet
21
+ from pathlib import Path
22
+
23
+ from utilities.types import PathLike
24
+
25
+
26
+ @command(**CONTEXT_SETTINGS)
27
+ @paths_argument
28
+ @repo_name_option
29
+ @description_option
30
+ def _main(
31
+ *,
32
+ paths: tuple[Path, ...],
33
+ repo_name: str | None = None,
34
+ description: str | None = None,
35
+ ) -> None:
36
+ if is_pytest():
37
+ return
38
+ funcs: list[Callable[[], bool]] = [
39
+ partial(
40
+ _run,
41
+ path=p.parent / README_MD,
42
+ repo_name=repo_name,
43
+ description=description,
44
+ )
45
+ for p in paths
46
+ ]
47
+ run_all_maybe_raise(*funcs)
48
+
49
+
50
+ def _run(
51
+ *,
52
+ path: PathLike = README_MD,
53
+ repo_name: str | None = None,
54
+ description: str | None = None,
55
+ ) -> bool:
56
+ modifications: set[Path] = set()
57
+ if (repo_name is not None) and (description is not None):
58
+ _add_header(repo_name, description, path=path, modifications=modifications)
59
+ return len(modifications) == 0
60
+
61
+
62
+ def _add_header(
63
+ repo_name: str,
64
+ description: str,
65
+ path: PathLike = README_MD,
66
+ modifications: MutableSet[Path] | None = None,
67
+ ) -> None:
68
+ with yield_text_file(path, modifications=modifications) as context:
69
+ lines = [f"# `{repo_name}`", description]
70
+ text = "\n\n".join(lines)
71
+ if search(escape(text), context.output, flags=MULTILINE) is None:
72
+ context.output += f"\n\n{text}"
73
+
74
+
75
+ if __name__ == "__main__":
76
+ _main()
@@ -23,6 +23,7 @@ from pre_commit_hooks.utilities import (
23
23
  )
24
24
 
25
25
  if TYPE_CHECKING:
26
+ from collections.abc import Callable
26
27
  from pathlib import Path
27
28
 
28
29
  from utilities.types import PathLike
@@ -36,12 +37,11 @@ def _main(
36
37
  ) -> None:
37
38
  if is_pytest():
38
39
  return
39
- run_all_maybe_raise(
40
- *(
41
- partial(_run, path=p.parent / RUFF_TOML, python_version=python_version)
42
- for p in paths
43
- )
44
- )
40
+ funcs: list[Callable[[], bool]] = [
41
+ partial(_run, path=p.parent / RUFF_TOML, python_version=python_version)
42
+ for p in paths
43
+ ]
44
+ run_all_maybe_raise(*funcs)
45
45
 
46
46
 
47
47
  def _run(
@@ -19,6 +19,7 @@ from pre_commit_hooks.utilities import (
19
19
  )
20
20
 
21
21
  if TYPE_CHECKING:
22
+ from collections.abc import Callable
22
23
  from pathlib import Path
23
24
 
24
25
  from utilities.packaging import Requirement
@@ -39,12 +40,11 @@ def _main(
39
40
  if is_pytest():
40
41
  return
41
42
  versions = _get_versions(index=index, native_tls=native_tls)
42
- run_all_maybe_raise(
43
- *(
44
- partial(_run, path=p, versions=versions, index=index, native_tls=native_tls)
45
- for p in paths
46
- )
47
- )
43
+ funcs: list[Callable[[], bool]] = [
44
+ partial(_run, path=p, versions=versions, index=index, native_tls=native_tls)
45
+ for p in paths
46
+ ]
47
+ run_all_maybe_raise(*funcs)
48
48
 
49
49
 
50
50
  def _get_versions(
@@ -25,7 +25,7 @@ from utilities.types import PathLike, StrDict
25
25
  from utilities.typing import is_str_dict
26
26
  from utilities.version import Version3, Version3Error
27
27
 
28
- from pre_commit_hooks.constants import BUMPVERSION_TOML, PATH_CACHE, PYPROJECT_TOML
28
+ from pre_commit_hooks.constants import BUMPVERSION_TOML, PATH_CACHE
29
29
 
30
30
  if TYPE_CHECKING:
31
31
  from collections.abc import Callable, Iterable, Iterator, MutableSet
@@ -40,6 +40,15 @@ if TYPE_CHECKING:
40
40
  )
41
41
 
42
42
 
43
+ def add_update_certificates(steps: list[StrDict], /) -> None:
44
+ ensure_contains(
45
+ steps, {"name": "Update CA certificates", "run": "sudo update-ca-certificates"}
46
+ )
47
+
48
+
49
+ ##
50
+
51
+
43
52
  def apply[T](func: Callable[[], T], /) -> T:
44
53
  return func()
45
54
 
@@ -67,12 +76,11 @@ def ensure_contains(container: ArrayLike, /, *objs: Any) -> None:
67
76
 
68
77
 
69
78
  def ensure_contains_partial_dict(
70
- container: list[StrDict], partial: StrDict, /, *, extra: StrDict | None = None
79
+ container: list[StrDict], dict_: StrDict, /
71
80
  ) -> StrDict:
72
81
  try:
73
- return get_partial_dict(container, partial)
82
+ return get_partial_dict(container, dict_)
74
83
  except OneEmptyError:
75
- dict_ = partial | ({} if extra is None else extra)
76
84
  container.append(dict_)
77
85
  return dict_
78
86
 
@@ -355,12 +363,12 @@ def run_all_maybe_raise(*funcs: Callable[[], bool]) -> None:
355
363
 
356
364
 
357
365
  def run_prettier(path: PathLike, /) -> None:
358
- with suppress(CalledProcessError):
366
+ with suppress(CalledProcessError, FileNotFoundError):
359
367
  run("prettier", "-w", str(path))
360
368
 
361
369
 
362
370
  def run_taplo(path: PathLike, /) -> None:
363
- with suppress(CalledProcessError):
371
+ with suppress(CalledProcessError, FileNotFoundError):
364
372
  run(
365
373
  "taplo",
366
374
  "format",
@@ -460,17 +468,6 @@ def yield_json_dict(
460
468
  ##
461
469
 
462
470
 
463
- @contextmanager
464
- def yield_pyproject_toml(
465
- *, modifications: MutableSet[Path] | None = None
466
- ) -> Iterator[TOMLDocument]:
467
- with yield_toml_doc(PYPROJECT_TOML, modifications=modifications) as doc:
468
- yield doc
469
-
470
-
471
- ##
472
-
473
-
474
471
  @contextmanager
475
472
  def yield_mutable_write_context[T](
476
473
  path: PathLike,
@@ -558,6 +555,7 @@ def yield_yaml_dict(
558
555
 
559
556
  __all__ = [
560
557
  "PyProjectDependencies",
558
+ "add_update_certificates",
561
559
  "apply",
562
560
  "are_equal_modulo_new_line",
563
561
  "ensure_contains",
@@ -591,7 +589,6 @@ __all__ = [
591
589
  "yield_immutable_write_context",
592
590
  "yield_json_dict",
593
591
  "yield_mutable_write_context",
594
- "yield_pyproject_toml",
595
592
  "yield_python_file",
596
593
  "yield_text_file",
597
594
  "yield_toml_doc",
@@ -1,28 +0,0 @@
1
- pre_commit_hooks/__init__.py,sha256=GeXnDOMO6g8ILZoWHaOQVE6skn1Zh8GsZaikKe8DvSY,60
2
- pre_commit_hooks/configs/gitignore,sha256=pIcfamKg40Tg2aWiMyY4pxHkSGNITRFD-ByA2wWGHQI,5062
3
- pre_commit_hooks/constants.py,sha256=8IuUpCRWMdavxr-fSFKo9VBgHZ72MYbB4CHkwbLjhlQ,4236
4
- pre_commit_hooks/hooks/__init__.py,sha256=U4S_2y3zgLZVfMenHRaJFBW8yqh2mUBuI291LGQVOJ8,35
5
- pre_commit_hooks/hooks/add_future_import_annotations.py,sha256=vUVaQIMrfPssrP0keiau-o8Jel2rpaXLi6pyT_d-QsU,1701
6
- pre_commit_hooks/hooks/add_hooks.py,sha256=CfIRttw21z8aDlcLq5bwQUl8pz7R3-YsXC6-UMo07-o,21560
7
- pre_commit_hooks/hooks/check_version_bumped.py,sha256=EIFQcACjM2_D4O4Yu01p-SzicAah1xklb_4KeU93V68,818
8
- pre_commit_hooks/hooks/check_versions_consistent.py,sha256=grfvgVJKBKMxYk2K3Jbr7uM26cXmDRTjyXrTN_Wb2Bk,980
9
- pre_commit_hooks/hooks/format_pre_commit_config.py,sha256=Vml8Mq5gFunOigs6l3FkTTMBaImYxtKfIehdMuYMO2E,1984
10
- pre_commit_hooks/hooks/format_requirements.py,sha256=R_FnEUx7jU3W5rWshLBKO2bX-cReHteF4iqm_mCnyCk,1474
11
- pre_commit_hooks/hooks/replace_sequence_str.py,sha256=G7JQ4n-hSjW5asM4C77ps8JYv5eV31rvFmmcxQYty1o,1816
12
- pre_commit_hooks/hooks/run_prek_autoupdate.py,sha256=SItY4GPTde7HXOaNy3zXsfFQFFZsc4hZzGzf380F0aI,1556
13
- pre_commit_hooks/hooks/run_version_bump.py,sha256=8jIqEGD0UzRQ7LGgGmL05wB3p1NaWIrgOMXDe1DDmh4,1222
14
- pre_commit_hooks/hooks/setup_bump_my_version.py,sha256=m6xl_SyjfKqeKoWh5gKIVuu3xSPJT6BOraeURLyy5TU,2819
15
- pre_commit_hooks/hooks/setup_direnv.py,sha256=UYY0n3o9SBiNXf2RvrtZ8Rs7g4id4NL_D2D1_WYCZUg,4095
16
- pre_commit_hooks/hooks/setup_git.py,sha256=nB87iKOuCPpc52RvLMRKzP-HZO53NluaX12k1JgxjfE,1193
17
- pre_commit_hooks/hooks/setup_pyright.py,sha256=DW9QdVE18aMzue8gLXxM4pnCIIooWm4DN-HDwM3A4RM,2565
18
- pre_commit_hooks/hooks/setup_ruff.py,sha256=GZlEtnc4mj4hAIjHxHQRoi9LQYDYCS4zPcZL8nWFKfg,4651
19
- pre_commit_hooks/hooks/update_ci_action_versions.py,sha256=hOS0QB_Hfyq__cVY5cMqtOqB-SnA4t-PE34DZkTm6Qk,1274
20
- pre_commit_hooks/hooks/update_ci_extensions.py,sha256=RqANNdSmroum6I3mLa91qbNcFjUPv5sc17XTxdWD1Gg,793
21
- pre_commit_hooks/hooks/update_requirements.py,sha256=2AypcOpCnZFhsBFHsHHYbUMBi9P-qtMWrrXlKWcM6B8,5303
22
- pre_commit_hooks/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
- pre_commit_hooks/types.py,sha256=ObWi-OEEyZHlSYZA3iiJHGMDJ3jADntS_OpR0jXpkDk,515
24
- pre_commit_hooks/utilities.py,sha256=_YLr0PGHYxV1StZLNGEkLTHXio9AmrJngnsgmS-pgSQ,16010
25
- dycw_pre_commit_hooks-0.14.39.dist-info/WHEEL,sha256=XV0cjMrO7zXhVAIyyc8aFf1VjZ33Fen4IiJk5zFlC3g,80
26
- dycw_pre_commit_hooks-0.14.39.dist-info/entry_points.txt,sha256=05O8H0ZWdhlU83tCa-58TmL_Jercprz8Ajg4hnBANtk,1200
27
- dycw_pre_commit_hooks-0.14.39.dist-info/METADATA,sha256=QghlkN_CZFMRhNjX3dV9xzwzFuyzbQNn1p8MDk4r5-4,1501
28
- dycw_pre_commit_hooks-0.14.39.dist-info/RECORD,,