#!/home/ida/python-venv-3.12.6/bin/python3
__doc__="""
make_program_info.py
- Create 'program_YYYYSNNN.html' & 'sidebar.html' files from
'program_YYYSNNN.txt' files for HTML page.
It is to be run in the current directory where program information files are.
-v - verbose
-h --help - shows this help
Example:
% cd /idas1/2021A/program_info && make_program_info.py
"""
import getopt
import os
import re
import sys
def usage():
print (__doc__)
def find_program_txt_files( dir, sort = True, only_base = True,
prog_re = r'program_(19|20)[0-9]{2}[AB][0-9]{3}.txt'
):
"""
Returns a list of base names of program ID directories.
Arguments:
dir : directory to search (non-recursive).
sort : flag to return a sorted list.
only_base: flag to return only base names, not complete paths.
prog_re : regex to match against directory base name.
"""
out = list()
for basename in os.listdir( dir ):
path = os.path.join( dir, basename )
if re.match( prog_re, basename ):
out.append( path if not only_base else basename )
if sort:
out.sort()
return out
def write_program_html( program_html, program_txt ):
"""
Creates the html file for program_YYYYSNNN.html
Arguments:
program_html: write the html code to this filename.
program_txt : name of the text file, ie: program_2016A001.txt
"""
html = """
""".format( program_txt )
with open( program_html, 'w') as fo:
fo.write( html )
return
def write_sidebar_html( filename, semester, program_list ):
"""
Creates the sidebar.html file for program_YYYYSNNN.html
Arguments:
filename: name of output file
semester: semester string, ie "2016B"
program_list: all the program_YYYYSNNN.txt filenames as a list
"""
with open( filename, 'w') as fo:
# beginning of file
html = \
"""
{} Programs
""".format( semester )
fo.write( html )
# from the program_list:
for program_txt in program_list:
# program_YYYYSNNN.html
program_html = os.path.splitext( program_txt )[0] + '.html'
# YYYSNNN
program = re.search( 'program_(.+?).txt', program_txt).group(1)
fo.write( "