Skip to content

Commit

Permalink
add a hook to autoupdate black in user configuration (#124)
Browse files Browse the repository at this point in the history
* add a rough script

* register the script as a hook

* move the script code to a function

* register the script as a executable

* try to use python -m

* finish the script

* remove the additional dependency

* remove the autoupdate console entrypoint

* changelog [skip-ci]

* rename the hook [skip-ci]
  • Loading branch information
keewis authored Sep 13, 2022
1 parent c184d8e commit 2910411
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@
require_serial: true
types: [file]
files: \.(py|rst)$

- id: blackdoc-autoupdate-black
name: autoupdate-black
entry: python -m blackdoc.autoupdate
language: python
language_version: python3
require_serial: true
types: [file]
files: \.pre-commit-config\.yaml
33 changes: 33 additions & 0 deletions blackdoc/autoupdate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import argparse
import re

version_re = re.compile(r"black\s+rev: (.+)\s+hooks:\s+- id: black")
black_pin_re = re.compile(
r"(- id: blackdoc.+?additional_dependencies:.+?black==)[.\w]+",
re.DOTALL,
)


def main():
parser = argparse.ArgumentParser()
parser.add_argument("path")
args = parser.parse_args()
with open(args.path) as f:
content = f.read()

match = version_re.search(content)
if match is None:
raise ValueError("cannot find the black hook")
version = match.group(1)
replaced = black_pin_re.sub(rf"\g<1>{version}", content)

if content != replaced:
with open(args.path, mode="w") as f:
f.write(replaced)
return 1
else:
return 0


if __name__ == "__main__":
raise SystemExit(main())
2 changes: 2 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ v0.4.0 (*unreleased*)
---------------------
- replace docstrings by modifying by token (:pull:`144`)
- switch the html theme to `furo <https://pradyunsg.me/furo>`_ (:pull:`149`)
- add a new hook to synchronize `black` pinned in `additional_dependencies` with the version from
the `black` hook (:pull:`124`)

v0.3.6 (25 August 2022)
-----------------------
Expand Down

0 comments on commit 2910411

Please sign in to comment.