"""Automate creation of standard header for Summaries. Expected usage is to redirect output of this script to ``cat - `` to then be redirected to a file: ``python makeheader.py summary | cat - summary > summary``. Uses basic %()s substitution to creation which is printed to stdout. See docstring for createheader for supported %-escapes. XXX: To Do: * Be able to specify header text file to use * Specify final output file to skip need to use 'cat' """ import os import datetime # Simple functions for creating filenames with .ht and .html extensions htname = lambda start, end: start.isoformat() + '_' + end.isoformat() + ".ht" htmlname = lambda start, end: htname(start, end) + "ml" def createheader(start_date, end_date, last_start_date, last_end_date, header_text): """Replace %-escaped values in header_text with proper information as generated using the start and end dates of coverage for this summary and the start and end dates of the last summary (all passed in as datetime.date objects). %-escaped values: + start_ISO_date Output start_date formatted as yyy-mm-dd + end_ISO_date Same as start_ISO_date except end_date is used + start_traditional_date start_date formatted as "%B %d, %Y" + end_traditional_date Same as start_traditional_date except with end_date + ht_name The expected name of the summary when saved as an .ht file + html_prev_summary The expected name of the last summary when save as a .html file """ subst_dict = dict( start_ISO_date = start_date.isoformat(), end_ISO_date = end_date.isoformat(), start_traditional_date = start_date.strftime("%B %d, %Y"), end_traditional_date = end_date.strftime("%B %d, %Y"), ht_name = htname(start_date, end_date), html_prev_summary = htmlname(last_start_date, last_end_date) ) if "XXX" in header_text: print>>sys.stderr, ("NOTE: 'XXX' markers found in header; " + "make sure to replace them") return header_text % subst_dict def lastsummary(directory, this_summary_start, file_ending=".ht"): """Figure out the start and end dates of the last summary as found in 'directory', excluding the newest one if exclude_newest is true.""" summary_files = os.listdir(os.getcwd()) # In case directory contains various versions of the summaries (.txt, # .html, etc.), filter on only a single file extension known to be there summary_files = [filename for filename in summary_files if (filename.endswith(file_ending) and len(filename) == (21 + len(file_ending))) and not filename.startswith(this_summary_start) ] # Can do a standard sort since filenames all in ISO format summary_files.sort() last_summary = summary_files[-1] start_date_text = last_summary[:10] # "yyyy-mm-dd" = 10 characters end_date_text = last_summary[11:21] return (convertISO(start_date_text), convertISO(end_date_text)) def convertISO(text): """Convert 'text', which is an ISO date string, into a datetime.date object.""" year = int(text[:4]) month = int(text[5:7]) day = int(text[8:]) return datetime.date(year, month, day) if __name__ == "__main__": import sys try: filename = sys.argv[1] except IndexError: print>>sys.stderr, "ERROR: You must specify the summary to use." print>>sys.stderr, ("Usage: python makeheader.py summary.txt | " "cat - summary.txt > summary.ht") sys.exit(1) start_date_text, end_date_text = os.path.splitext(filename)[0].split('_') start_date = convertISO(start_date_text) end_date = convertISO(end_date_text) last_start_date, last_end_date = lastsummary(os.getcwd(), start_date_text) # XXX: can make a command-line argument header_FILE = file("header.txt") try: header_text = header_FILE.read() finally: header_FILE.close() # Printed so that you can do ``cat - `` print createheader(start_date, end_date, last_start_date, last_end_date, header_text)