theme: Move theme handling to own file.

This commit is contained in:
Dylan Araps 2018-03-18 10:03:48 +11:00
parent 20ae5f8d7a
commit 1cff79bcc6
5 changed files with 82 additions and 58 deletions

View File

@ -15,6 +15,7 @@ from . import export
from . import image
from . import reload
from . import sequences
from . import theme
from . import wallpaper
__all__ = [
@ -25,5 +26,6 @@ __all__ = [
"image",
"reload",
"sequences",
"theme",
"wallpaper",
]

View File

@ -14,12 +14,13 @@ import os
import shutil
import sys
from .settings import __version__, CACHE_DIR, MODULE_DIR, CONF_DIR
from .settings import __version__, CACHE_DIR, CONF_DIR
from . import colors
from . import export
from . import image
from . import reload
from . import sequences
from . import theme
from . import util
from . import wallpaper
@ -105,11 +106,7 @@ def process_args(args):
sys.exit(0)
if args.f == "list_themes":
themes = os.listdir(os.path.join(CONF_DIR, "colorschemes"))
themes += os.listdir(os.path.join(MODULE_DIR, "colorschemes"))
themes = [theme.replace(".json", "") for theme in themes]
print("Themes:", ", ".join(themes))
print("Themes:", ", ".join(theme.index()))
sys.exit(0)
if args.q:
@ -133,7 +130,7 @@ def process_args(args):
colors_plain = colors.get(image_file, light=args.l)
if args.f:
colors_plain = colors.file(args.f)
colors_plain = theme.file(args.f)
if args.a:
util.Color.alpha_num = args.a
@ -165,6 +162,9 @@ def process_args(args):
def main():
"""Main script function."""
util.create_dir(os.path.join(CONF_DIR, "colorschemes"))
util.create_dir(os.path.join(CONF_DIR, "templates"))
args = get_args(sys.argv[1:])
process_args(args)

View File

@ -7,8 +7,9 @@ import shutil
import subprocess
import sys
from .settings import CACHE_DIR, COLOR_COUNT, MODULE_DIR, \
CONF_DIR, __cache_version__
from . import theme
from .settings import CACHE_DIR, COLOR_COUNT, __cache_version__
from . import util
@ -112,7 +113,7 @@ def get(img, cache_dir=CACHE_DIR,
% (cache_file, color_type, __cache_version__))
if os.path.isfile(cache_file):
colors = file(cache_file)
colors = theme.file(cache_file)
util.Color.alpha_num = colors["alpha"]
print("colors: Found cached colorscheme.")
@ -128,51 +129,6 @@ def get(img, cache_dir=CACHE_DIR,
return colors
def terminal_sexy_to_wal(data):
"""Convert terminal.sexy json schema to wal."""
data["colors"] = {}
data["special"] = {
"foreground": data["foreground"],
"background": data["background"],
"cursor": data["color"][9]
}
for i, color in enumerate(data["color"]):
data["colors"]["color%s" % i] = color
return data
def file(input_file):
"""Import colorscheme from json file."""
theme_file = ".".join((input_file, "json"))
user_theme_dir = os.path.join(CONF_DIR, "colorschemes")
user_theme_file = os.path.join(user_theme_dir, theme_file)
theme_file = os.path.join(MODULE_DIR, "colorschemes", theme_file)
util.create_dir(user_theme_dir)
if os.path.isfile(user_theme_file):
input_file = user_theme_file
elif os.path.isfile(theme_file):
input_file = theme_file
if os.path.isfile(input_file):
data = util.read_file_json(input_file)
if "wallpaper" not in data:
data["wallpaper"] = "None"
if "alpha" not in data:
data["alpha"] = "100"
# Terminal.sexy format.
if "color" in data:
data = terminal_sexy_to_wal(data)
return data
else:
print("No colorscheme file found, exiting...")
sys.exit(1)
"""Deprecated: symbolic link to --> theme.file"""
return theme.file(input_file)

View File

@ -53,7 +53,6 @@ def every(colors, output_dir=CACHE_DIR):
colors = flatten_colors(colors)
template_dir = os.path.join(MODULE_DIR, "templates")
template_dir_user = os.path.join(CONF_DIR, "templates")
util.create_dir(template_dir_user)
join = os.path.join # Minor optimization.

67
pywal/theme.py Normal file
View File

@ -0,0 +1,67 @@
"""
Theme file handling.
"""
import os
import sys
from .settings import CONF_DIR, MODULE_DIR
from . import util
def index():
"""List all installed theme files."""
themes = os.listdir(os.path.join(CONF_DIR, "colorschemes"))
themes += os.listdir(os.path.join(MODULE_DIR, "colorschemes"))
return [theme.replace(".json", "") for theme in themes]
def terminal_sexy_to_wal(data):
"""Convert terminal.sexy json schema to wal."""
data["colors"] = {}
data["special"] = {
"foreground": data["foreground"],
"background": data["background"],
"cursor": data["color"][9]
}
for i, color in enumerate(data["color"]):
data["colors"]["color%s" % i] = color
return data
def file(input_file):
"""Import colorscheme from json file."""
theme_name = ".".join((input_file, "json"))
user_theme_file = os.path.join(CONF_DIR, "colorschemes", theme_name)
theme_file = os.path.join(MODULE_DIR, "colorschemes", theme_name)
# Find the theme file.
if os.path.isfile(input_file):
theme_file = input_file
elif os.path.isfile(user_theme_file):
theme_file = user_theme_file
elif os.path.isfile(theme_file):
theme_file = theme_file
# Parse the theme file.
if os.path.isfile(theme_file):
data = util.read_file_json(theme_file)
if "wallpaper" not in data:
data["wallpaper"] = "None"
if "alpha" not in data:
data["alpha"] = "100"
# Terminal.sexy format.
if "color" in data:
data = terminal_sexy_to_wal(data)
return data
else:
print("No colorscheme file found, exiting...")
sys.exit(1)