Merge pull request #19 from dylanaraps/json

colors: Use template files and un-hard-code export formats.
This commit is contained in:
Dylan Araps 2017-06-29 23:08:57 +10:00 committed by GitHub
commit 55d4636af8
13 changed files with 252 additions and 190 deletions

View File

@ -11,6 +11,7 @@ from pywal.settings import CACHE_DIR, __version__
from pywal import export_colors
from pywal import gen_colors
from pywal import set_colors
from pywal import reload
from pywal import wallpaper
from pywal import util
@ -94,15 +95,15 @@ def process_args(args):
if not args.n:
wallpaper.set_wallpaper(image)
# Set the colors.
set_colors.send_sequences(colors_plain, args.t)
export_colors.export_colors(colors_plain)
# -f
elif args.f:
colors_plain = util.read_file_json(args.f)
# -i or -f
if args.i or args.f:
set_colors.send_sequences(colors_plain, args.t)
export_colors.export_colors(colors_plain)
export_colors.export_all_templates(colors_plain)
reload.reload_env()
# -o
if args.o:

View File

@ -1,56 +1,56 @@
"""
Export colors in various formats.
"""
import shutil
import subprocess
import os
import pathlib
from pywal.settings import CACHE_DIR
from pywal import util
from pywal import format_colors
def save_colors(colors, export_file, message):
"""Export colors to var format."""
colors = "".join(colors)
util.save_file(colors, CACHE_DIR / export_file)
print(f"export: exported {message}.")
def template(colors, input_file, output_dir):
"""Read template file, substitute markers and
save the file elsewhere."""
# Get the template name.
template_file = os.path.basename(input_file)
# Import the template.
with open(input_file) as file:
template_data = file.readlines()
# Format the markers.
template_data = "".join(template_data).format(**colors)
# Export the template.
output_file = output_dir / template_file
util.save_file(template_data, output_file)
print(f"export: Exported {template_file}.")
def reload_xrdb(export_file):
"""Merge the colors into the X db so new terminals use them."""
if shutil.which("xrdb"):
subprocess.call(["xrdb", "-merge", CACHE_DIR / export_file])
def export_all_templates(colors):
"""Export all template files."""
# Add the template dir to module path.
template_dir = os.path.join(os.path.dirname(__file__), "templates")
# Exclude these templates from the loop.
# The excluded templates need color
# conversion or other intervention.
exclude = ["colors-putty.reg"]
def reload_i3():
"""Reload i3 colors."""
if shutil.which("i3-msg"):
util.disown("i3-msg", "reload")
# Merge both dicts so we can access their
# values simpler.
colors["colors"].update(colors["special"])
# Convert colors to other format.
colors_rgb = {k: util.hex_to_rgb(v) for k, v in colors["colors"].items()}
def export_colors(colors):
"""Export colors in various formats."""
plain_colors = format_colors.plain(colors)
save_colors(plain_colors, "colors", "plain hex colors")
# pylint: disable=W0106
[template(colors["colors"], file.path, CACHE_DIR)
for file in os.scandir(template_dir)
if file.name not in exclude]
# Shell based colors.
shell_colors = format_colors.shell(colors)
save_colors(shell_colors, "colors.sh", "shell variables")
# Web based colors.
css_colors = format_colors.css(colors)
save_colors(css_colors, "colors.css", "css variables")
scss_colors = format_colors.scss(colors)
save_colors(scss_colors, "colors.scss", "scss variables")
# Text editor based colors.
putty_colors = format_colors.putty(colors)
save_colors(putty_colors, "colors-putty.reg", "putty theme")
# X based colors.
xrdb_colors = format_colors.xrdb(colors)
save_colors(xrdb_colors, "xcolors", "xrdb colors")
# i3 colors.
reload_xrdb("xcolors")
reload_i3()
# Call 'putty' manually since it needs RGB
# colors.
putty_file = template_dir / pathlib.Path("colors-putty.reg")
template(colors_rgb, putty_file, CACHE_DIR)

View File

