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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
# SPDX-License-Identifier: GPL-3.0-or-later
"""Generate Graphs GResource at build time."""
import argparse
import importlib.util
import logging
import shutil
import sys
from pathlib import Path
from xml.etree import ElementTree
from PIL import Image
from gi.repository import Gio
from matplotlib import font_manager
import numpy
parser = argparse.ArgumentParser(
description="Generate Graphs GResource at build time.",
)
parser.add_argument(
"out",
help="The output file",
)
parser.add_argument(
"dir",
help="Path to build directory",
)
parser.add_argument(
"style_io",
help="Path to style_io.py",
)
parser.add_argument(
"--ui",
required=True,
nargs="+",
dest="ui",
help="List of UI Files.",
)
parser.add_argument(
"--styles",
required=True,
nargs="+",
dest="styles",
help="List of Styles.",
)
parser.add_argument(
"--other",
required=True,
nargs="+",
dest="other",
help="List of other Files to include at toplevel.",
)
parser.add_argument(
"--icons",
required=True,
nargs="+",
dest="icons",
help="List of icon Files.",
)
args = parser.parse_args()
# Check fonts
font_list = font_manager.findSystemFonts(fontpaths=None, fontext="ttf")
for font in font_list:
try:
font_manager.fontManager.addfont(font)
except RuntimeError:
logging.warning("Could not load %s", font)
# Disable matplotlib logging
logging.getLogger("matplotlib.font_manager").setLevel(logging.ERROR)
# dynamically import style_io
spec = importlib.util.spec_from_file_location("style_io", args.style_io)
style_io = importlib.util.module_from_spec(spec)
sys.modules["style_io"] = style_io
spec.loader.exec_module(style_io)
# GResource tree creation
gresources = ElementTree.Element("gresources")
main_gresource = ElementTree.SubElement(
gresources,
"gresource",
attrib={"prefix": "/se/sjoerd/Graphs/"},
)
# Begin Other Section
for file in args.other:
name = Path(shutil.copy(file, args.dir)).name
element = ElementTree.SubElement(
main_gresource,
"file",
attrib={
"compressed": "True",
},
)
element.text = name
# End Other Section
# Begin style section
styles = {}
styles_gresource = ElementTree.SubElement(
gresources,
"gresource",
attrib={"prefix": "/se/sjoerd/Graphs/styles/"},
)
for style_path in args.styles:
style_file = shutil.copy(style_path, args.dir)
name = Path(style_file).name
style_element = ElementTree.SubElement(
styles_gresource,
"file",
attrib={
"compressed": "True",
},
)
style_element.text = name
params_file = Gio.File.new_for_path(style_file)
params, stylename = style_io.parse(params_file)
out_path = Path(args.dir, name.replace(".mplstyle", ".png"))
styles[stylename] = out_path
with open(out_path, "wb") as out_file:
style_io.create_preview(out_file, params, "png")
preview_element = ElementTree.SubElement(
main_gresource,
"file",
attrib={
"compressed": "True",
},
)
preview_element.text = out_path.name
def _to_array(file_path):
with open(file_path, "rb") as file:
return numpy.array(Image.open(file).convert("RGB"))
# Generate stitched system previews for Adwaita and Yaru
for sys_style in ("Adwaita", "Yaru"):
light_array = _to_array(styles[sys_style])
dark_array = _to_array(styles[sys_style + " Dark"])
height, width = light_array.shape[0:2]
stitched_array = numpy.concatenate(
(light_array[:, :width // 2], dark_array[:, width // 2:]),
axis=1,
)
stitched_image = Image.fromarray(stitched_array)
out_path = Path(args.dir + "/system-style-" + sys_style.lower() + ".png")
with open(out_path, "wb") as file:
stitched_image.save(file, "PNG")
preview_element = ElementTree.SubElement(
main_gresource,
"file",
attrib={
"compressed": "True",
},
)
preview_element.text = out_path.name
# End style section
# Begin ui section
ui_gresource = ElementTree.SubElement(
gresources,
"gresource",
attrib={"prefix": "/se/sjoerd/Graphs/ui/"},
)
help_overlay_path = None
for ui_file in args.ui:
path = Path(ui_file)
if path.name == "shortcuts.ui":
help_overlay_path = path
continue
ui_file_element = ElementTree.SubElement(
ui_gresource,
"file",
attrib={
"preprocess": "xml-stripblanks",
"alias": path.name,
},
)
ui_file_element.text = "ui/" + path.name
help_overlay_element = ElementTree.SubElement(
main_gresource,
"file",
attrib={
"preprocess": "xml-stripblanks",
"alias": "gtk/help-overlay.ui",
},
)
help_overlay_element.text = "ui/" + help_overlay_path.name
# End ui section
# Begin icon section
icon_gresource = ElementTree.SubElement(
gresources,
"gresource",
attrib={"prefix": "/se/sjoerd/Graphs/icons/scalable/actions/"},
)
for icon_file in args.icons:
path = Path(Path(shutil.copy(icon_file, args.dir)))
icon_file_element = ElementTree.SubElement(
icon_gresource,
"file",
attrib={
"preprocess": "xml-stripblanks",
},
)
icon_file_element.text = path.name
# End icon section
# Write
tree = ElementTree.ElementTree(gresources)
tree.write(args.out)
|