efficient vim config
[dotfiles/.git] / .local / lib / python3.9 / site-packages / pywal / export.py
1 """
2 Export colors in various formats.
3 """
4 import logging
5 import os
6
7 from .settings import CACHE_DIR, MODULE_DIR, CONF_DIR
8 from . import util
9
10
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)
16
17     util.save_file(template_data, output_file)
18
19
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"],
25                   **colors["special"],
26                   **colors["colors"]}
27     return {k: util.Color(v) for k, v in all_colors.items()}
28
29
30 def get_export_type(export_type):
31     """Convert template type to the right filename."""
32     return {
33         "css": "colors.css",
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",
42         "plain": "colors",
43         "putty": "colors-putty.reg",
44         "rofi": "colors-rofi.Xresources",
45         "scss": "colors.scss",
46         "shell": "colors.sh",
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",
53         "yaml": "colors.yml",
54     }.get(export_type, export_type)
55
56
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)
63
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))
69
70     logging.info("Exported all files.")
71     logging.info("Exported all user files.")
72
73
74 def color(colors, export_type, output_file=None):
75     """Export a single template file."""
76     all_colors = flatten_colors(colors)
77
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)
81
82     if os.path.isfile(template_file):
83         template(all_colors, template_file, output_file)
84         logging.info("Exported %s.", export_type)
85     else:
86         logging.warning("Template '%s' doesn't exist.", export_type)