@ -1,92 +0,0 @@
"""
Convert colors to various formats.
"""
from pywal import util
def plain(colors):
"""Convert colors to plain hex."""
return [f"{color}\n" for color in colors["colors"].values()]
def shell(colors):
"""Convert colors to shell variables."""
return [f"color{index}='{color}'\n"
for index, color in enumerate(colors["colors"].values())]
def css(colors):
"""Convert colors to css variables."""
css_colors = [":root {\n"]
css_colors.extend([f"\t--color{index}: {color};\n"
for index, color in
enumerate(colors["colors"].values())])
css_colors.append("}\n")
return css_colors
def scss(colors):
"""Convert colors to scss variables."""
return [f"$color{index}: {color};\n"
for index, color in enumerate(colors["colors"].values())]
def putty(colors):
"""Convert colors to putty theme."""
rgb = util.hex_to_rgb
putty_colors = [
"Windows Registry Editor Version 5.00\n\n",
"[HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\Wal]\n",
]
putty_colors.extend([f"\"colour{index}\"=\"{rgb(color)}\"\n"
for index, color in
enumerate(colors["colors"].values())])
return putty_colors
def xrdb(colors):
"""Convert colors to xrdb format."""
x_colors = []
x_colors.append(f"URxvt*foreground: {colors['special']['foreground']}\n")
x_colors.append(f"XTerm*foreground: {colors['special']['foreground']}\n")
x_colors.append(f"URxvt*background: {colors['special']['background']}\n")
x_colors.append(f"XTerm*background: {colors['special']['background']}\n")
x_colors.append(f"URxvt*cursorColor: {colors['special']['cursor']}\n")
x_colors.append(f"XTerm*cursorColor: {colors['special']['cursor']}\n")
# Colors 0-15.
x_colors.extend([f"*.color{index}: {color}\n*color{index}: {color}\n"
for index, color in enumerate(colors["colors"].values())])
x_colors.append(f"*.color66: {colors['special']['background']}\n"
f"*color66: {colors['special']['background']}\n")
# Rofi colors.
x_colors.append(f"rofi.color-window: "
f"{colors['special']['background']}, "
f"{colors['special']['background']}, "
f"{colors['colors']['color10']}\n")
x_colors.append(f"rofi.color-normal: "
f"{colors['special']['background']}, "
f"{colors['special']['foreground']}, "
f"{colors['special']['background']}, "
f"{colors['colors']['color10']}, "
f"{colors['special']['background']}\n")
x_colors.append(f"rofi.color-active: "
f"{colors['special']['background']}, "
f"{colors['special']['foreground']}, "
f"{colors['special']['background']}, "
f"{colors['colors']['color10']}, "
f"{colors['special']['background']}\n")
x_colors.append(f"rofi.color-urgent: "
f"{colors['special']['background']}, "
f"{colors['colors']['color9']}, "
f"{colors['special']['background']}, "
f"{colors['colors']['color9']}, "
f"{colors['special']['foreground']}\n")
# Emacs colors.
x_colors.append(f"emacs*background: {colors['special']['background']}\n")
x_colors.append(f"emacs*foreground: {colors['special']['foreground']}\n")
return x_colors

27
pywal/reload.py Normal file
View File

@ -0,0 +1,27 @@
"""
Reload programs.
"""
import shutil
import subprocess
from pywal.settings import CACHE_DIR
from pywal import util
def reload_i3():
"""Reload i3 colors."""
if shutil.which("i3-msg"):
util.disown("i3-msg", "reload")
def reload_xrdb():
"""Merge the colors into the X db so new terminals use them."""
if shutil.which("xrdb"):
subprocess.call(["xrdb", "-merge", CACHE_DIR / "colors.Xresources"])
def reload_env():
"""Reload environment programs."""
reload_i3()
reload_xrdb()
print("reload: Reloaded environment.")

View File

@ -0,0 +1,19 @@
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\Default%20Settings]]
"colour0"="{color0}"
"colour1"="{color1}"
"colour2"="{color2}"
"colour3"="{color3}"
"colour4"="{color4}"
"colour5"="{color5}"
"colour6"="{color6}"
"colour7"="{color7}"
"colour8"="{color8}"
"colour9"="{color9}"
"colour10"="{color10}"
"colour11"="{color11}"
"colour12"="{color12}"
"colour13"="{color13}"
"colour14"="{color14}"
"colour15"="{color15}"

View File

@ -0,0 +1,55 @@
! X colors
! Generated by 'wal'
URxvt*foreground: {foreground}
XTerm*foreground: {foreground}
URxvt*background: {background}
XTerm*background: {background}
URxvt*cursorColor: {cursor}
XTerm*cursorColor: {cursor}
*.color0: {color0}
*color0: {color0}
*.color1: {color1}
*color1: {color1}
*.color2: {color2}
*color2: {color2}
*.color3: {color3}
*color3: {color3}
*.color4: {color4}
*color4: {color4}
*.color5: {color5}
*color5: {color5}
*.color6: {color6}
*color6: {color6}
*.color7: {color7}
*color7: {color7}
*.color8: {color8}
*color8: {color8}
*.color9: {color9}
*color9: {color9}
*.color10: {color10}
*color10: {color10}
*.color11: {color11}
*color11: {color11}
*.color12: {color12}
*color12: {color12}
*.color13: {color13}
*color13: {color13}
*.color14: {color14}
*color14: {color14}
*.color15: {color15}
*color15: {color15}
! Black color that will not be affected by bold highlighting.
*.color66: {color0}
*color66: {color0}
! Rofi colors.
rofi.color-window: {background}, {background}, {color10}
rofi.color-normal: {background}, {foreground}, {background}, {color10}, {background}
rofi.color-active: {background}, {foreground}, {background}, {color10}, {background}
rofi.color-urgent: {background}, {color9}, {background}, {color9}, {foreground}
! Emacs colors.
emacs*background: {background}
emacs*foreground: {foreground}

