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

PR: Implement support for injecting vaab/colour objects into our namespace. #1222

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion colour/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"""

import contextlib
import os
import sys

import numpy as np
Expand Down Expand Up @@ -948,11 +949,85 @@ def __getattr__(self, attribute) -> Any:
sys.modules["colour"], build_API_changes(API_CHANGES)
)

del ModuleAPI, is_documentation_building, build_API_changes, sys
del ModuleAPI, is_documentation_building, build_API_changes

colour.__disable_lazy_load__ = True # pyright: ignore
__disable_lazy_load__ = colour.__disable_lazy_load__ # pyright: ignore
"""
Ensures that the lazy loaded datasets are not transformed during import.
See :class:`colour.utilities.LazyCanonicalMapping` for more information.
"""

# NOTE: We are solving the clash with https://github.com/vaab/colour by loading
# a known subset of the objects given by vaab/colour-0.1.5 into our namespace
# if the *COLOUR_SCIENCE__COLOUR__IMPORT_VAAB_COLOUR=True* environment variable
# is defined.
#
# See the following issues for more information:
# - https://github.com/colour-science/colour/issues/958
# - https://github.com/colour-science/colour/issues/1221
# - https://github.com/vaab/colour/issues/62
for _path in sys.path:
_module_path = os.path.join(_path, "colour.py")
if os.path.exists(_module_path):
import colour # pyright: ignore

if not os.environ.get("COLOUR_SCIENCE__COLOUR__IMPORT_VAAB_COLOUR"):
colour.utilities.warning( # pyright: ignore
'"vaab/colour" was detected in "sys.path", please define a '
'"COLOUR_SCIENCE__COLOUR__IMPORT_VAAB_COLOUR=True" environment '
'variable to import its objects into "colour" namespace!'
)
break

import importlib.machinery

_module = importlib.machinery.SourceFileLoader(
"__vaab_colour__", _module_path
).load_module()

for name in [
"COLOR_NAME_TO_RGB",
"C_HEX",
"C_HSL",
"C_RGB",
"Color",
"HEX",
"HSL",
"HSL_equivalence",
"LONG_HEX_COLOR",
"RGB",
"RGB_TO_COLOR_NAMES",
"RGB_color_picker",
"RGB_equivalence",
"SHORT_HEX_COLOR",
"color_scale",
"hash_or_str",
"hex2hsl",
"hex2rgb",
"hex2web",
"hsl2hex",
"hsl2rgb",
"hsl2web",
"make_color_factory",
"rgb2hex",
"rgb2hsl",
"rgb2web",
"web2hex",
"web2hsl",
"web2rgb",
]:
if name in dir(_module):
colour.utilities.warning( # pyright: ignore
f'Importing "vaab/colour" "{name}" object into '
f'"Colour"\'s namespace!'
)
setattr(sys.modules["colour"], name, getattr(_module, name))

del importlib, _module

break

del _module_path, _path

del os, sys
5 changes: 5 additions & 0 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ runtime:
:func:`warnings.showwarning` definition to be replaced with the
:func:`colour.utilities.show_warning` definition and thus providing
complete traceback from the point where the warning occurred.
- `COLOUR_SCIENCE__COLOUR__IMPORT_VAAB_COLOUR`: Import
`vaab/colour <https://github.com/vaab/colour>`__ injection into
**Colour** namespace. This solves the clash with
`vaab/colour <https://github.com/vaab/colour>`__ by loading a known subset
of the objects given by vaab/colour-0.1.5 into our namespace.

Caching
-------
Expand Down