|
1 | 1 | import os |
2 | 2 | import jinja2 |
3 | 3 | from argparse import ArgumentParser, RawTextHelpFormatter |
| 4 | +from glob import glob |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def build_parser(): |
7 | 8 | parser = ArgumentParser( |
8 | 9 | description='accelBIDSTransform BIDS args', |
9 | 10 | formatter_class=RawTextHelpFormatter |
10 | 11 | ) |
11 | | - parser.add_argument('log_dir', help='directory where log ' |
| 12 | + parser.add_argument('log_dir', help='(abs path) directory where log ' |
12 | 13 | 'files are mounted') |
13 | 14 | parser.add_argument('output_file', help='location of the html output') |
14 | | - parser.add_argument('template_file', help='location of the html template') |
| 15 | + parser.add_argument('--template-file', help='location of the html template', |
| 16 | + required=False, default='templates/temp.tpl') |
15 | 17 |
|
16 | 18 | return parser |
17 | 19 |
|
18 | 20 |
|
19 | | -class Accel(): |
20 | | - def __init__(self, filename, status, path, date, time): |
21 | | - self.filename = filename |
22 | | - self.status = status |
23 | | - if status == 'ERROR': |
24 | | - self.css = 'redtext' |
| 21 | +def main(): |
| 22 | + class Accel(): |
| 23 | + def __init__(self, filename, status, path, date, time): |
| 24 | + self.filename = filename |
| 25 | + self.status = status |
| 26 | + if status == 'ERROR': |
| 27 | + self.css = 'redtext' |
| 28 | + else: |
| 29 | + self.css = 'greentext' |
| 30 | + self.path = path |
| 31 | + self.date = date |
| 32 | + self.time = time |
| 33 | + |
| 34 | + opts = build_parser().parse_args() |
| 35 | + # converting relative paths to absolute |
| 36 | + template_file = os.path.abspath(opts.template_file) |
| 37 | + log_dir = os.path.abspath(opts.log_dir) |
| 38 | + os.chdir(os.path.dirname(log_dir)) |
| 39 | + log_dir_base = os.path.basename(log_dir) |
| 40 | + |
| 41 | + # glob returns whatever path type you pass into it |
| 42 | + # in this case it returns relative paths |
| 43 | + filenames = glob(os.path.join(log_dir_base, '*')) |
| 44 | + |
| 45 | + accel_files = [] |
| 46 | + |
| 47 | + for fil in filenames: |
| 48 | + contents = open(fil, 'r').read() |
| 49 | + date = contents[0:10] |
| 50 | + time = contents[11:19] |
| 51 | + if 'ERROR' in contents: |
| 52 | + accel_files.append(Accel(os.path.basename(fil), 'ERROR', 'file:' + |
| 53 | + fil, date, time)) |
25 | 54 | else: |
26 | | - self.css = 'greentext' |
27 | | - self.path = path |
28 | | - self.date = date |
29 | | - self.time = time |
30 | | - |
31 | | - |
32 | | -opts = build_parser().parse_args() |
33 | | -(_, _, filenames) = next(os.walk(opts.log_dir)) |
34 | | - |
35 | | -accel_files = [] |
36 | | - |
37 | | -for fil in filenames: |
38 | | - contents = open(opts.log_dir + fil, 'r').read() |
39 | | - date = contents[0:10] |
40 | | - time = contents[11:19] |
41 | | - if 'ERROR' in contents: |
42 | | - accel_files.append(Accel(fil, 'ERROR', 'file:' + |
43 | | - os.path.join(opts.log_dir, fil), date, time)) |
44 | | - else: |
45 | | - accel_files.append(Accel(fil, 'SUCCESS', 'file:' + |
46 | | - os.path.join(opts.log_dir, fil), date, time)) |
47 | | - |
48 | | -env = jinja2.Environment( |
49 | | - loader=jinja2.FileSystemLoader(searchpath=os.path.dirname(opts.template_file)) |
50 | | -) |
51 | | -report_tpl = env.get_template(os.path.basename(opts.template_file)) |
52 | | -report_render = report_tpl.render(accel_files=accel_files) |
53 | | - |
54 | | -with open(opts.output_file, 'w+') as file: |
55 | | - file.write(report_render) |
| 55 | + accel_files.append(Accel(os.path.basename(fil), 'SUCCESS', 'file:' + |
| 56 | + fil, date, time)) |
| 57 | + |
| 58 | + env = jinja2.Environment( |
| 59 | + loader=jinja2.FileSystemLoader(searchpath=os.path.dirname(template_file)) |
| 60 | + ) |
| 61 | + report_tpl = env.get_template(os.path.basename(template_file)) |
| 62 | + report_render = report_tpl.render(accel_files=accel_files) |
| 63 | + |
| 64 | + with open(os.path.join(os.path.dirname(log_dir), opts.output_file), 'w+') as file: |
| 65 | + file.write(report_render) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + main() |
0 commit comments