-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterm2base24.py
104 lines (87 loc) · 2.66 KB
/
iterm2base24.py
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
#!/usr/bin/env python3
"""Convert .itermcolors to base24 scheme
"""
import os
import sys
import argparse
import defusedxml.ElementTree as ET
import yaml
from metprint import (
LogType,
Logger
)
import base24tools
def rgb_to_hex(rgb):
''' Converts rgb to hex '''
return '%02x%02x%02x' % rgb
def iterm2hex(root):
"""Get hex codes from iterm xml
Args:
root (xmlroot): xmlroot
Returns:
dict: iterm keys to hex
"""
keys = root.findall("./dict/key")
dicts = root.findall("./dict/dict")
iterm = {}
for i, _key in enumerate(keys):
keyName = keys[i].text
r = g = b = None
for index, item in enumerate(dicts[i]):
if "Red Component" in item.text:
r = int(float(dicts[i][index+1].text) * 255.0)
if "Green Component" in item.text:
g = int(float(dicts[i][index+1].text) * 255.0)
if "Blue Component" in item.text:
b = int(float(dicts[i][index+1].text) * 255.0)
iterm[keyName] = rgb_to_hex((r, g, b))
return iterm
def genBase24(filename, iterm):
"""Generate the base24 json object
Args:
filename (str): filename from args
iterm (dict): iterm keys to hex
Returns:
dict: base24 dict to write to scheme file
"""
base24 = {"author": "Iterm2B24", "scheme": filename.split(".")[0]}
base24lookup = {
"base00": "Background Color",
"base01": "Ansi 0 Color", #Black
"base02": "Ansi 8 Color", #Bright black
"base06": "Ansi 7 Color", #White
"base07": "Ansi 15 Color", #Bright white
"base08": "Ansi 1 Color", #Red
"base09": "Ansi 3 Color", #Yellow
"base0A": "Ansi 12 Color", #Bright yellow (variant 2)
"base0B": "Ansi 2 Color", #Green
"base0C": "Ansi 6 Color", #Cyan
"base0D": "Ansi 4 Color", #Blue
"base0E": "Ansi 5 Color", #Purple
"base12": "Ansi 9 Color", #Bright red
"base13": "Ansi 11 Color", #Bright yellow
"base14": "Ansi 10 Color", #Bright green
"base15": "Ansi 14 Color", #Bright cyan
"base16": "Ansi 12 Color", #Bright blue
"base17": "Ansi 13 Color", #Bright purple
}
return base24tools.process(base24, base24lookup, iterm, 2, 6)
def main():
''' Main entry point for cli '''
parser = argparse.ArgumentParser(
description="Convert .itermcolors to base24 scheme")
parser.add_argument("file", action="store",
help="file.itermschemes")
args = parser.parse_args()
# Check for and report level8 errors
if not os.path.isfile(args.file):
Logger().logPrint(args.file + " is not a valid file", LogType.ERROR)
sys.exit(1)
filename = args.file
tree = ET.parse(filename)
base24 = genBase24(filename, iterm2hex(tree.getroot()))
with open(base24["scheme"]+".yaml", "w") as outfile:
Logger().logPrint("writing \"" + base24["scheme"] + "\" to file", LogType.SUCCESS)
yaml.dump(base24, outfile)
if __name__ == '__main__':
main()