diff --git a/.github/workflows/continuous-integration-quality-unit-tests.yml b/.github/workflows/continuous-integration-quality-unit-tests.yml index 3e2b92354..98d9213bc 100644 --- a/.github/workflows/continuous-integration-quality-unit-tests.yml +++ b/.github/workflows/continuous-integration-quality-unit-tests.yml @@ -63,6 +63,8 @@ jobs: ln -s /usr/local/Cellar/openimageio/*/lib/python*/site-packages/OpenImageIO/OpenImageIO*.so /Library/Frameworks/Python.framework/Versions/${{ matrix.python-version }}/lib/python${{ matrix.python-version }}/site-packages/OpenImageIO.so shell: bash - name: Pre-Commit (All Files) + # "poetry" is not available on Windows from within "pre-commit". + if: matrix.os == 'macOS-latest' || matrix.os == 'ubuntu-22.04' run: | poetry run pre-commit run --all-files shell: bash diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b527df66f..510e31749 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,4 +1,12 @@ repos: +- repo: local + hooks: + - id: literalise + name: literalise + entry: poetry run python utilities/literalise.py + always_run: true + language: system + types: [ python ] - repo: https://github.com/ikamensh/flynt/ rev: '1.0.1' hooks: diff --git a/tasks.py b/tasks.py index 6b38a42d2..5ee923bf9 100644 --- a/tasks.py +++ b/tasks.py @@ -11,7 +11,6 @@ import os import re import uuid -from textwrap import dedent import colour from colour.utilities import message_box @@ -38,7 +37,6 @@ "PYPI_PACKAGE_NAME", "PYPI_ARCHIVE_NAME", "BIBLIOGRAPHY_NAME", - "literalise", "clean", "formatting", "quality", @@ -68,56 +66,6 @@ BIBLIOGRAPHY_NAME: str = "BIBLIOGRAPHY.bib" -@task -def literalise(ctx: Context): - """ - Write various literals in the `colour.hints` module. - - Parameters - ---------- - ctx - Context. - """ - - path_hints = os.path.join( - os.path.dirname(__file__), "colour", "hints", "__init__.py" - ) - - with open(path_hints) as file_hints: - content = file_hints.read() - - content = re.sub( - "# LITERALISE::BEGIN.*?# LITERALISE::END", - dedent( - f""" - # LITERALISE::BEGIN - LiteralChromaticAdaptationTransform = \ - Literal{sorted(colour.CHROMATIC_ADAPTATION_TRANSFORMS)} - LiteralColourspaceModel = Literal{sorted(colour.COLOURSPACE_MODELS)} - LiteralRGBColourspace = Literal{sorted(colour.RGB_COLOURSPACES.keys())} - LiteralLogEncoding = Literal{sorted(colour.LOG_ENCODINGS)} - LiteralLogDecoding = Literal{sorted(colour.LOG_DECODINGS)} - LiteralOETF = Literal{sorted(colour.OETFS)} - LiteralOETFInverse = Literal{sorted(colour.OETF_INVERSES)} - LiteralEOTF = Literal{sorted(colour.EOTFS)} - LiteralEOTFInverse = Literal{sorted(colour.EOTF_INVERSES)} - LiteralCCTFEncoding = Literal{sorted(colour.CCTF_ENCODINGS)} - LiteralCCTFDecoding = Literal{sorted(colour.CCTF_DECODINGS)} - LiteralOOTF = Literal{sorted(colour.OOTFS)} - LiteralOOTFInverse = Literal{sorted(colour.OOTF_INVERSES)} - # LITERALISE::END - """ - ).strip(), - content, - flags=re.DOTALL, - ) - - with open(path_hints, "w") as file_hints: - file_hints.write(content) - - ctx.run(f"pre-commit run --files {path_hints}") - - @task def clean( ctx: Context, @@ -399,7 +347,7 @@ def requirements(ctx: Context): ) -@task(literalise, clean, preflight, docs, todo, requirements) +@task(clean, preflight, docs, todo, requirements) def build(ctx: Context): """ Build the project and runs dependency tasks, i.e. *docs*, *todo*, and diff --git a/utilities/literalise.py b/utilities/literalise.py new file mode 100755 index 000000000..91022275e --- /dev/null +++ b/utilities/literalise.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +""" +Literalise +========== +""" + +from __future__ import annotations + +import os +import re +from textwrap import dedent + +import sys +import subprocess + +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) + +import colour # noqa: E402 + +__copyright__ = "Copyright 2013 Colour Developers" +__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause" +__maintainer__ = "Colour Developers" +__email__ = "colour-developers@colour-science.org" +__status__ = "Production" + +__all__ = [ + "PATH_MODULE_HINTS", + "literalise", +] + +PATH_MODULE_HINTS = os.path.join( + os.path.dirname(__file__), "..", "colour", "hints", "__init__.py" +) + + +def literalise(path_module_hints: str = PATH_MODULE_HINTS): + """ + Write various literals in the `colour.hints` module. + + Parameters + ---------- + path_module_hints + Path to the hints module. + """ + + with open(path_module_hints) as file_module_hints: + content = file_module_hints.read() + + content = re.sub( + "# LITERALISE::BEGIN.*?# LITERALISE::END", + dedent( + f""" + # LITERALISE::BEGIN + LiteralChromaticAdaptationTransform = \ + Literal{sorted(colour.CHROMATIC_ADAPTATION_TRANSFORMS)} + LiteralColourspaceModel = Literal{sorted(colour.COLOURSPACE_MODELS)} + LiteralRGBColourspace = Literal{sorted(colour.RGB_COLOURSPACES.keys())} + LiteralLogEncoding = Literal{sorted(colour.LOG_ENCODINGS)} + LiteralLogDecoding = Literal{sorted(colour.LOG_DECODINGS)} + LiteralOETF = Literal{sorted(colour.OETFS)} + LiteralOETFInverse = Literal{sorted(colour.OETF_INVERSES)} + LiteralEOTF = Literal{sorted(colour.EOTFS)} + LiteralEOTFInverse = Literal{sorted(colour.EOTF_INVERSES)} + LiteralCCTFEncoding = Literal{sorted(colour.CCTF_ENCODINGS)} + LiteralCCTFDecoding = Literal{sorted(colour.CCTF_DECODINGS)} + LiteralOOTF = Literal{sorted(colour.OOTFS)} + LiteralOOTFInverse = Literal{sorted(colour.OOTF_INVERSES)} + # LITERALISE::END + """ + ).strip(), + content, + flags=re.DOTALL, + ) + + with open(path_module_hints, "w") as file_module_hints: + file_module_hints.write(content) + + subprocess.run(["black", PATH_MODULE_HINTS]) # noqa: PLW1510, S603, S607 + + +if __name__ == "__main__": + os.chdir(os.path.dirname(__file__)) + + literalise()