Skip to content

Commit d01aa99

Browse files
authored
Merge pull request #7 from HBClab/dev
add main func; add default template path
2 parents 2e5cbdc + d143ee4 commit d01aa99

File tree

2 files changed

+53
-39
lines changed

2 files changed

+53
-39
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ COPY --chown=coder . /home/coder/projects/
2424
RUN conda env create -f environment.yml
2525
RUN bash -c 'conda init && . /home/coder/.bashrc'
2626

27-
ENTRYPOINT ["/opt/miniconda-latest/envs/log/bin/python", "./logToHtml.py"]
27+
ENTRYPOINT ["/opt/miniconda-latest/envs/log/bin/python", "/home/coder/projects/logToHtml.py"]

logToHtml.py

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,69 @@
11
import os
22
import jinja2
33
from argparse import ArgumentParser, RawTextHelpFormatter
4+
from glob import glob
45

56

67
def build_parser():
78
parser = ArgumentParser(
89
description='accelBIDSTransform BIDS args',
910
formatter_class=RawTextHelpFormatter
1011
)
11-
parser.add_argument('log_dir', help='directory where log '
12+
parser.add_argument('log_dir', help='(abs path) directory where log '
1213
'files are mounted')
1314
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')
1517

1618
return parser
1719

1820

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))
2554
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

Comments
 (0)