summaryrefslogtreecommitdiff
path: root/pywal/export.py
blob: 06ad215369e4ef8e64fba8551626aa85bde1fc83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Export colors in various formats.
"""
import os

from pywal import util


def template(colors, input_file, output_dir):
    """Read template file, substitute markers and
       save the file elsewhere."""
    # Import the template.
    with open(input_file) as file:
        template_data = file.readlines()

    # Format the markers.
    template_data = "".join(template_data).format(**colors)

    # Get the template name.
    template_file = os.path.basename(input_file)

    # Export the template.
    output_file = output_dir / template_file
    util.save_file(template_data, output_file)

    print(f"export: Exported {template_file}.")


def export_all_templates(colors, output_dir, template_dir=None):
    """Export all template files."""
    # Add the template dir to module path.
    template_dir = template_dir or \
        os.path.join(os.path.dirname(__file__), "templates")

    # Merge all colors (specials and normals) into one dict so we can access
    # their values simpler.
    all_colors = {"wallpaper": colors["wallpaper"],
                  **colors["special"],
                  **colors["colors"]}

    # Turn all those colors into util.Color instances for accessing the
    # .hex and .rgb formats
    all_colors = {k: util.Color(v) for k, v in all_colors.items()}

    # pylint: disable=W0106
    [template(all_colors, file.path, output_dir)
     for file in os.scandir(template_dir)]