Skip to content

Instantly share code, notes, and snippets.

@Kethen
Last active February 2, 2022 21:29
Show Gist options
  • Save Kethen/9330128586210276813b9ea03861bf71 to your computer and use it in GitHub Desktop.
Save Kethen/9330128586210276813b9ea03861bf71 to your computer and use it in GitHub Desktop.
python script for merging themes.xml and colors.xml
from xml.etree.ElementTree import parse, indent
import re
import sys
colors = parse("material-theme/values/colors.xml")
day = parse("material-theme/values/themes.xml")
night = parse("material-theme/values-night/themes.xml")
def get_color(tree, colorString):
colorName = re.compile("@color/(.+)").match(colorString).group(1)
for color in tree.getroot().findall("color"):
if color.get("name") == colorName:
return color.text
def apply_patch(theme, colors):
style = theme.getroot().find("style")
style.set("name", sys.argv[1])
del style.attrib["parent"]
for item in style:
item.text = get_color(colors, item.text)
apply_patch(day, colors)
apply_patch(night, colors)
indent(day)
day.write("day.xml")
indent(night)
night.write("night.xml")
@Kethen
Copy link
Author

Kethen commented Feb 2, 2022

python3 convert.py <patch name>, with material-theme in the current directory
generates day.xml and night.xml into current directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment