Source code for libvhc.html.collate_html_recaps
# Virus Health Check: a validation tool for HETDEX/VIRUS data
# Copyright (C) 2015, 2016 "The HETDEX collaboration"
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse as ap
import logging
import os
import os.path as op
import re
import shutil
import sys
import jinja2
from pyhetdex.tools.files.file_tools import scan_files
from .communication import HTML_RECAP
[docs]def read_arguments(argv=None):
"""
Parse command line
Parameters
----------
argv : list of strings, optional
command line arguments to parse
"""
parser = ap.ArgumentParser(description="""Collate multiple VHC HTML recap
files in one folder. Produce a single HTML file
with which to browse through them all.""",
formatter_class=ap.ArgumentDefaultsHelpFormatter)
parser.add_argument('dir', help="""Path of directory containing virus?????
folders (i.e. shots)""")
parser.add_argument('-o', '--outdir', help='output directory',
default='vhc_collates')
parser.add_argument('-m', '--match', help='match for the html files',
default=HTML_RECAP.format('*').format(recipe='*'))
parser.add_argument('--shot-id', help='''regex string used to extract the
shot id from the html file paths''',
default=r'.*/virus(\d+?)/.*')
parser.add_argument('-u', '--use-unknown', help='''If no shot id extracted
from the file name, use 99999 instead of skipping the
file''', action='store_true')
return parser.parse_args(args=argv)
[docs]def main(argv=None):
"""Main function.
Copies all of the html files in folders with virus[SHOTID] as the signiture
to a single folder and creates a HTML file to browse through them.
Parameters
----------
argv : list of strings, optional
command line arguments, except the caller name.
"""
args = read_arguments(argv=argv)
if not op.isdir(args.dir):
print("Input is not a directory!")
sys.exit(1)
# Check if the output directory exists, create if not
outdir = args.outdir
if op.exists(outdir):
if not op.isdir(outdir):
print("Output dir exists and is not a directory!")
sys.exit(1)
else:
os.makedirs(outdir)
# Look into all of the virus???? directories, copying all of the .html
# files into the outdir
outfiles = []
shot_pattern = re.compile(args.shot_id)
for html in scan_files(args.dir, matches=args.match):
shotid = shot_pattern.findall(html)
if shotid:
shotid = shotid[-1]
else:
logging.warning("Cound not extract the shot id from the file '%s'",
html)
if args.use_unknown:
shotid = '99999'
else:
continue
root, ext = op.splitext(html)
ofile = os.path.basename(root + '_' + shotid + ext)
i = 1
while ofile in outfiles:
root, ext = op.splitext(ofile)
ofile = root + '_{0:03d}'.format(i) + ext
i += 1
out = op.join(outdir, ofile)
outfiles.append(ofile)
try:
shutil.copy(html, out)
except IOError as e:
logging.error("Could not copy '{}' to '{}'".format(html, out))
if outfiles:
# Create the HTML file that links them all
curdir = op.dirname(op.abspath(__file__))
loader = jinja2.FileSystemLoader(op.join(curdir, "templates"))
env = jinja2.Environment(loader=loader)
template = env.get_or_select_template("recap_browser.html")
# Use jinja to fill out the 'pages' and 'night' fields in the template
# HTML
html = template.render(pages=outfiles, night=args.dir)
with open(op.join(outdir, 'recap_browser.html'), 'w') as fp:
fp.write(html)
logging.info("All done..")
else:
logging.warning('No matching html file found')
if __name__ == "__main__": # pragma: no cover
main()