-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
323 lines (255 loc) · 11.1 KB
/
main.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import logging
import argparse
import json
from os.path import dirname, isabs, splitext, exists, join
from os import makedirs
import sys
from terminal2html import parse as html_parse, HopTarget
from asciinema2html import parse as asciinema_parse, copy_asciinema_files
from twebber import parse as parse_hops
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
LOG = logging.getLogger()
class TodoArgs:
def __init__(self, args):
self.infile = args.infile
self.format = 'terminal'
self.outfile = args.outfile
self.palette = args.palette
self.review_mode = args.review_mode
self.title = None
self.chapters = {}
self.filter = []
self.hopto = None
class Index:
HTML_INTRO = """
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta charset="utf-8"/>
<title>%(title)s</title>
<style type="text/css">
/* *** Text styling *** */
h1 { color: #D1C3CB; }
h2 { color: #e0e0c0; }
section { color: #e0e0c0; font-family: sans-serif; }
h2 > a { color: #e0e0c0; text-decoration: none; }
h2 > a:hover { color: #FFFFEE; text-decoration: underline; }
h2 > a:visited { color: #BEBE90; text-decoration: none; }
section > a { color: #e0e0c0; text-decoration: none; }
section > a:hover { color: #FFFFEE; text-decoration: underline; }
section > a:visited { color: #BEBE90; text-decoration: none; }
.f9 { color: %(cf9)s; }
.b9 { background-color: %(cb9)s; }
</style>
<style type="text/css">
/* *** Layout *** */
h1 { text-align: center; }
h2 { padding-left: 1em; }
section { padding-left: 4em; }
</style>
"""
BODY_INTRO = """
</head>
<body class="f9 b9">
<h1>%(title)s</h1>
"""
HTML_OUTRO = """
</body>
</html>
"""
def __init__(self, title=None, dark_bg=True):
self.dark_bg = dark_bg
self.title = title
self.files = {}
sdict = {
'title' : self.title,
'cf9' : "#f8f8f2",
'cb9' : "#21222c"
}
self.html_intro = (self.HTML_INTRO + self.BODY_INTRO) % sdict
self.html_body_string = ""
self.html_outro = self.HTML_OUTRO
def add_file(self, outfile, title=None):
if outfile in self.files:
LOG.error("The file %s already exists in the index, cannot add again.", outfile)
return
if not title:
title = splitext(outfile)
self.files[outfile] = {'title' : title}
def add_chapters(self, outfile, chapters):
if not outfile in self.files:
self.add_file(outfile)
self.files[outfile]['chapters'] = chapters
def build_body(self):
body_string = self.html_body_string
for filename in self.files:
file = self.files[filename]
body_string += '\n <h2><a href="' + filename + '">' + file['title'] + '</a></h2>\n'
if 'chapters' in file:
chapters = file['chapters']
for id in chapters:
if id:
body_string += ' <section><a href="' + filename + '#c' + id + '">' + chapters[id] + '</a></section>\n'
return body_string
def get_html_page(self):
return self.html_intro + self.build_body() + self.html_outro
def parse_to_html(args, logfile, destfile):
if args.format == 'asciinema':
asciinema_parse(logfile, destfile, palette=args.palette, title=args.title, chapters=args.chapters, cmd_filter=args.filter, hopto=args.hopto, review=args.review_mode)
if args.outfile:
copy_asciinema_files(dirname(args.outfile))
else:
html_parse(logfile, destfile, palette=args.palette, title=args.title, chapters=args.chapters, cmd_filter=args.filter, hopto=args.hopto, review=args.review_mode)
def parse_file(args):
with open(args.infile, 'rb') as logfile:
LOG.info("Parsing file %s", args.infile)
if args.outfile:
if not exists(dirname(args.outfile)):
makedirs(dirname(args.outfile))
with open(args.outfile, encoding="utf-8", mode='w') as destfile:
parse_to_html(args, logfile, destfile)
else:
parse_to_html(args, logfile, None)
def parse_file_hops(fromfile, tofile):
with open(fromfile, 'rb') as leftfile, open(tofile, 'rb') as rightfile:
LOG.info("Parsing file hops from %s to %s", fromfile, tofile)
return parse_hops(leftfile, rightfile)
# def join(path, file):
# """ Redefined join since even under windows we work in a Linux shell """
# return path + '/' + file
def files_by_id(file_list, dir=''):
filedict = {}
for file in file_list:
if 'id' in file and file['id']:
if dir:
filedict[file['id']] = file[dir]
else:
filedict[file['id']] = (file['in'], file['out'])
return filedict
def process_file_list(args, file_list_file):
with open(file_list_file, 'r', encoding="utf-8") as file_list:
data = json.load(file_list)
base_dir_in = dirname(file_list_file)
if 'base_dir_in' in data and data['base_dir_in']:
dir = data['base_dir_in']
if isabs(dir):
base_dir_in = dir
else:
base_dir_in = join(base_dir_in, dir)
base_dir_out = dirname(file_list_file)
if 'base_dir_out' in data and data['base_dir_out']:
dir = data['base_dir_out']
if isabs(dir):
base_dir_out = dir
else:
base_dir_out = join(base_dir_out, dir)
if 'title' in data and data['title']:
index_title = data['title']
else:
index_title = "Git Training"
index = Index(index_title)
if data['files']:
files = files_by_id(data['files'])
for file in data['files']:
in_file = join(base_dir_in, file['in'])
if 'out' in file and file['out']:
out_file_name = file['out']
else:
base, ext = splitext(file['in'])
out_file_name = base + '.html'
out_file = join(base_dir_out, out_file_name)
if 'format' in file and file['format']:
log_format = file['format']
if log_format != 'terminal' and log_format != 'asciinema':
print("Unsupported input file format '%s' for file '%s'. Exiting.".format(log_format, file['in']), file=sys.stderr)
return
else:
log_format = 'terminal'
if 'title' in file and file['title']:
index.add_file(out_file_name, file['title'])
else:
index.add_file(out_file_name)
my_args = TodoArgs(args)
my_args.infile = in_file
my_args.outfile = out_file
my_args.format = log_format
if 'palette' in file and file['palette']:
my_args.palette = file['palette']
if 'title' in file and file['title']:
my_args.title = file['title']
if 'review' in file:
my_args.review_mode = file['review']
if 'id' in file and file['id']:
chapters = file['id'] + '-chapters'
if chapters in data:
index.add_chapters(out_file_name, data[chapters])
my_args.chapters = data[chapters]
filter = file['id'] + '-suppress'
if filter in data:
my_args.filter = data[filter]
hopto = file['id'] + '-hopto'
if hopto in data:
my_args.hopto = data[hopto]
ofid = data[hopto]['id']
tfilterid = ofid + '-suppress'
if tfilterid in data:
tfilter = data[tfilterid]
else:
tfilter = tuple()
my_args.hopto['target'] = HopTarget(ofid, files[ofid][1], tfilter)
print(len(tfilter))
if my_args.review_mode and 'ahopto' in file and file['ahopto']:
to_file = join(base_dir_in, files[file['ahopto']][0])
ahops = parse_file_hops(in_file, to_file)
if not my_args.hopto:
my_args.hopto = {}
my_args.hopto['rev_hops'] = ahops.hops_from_left
print("Process")
print(f" {my_args.infile}")
print(f" -> {my_args.outfile}")
print(f" as {my_args.title}")
print(f" in {my_args.palette}")
sys.stdout.flush()
parse_file(my_args)
print("Generating index file")
generate_index(base_dir_out, index)
def generate_index(base_dir_out, index : Index):
index_file = join(base_dir_out, "index.html")
if not exists(dirname(index_file)):
makedirs(dirname(index_file))
with open(index_file, encoding="utf-8", mode='w') as indexfile:
indexfile.write(index.get_html_page())
def main():
"""
main.py [<options>] <infile> [<outfile>]
<infile> logfile to convert
<outfile> HTML file to write to. Default is standard out.
"""
argparser = argparse.ArgumentParser(description="Convert a terminal log file into processed output, e.g. HTML")
argparser.add_argument('infile', help="terminal log input file")
argparser.add_argument('outfile', nargs='?', help="HTML file to write to. Default is stdout")
argparser.add_argument('--MyDracula', '--MyDarcula', '--local', dest='palette', action='store_const', const='MyDracula',
help="Use color palette MyDracula (default)")
argparser.add_argument('--Dracula', '--Darcula', '--remote', dest='palette', action='store_const', const='Dracula',
help="Use color palette Dracula")
argparser.add_argument('--TangoDark', dest='palette', action='store_const', const='TangoDark',
help="Use color palette Tango Dark")
argparser.add_argument('--list', '-l', action='store_true', dest='filelist',
help="The input file is a JSON todo list with files to convert and their options")
argparser.add_argument('--review', '-w', action='store_true', dest='review_mode',
help="In review mode some hidden elements are shown in the HTML")
args = argparser.parse_args()
if args.filelist:
process_file_list(args, args.infile)
else:
args.title = ''
parse_file(TodoArgs(args))
if __name__ == '__main__':
LOG_FORMAT = "%(levelname)s :%(module)s - %(message)s"
logging.basicConfig(filename="parser.log",
level=logging.INFO,
format=LOG_FORMAT,
filemode='w')
main()