Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a hook to autoupdate black in user configuration #124

Merged
merged 11 commits into from
Sep 13, 2022
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