2 Export colors in various formats.
7 from .settings import CACHE_DIR, MODULE_DIR, CONF_DIR
11 def template(colors, input_file, output_file=None):
12 """Read template file, substitute markers and
13 save the file elsewhere."""
14 template_data = util.read_file_raw(input_file)
15 template_data = "".join(template_data).format(**colors)
17 util.save_file(template_data, output_file)
20 def flatten_colors(colors):
21 """Prepare colors to be exported.
22 Flatten dicts and convert colors to util.Color()"""
23 all_colors = {"wallpaper": colors["wallpaper"],
24 "alpha": colors["alpha"],
27 return {k: util.Color(v) for k, v in all_colors.items()}
30 def get_export_type(export_type):
31 """Convert template type to the right filename."""
34 "dmenu": "colors-wal-dmenu.h",
35 "dwm": "colors-wal-dwm.h",
36 "st": "colors-wal-st.h",
37 "tabbed": "colors-wal-tabbed.h",
38 "gtk2": "colors-gtk2.rc",
39 "json": "colors.json",
40 "konsole": "colors-konsole.colorscheme",
41 "kitty": "colors-kitty.conf",
43 "putty": "colors-putty.reg",
44 "rofi": "colors-rofi.Xresources",
45 "scss": "colors.scss",
47 "speedcrunch": "colors-speedcrunch.json",
48 "sway": "colors-sway",
49 "tty": "colors-tty.sh",
50 "waybar": "colors-waybar.css",
51 "xresources": "colors.Xresources",
52 "xmonad": "colors.hs",
54 }.get(export_type, export_type)
57 def every(colors, output_dir=CACHE_DIR):
58 """Export all template files."""
59 colors = flatten_colors(colors)
60 template_dir = os.path.join(MODULE_DIR, "templates")
61 template_dir_user = os.path.join(CONF_DIR, "templates")
62 util.create_dir(template_dir_user)
64 join = os.path.join # Minor optimization.
65 for file in [*os.scandir(template_dir),
66 *os.scandir(template_dir_user)]:
67 if file.name != ".DS_Store":
68 template(colors, file.path, join(output_dir, file.name))
70 logging.info("Exported all files.")
71 logging.info("Exported all user files.")
74 def color(colors, export_type, output_file=None):
75 """Export a single template file."""
76 all_colors = flatten_colors(colors)
78 template_name = get_export_type(export_type)
79 template_file = os.path.join(MODULE_DIR, "templates", template_name)
80 output_file = output_file or os.path.join(CACHE_DIR, template_name)
82 if os.path.isfile(template_file):
83 template(all_colors, template_file, output_file)
84 logging.info("Exported %s.", export_type)
86 logging.warning("Template '%s' doesn't exist.", export_type)