backends: Dynamically import the backend we want to use.

This commit is contained in:
Dylan Araps 2018-04-01 08:39:32 +10:00
parent 83cdbaedb8
commit fc5f167d19

View File

@ -5,14 +5,15 @@ import os
import re import re
import sys import sys
from . import backends
from . import util from . import util
from .settings import CACHE_DIR, __cache_version__ from .settings import CACHE_DIR, MODULE_DIR, __cache_version__
def list_backends(): def list_backends():
"""List color backends.""" """List color backends."""
return [backend for backend in dir(backends) if "__" not in backend] return [b.name.replace(".py", "") for b in
os.scandir(os.path.join(MODULE_DIR, "backends"))
if "__" not in b.name]
def colors_to_dict(colors, img): def colors_to_dict(colors, img):
@ -92,7 +93,13 @@ def get(img, light=False, backend="wal", cache_dir=CACHE_DIR):
else: else:
print("wal: Generating a colorscheme...") print("wal: Generating a colorscheme...")
# Dynamic shiz. # Dynamically import the backend we want to use.
# This keeps the dependencies "optional".
try:
__import__("pywal.backends.%s" % backend)
except ImportError:
backend = "wal"
backend = sys.modules["pywal.backends.%s" % backend] backend = sys.modules["pywal.backends.%s" % backend]
colors = colors_to_dict(getattr(backend, "get")(img, light), img) colors = colors_to_dict(getattr(backend, "get")(img, light), img)