View File

@ -0,0 +1,23 @@
/* CSS variables
Generated by 'wal' */
:root {{
--background: {background};
--foreground: {foreground};
--cursor: {cursor};
--color0: {color0};
--color1: {color1};
--color2: {color2};
--color3: {color3};
--color4: {color4};
--color5: {color5};
--color6: {color6};
--color7: {color7};
--color8: {color8};
--color9: {color9};
--color10: {color10};
--color11: {color11};
--color12: {color12};
--color13: {color13};
--color14: {color14};
--color15: {color15};
}}

View File

@ -0,0 +1,25 @@
{{
"special": {{
"background": "{background}",
"foreground": "{foreground}",
"cursor": "{cursor}"
}},
"colors": {{
"color0": "{color0}",
"color1": "{color1}",
"color2": "{color2}",
"color3": "{color3}",
"color4": "{color4}",
"color5": "{color5}",
"color6": "{color6}",
"color7": "{color7}",
"color8": "{color8}",
"color9": "{color9}",
"color10": "{color10}",
"color11": "{color11}",
"color12": "{color12}",
"color13": "{color13}",
"color14": "{color14}",
"color15": "{color15}"
}}
}}

View File

@ -0,0 +1,21 @@
// SCSS Variables
// Generated by 'wal'
$background: {background};
$foreground: {foreground};
$cursor: {cursor};
$color0: {color0};
$color1: {color1};
$color2: {color2};
$color3: {color3};
$color4: {color4};
$color5: {color5};
$color6: {color6};
$color7: {color7};
$color8: {color8};
$color9: {color9};
$color10: {color10};
$color11: {color11};
$color12: {color12};
$color13: {color13};
$color14: {color14};
$color15: {color15};

21
pywal/templates/colors.sh Normal file
View File

@ -0,0 +1,21 @@
# Shell variables
# Generated by 'wal'
background='{background}'
foreground='{foreground}'
cursor='{cursor}'
color0='{color0}'
color1='{color1}'
color2='{color2}'
color3='{color3}'
color4='{color4}'
color5='{color5}'
color6='{color6}'
color7='{color7}'
color8='{color8}'
color9='{color9}'
color10='{color10}'
color11='{color11}'
color12='{color12}'
color13='{color13}'
color14='{color14}'
color15='{color15}'

View File

@ -13,11 +13,18 @@ COLORS = util.read_file_json("tests/test_files/test_file.json")
class TestExportColors(unittest.TestCase):
"""Test the export_colors functions."""
def test_save_colors(self):
"""> Export colors to a file."""
tmp_file = pathlib.Path("/tmp/test_file.json")
export_colors.save_colors(COLORS, tmp_file, "plain colors")
result = tmp_file.is_file()
def test_template(self):
"""> Test substitutions in template file."""
# Merge both dicts so we can access their
# values simpler.
COLORS["colors"].update(COLORS["special"])
# Dirs to use.
tmp_dir = pathlib.Path("/tmp")
test_template = pathlib.Path("tests/test_files/test_template")
export_colors.template(COLORS["colors"], test_template, tmp_dir)
result = pathlib.Path("/tmp/test_template").is_file()
self.assertTrue(result)

View File

@ -0,0 +1,2 @@
test {color0}
test {background}

View File

@ -1,47 +0,0 @@
"""Test format functions."""
import unittest
from pywal import format_colors
from pywal import util
# Import colors.
COLORS = util.read_file_json("tests/test_files/test_file.json")
class TestFormatColors(unittest.TestCase):
"""Test the format_colors functions."""
def test_plain(self):
"""> Convert colors to plain."""
result = format_colors.plain(COLORS)
self.assertEqual(result[0], "#3A5130\n")
def test_shell(self):
"""> Convert colors to shell variables."""
result = format_colors.shell(COLORS)
self.assertEqual(result[0], "color0='#3A5130'\n")
def test_css(self):
"""> Convert colors to css variables."""
result = format_colors.css(COLORS)
self.assertEqual(result[1], "\t--color0: #3A5130;\n")
def test_scss(self):
"""> Convert colors to scss variables."""
result = format_colors.scss(COLORS)
self.assertEqual(result[0], "$color0: #3A5130;\n")
def test_putty(self):
"""> Convert colors to putty theme."""
result = format_colors.putty(COLORS)
self.assertEqual(result[2], "\"colour0\"=\"58,81,48\"\n")
def test_xrdb(self):
"""> Convert colors to putty theme."""
result = format_colors.xrdb(COLORS)
self.assertEqual(result[6], "*.color0: #3A5130\n*color0: #3A5130\n")
if __name__ == "__main__":
unittest.main()