-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjcffile.py
112 lines (95 loc) · 3.65 KB
/
jcffile.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
105
106
107
108
109
110
111
112
class JCFFile:
def __init__(self):
self.sections = []
def load(self, filename):
with open(filename, 'r') as f:
sections = [JCFSection(None)]
lastSectionData = []
for line in f:
d = line.strip()
if d.startswith('.colors'):
colors = d[len('.colors'):].strip()
sect = JCFSection(colors)
sections[-1].content = ''.join(lastSectionData)
sections.append(sect)
sect.colorline = line
lastSectionData = []
else:
lastSectionData.append(line)
sections[-1].content = ''.join(lastSectionData)
self.sections = sections
return self
def save(self, filename):
with open(filename, 'w') as f:
for sect in self.sections:
f.write(sect.colorline)
f.write(sect.content)
if not sect.content.endswith("\n\n"):
f.write("\n")
if not sect.content.endswith("\n"):
f.write("\n")
def getSection(self, colors):
return next((s for s in self.sections if s.colors == colors), None)
def addSection(self, section):
self.sections.append(section)
class JCFSection:
def __init__(self, colors):
self.colors = colors
self.colorline = ""
self.content = ""
def __repr__(self):
return "<JCF Section %s>" % repr(self.colors)
def parse(self):
result = {}
for line in self.content.split('\n'):
cmt = line.find('#')
if cmt >= 0:
line = line[:cmt]
tokens = line.split()
if len(tokens) == 0: continue
if tokens[0].startswith('.'): continue
spec = JCFSpec(key=tokens[0])
if spec.key == '-term':
spec.key += ' ' + tokens[1]
remaining = tokens[2:]
else:
remaining = tokens[1:]
for token in remaining:
# This assumes:
# - No space between colors and '/'
# - fg_* and bg_* are not used.
# These assumptions are wrong for general purpose,
# but good enough for our use cases here.
if '/' in token:
spec.fg, spec.bg = token.split('/')
if not spec.fg: spec.fg = None
if not spec.bg: spec.bg = None
elif token.lower() in ('italic', 'underline', 'bold', 'dim', 'blink', 'reverse'):
spec.attrs.append(token.lower())
elif token.startswith('+'):
spec.refs.append(token[1:])
else:
spec.fg = token
result[spec.key] = spec
return result
class JCFSpec:
def __init__(self, key=None, fg=None, bg=None, attrs=None, refs=None):
self.key = key
self.fg = fg
self.bg = bg
self.attrs = attrs or []
self.refs = refs or []
def __str__(self):
result = self.key
if self.refs:
result += " ".join("+%s" % r for r in self.refs)
else:
if self.bg:
result += " %s/%s" % (self.fg or '', self.bg or '')
elif self.fg:
result += " " + self.fg
if self.attrs:
result += ' ' + ' '.join(self.attrs)
return result
def __repr__(self):
return "<JCFSpec: %s>" % str(self)