Skip to content

Instantly share code, notes, and snippets.

@bcicen
Created August 21, 2016 08:32
Show Gist options
  • Save bcicen/e77fed9f893b7ee6183234052a74ed9c to your computer and use it in GitHub Desktop.
Save bcicen/e77fed9f893b7ee6183234052a74ed9c to your computer and use it in GitHub Desktop.
from colour import Color
import xml.etree.ElementTree
def import_itermcolors(filename):
ret = {}
for k,v in import_itermcolors_raw(filename).items():
r = v['Red Component']
g = v['Green Component']
b = v['Blue Component']
ret[k] = Color(rgb=(r,g,b))
return ret
def import_itermcolors_raw(filename):
return { k:read_color_dict(e) for k,e in read_file(filename).items() }
def list_to_dict(kvlist):
"""
Given a list with a sequential key then value format,
return a dict pairing them
"""
kvdict = {}
while kvlist:
k,v = kvlist[0:2]
kvdict[k] = v
del kvlist[0:2]
return kvdict
def try_float(s):
try:
return float(s)
except ValueError:
return s
def read_file(filename):
r = xml.etree.ElementTree.parse(filename).getroot()
main = r.getchildren()[0]
elements = []
keys = [ k.text for k in main.findall('key') ]
dicts = main.findall('dict')
return dict(zip(keys, dicts))
def read_color_dict(xmldict):
config = {}
kvlist = [ k.text for k in xmldict.iter() if k.tag != 'dict' ]
return { k:try_float(v) for k,v in list_to_dict(kvlist).items() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment