Skip to content

Commit

Permalink
Disable default import of *vaab/colour* objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Nov 8, 2023
1 parent 231c945 commit 7929f7d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 59 deletions.
114 changes: 59 additions & 55 deletions colour/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,68 +960,72 @@ def __getattr__(self, attribute) -> Any:

# 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.
# There haven't been many clashes over the years but people using
# https://www.manim.community are experiencing problems:
# See the following issues for more information:
# - https://github.com/colour-science/colour/issues/958
# - https://github.com/colour-science/colour/issues/1221
if not os.environ.get(
"COLOUR_SCIENCE__COLOUR__SKIP_VAAB_COLOUR_INJECTION"
): # pragma: no cover
for _path in sys.path:
_module_path = os.path.join(_path, "colour.py")
if os.path.exists(_module_path):
import importlib.machinery
# - https://github.com/vaab/colour/issues/62

import colour # pyright: ignore
for _path in sys.path:
_module_path = os.path.join(_path, "colour.py")
if os.path.exists(_module_path):
import colour # pyright: ignore

_module = importlib.machinery.SourceFileLoader(
"__vaab_colour__", _module_path
).load_module()
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

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'Injecting "vaab/colour" "{name}" object into '
f'"Colour"\'s namespace!'
)
setattr(
sys.modules["colour"], name, getattr(_module, name)
)
import importlib.machinery

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

break
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 _module_path, _path

del os, sys
9 changes: 5 additions & 4 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +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__SKIP_VAAB_COLOUR_INJECTION`: Skip
`vaab/colour <https://github.com/vaab/colour>`__ objects injection into
**Colour** namespace. See the bottom of the core `__init__.py` module for
more information.
- `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

0 comments on commit 7929f7d

Please sign in to comment.