Applying Post Processing and Python Code Formatters to Generated Revisions

Revision scripts generated by the alembic revision command can optionally be piped through a series of post-production functions which may analyze or rewrite Python source code generated by Alembic, within the scope of running the revision command. The primary intended use of this feature is to run code-formatting tools such as Black or autopep8, as well as custom-written formatting and linter functions, on revision files as Alembic generates them. Any number of hooks can be configured and they will be run in series, given the path to the newly generated file as well as configuration options.

由 alembic 修订命令生成的修订脚本可以选择通过一系列后期制作功能进行管道传输,这些功能可以在运行修订命令的范围内分析或重写 Alembic 生成的 Python 源代码。 此功能的主要预期用途是在 Alembic 生成修订文件时在修订文件上运行代码格式化工具,例如 Blackautopep8 ,以及自定义编写的格式化和 linter 函数。 给定新生成文件的路径以及配置选项,可以配置任意数量的钩子,并且它们将连续运行。

The post write hooks, when configured, run against generated revision files regardless of whether or not the autogenerate feature was used.

对于提交的钩子,在配置后,无论是否使用自动生成功能,都会针对生成的修订文件运行。

New in version 1.2.0

版本1.2.0 新增

Note: Alembic’s post write system is partially inspired by the pre-commit tool, which configures git hooks that reformat source files as they are committed to a git repository. Pre-commit can serve this role for Alembic revision files as well, applying code formatters to them as they are committed. Alembic’s post write hooks are useful only in that they can format the files immediately upon generation, rather than at commit time, and also can be useful for projects that prefer not to use pre-commit.

注意: Alembic 的写后系统部分受到 pre-commit 工具的启发,该工具配置了 git 钩子,在源文件提交到 git 存储库时重新格式化它们。 预提交也可以为 Alembic 修订文件服务,在提交时将代码格式化程序应用于它们。 Alembic 的 post write hook 的用处仅在于它们可以在生成文件时立即格式化文件,而不是在提交时格式化,并且对于不喜欢使用 pre-commit 的项目也很有用。