Skip to content

Commit

Permalink
Implement literalise run via pre-commit hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Sep 15, 2023
1 parent 8609a7f commit b6ff863
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
54 changes: 1 addition & 53 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import os
import re
import uuid
from textwrap import dedent

import colour
from colour.utilities import message_box
Expand All @@ -38,7 +37,6 @@
"PYPI_PACKAGE_NAME",
"PYPI_ARCHIVE_NAME",
"BIBLIOGRAPHY_NAME",
"literalise",
"clean",
"formatting",
"quality",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions utilities/literalise.py
Original file line number Diff line number Diff line change
@@ -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__ = "[email protected]"
__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()

0 comments on commit b6ff863

Please sign in to comment.