"""Automate the creation of the python-dev Summaries from a reST body. To use, pass in the .txt file for the newest summary. A new summary will be created by pasting together a header (found in header.txt), the body (as passed in), and a footer (found at trailer.txt). Proper string substitution for needed values will be done upon both the header and footer. All text is expected to be reST-formatted. The passed-in filename must be in the format of ``yyyy-mm-dd_yyyy-mm-dd.txt`` with the first specified date the beginning of coverage of this summary and the last date the ending date, inclusive. """ import sys import os from datetime import date as dt_date from time import strptime from string import Template date_format = "%Y-%m-%d" def datesfromfilename(filename): """Return datetime.Date instances for start and end dates based on a filename""" if '.' in filename: filename = filename[:filename.index('.')] start_date_str = filename[:date_char_count] end_date_str = filename[date_char_count+1:] start_date = dt_date(*strptime(start_date_str, date_format)[:3]) end_date = dt_date(*strptime(end_date_str, date_format)[:3]) return start_date, end_date def lastsummarydates(directory='.'): """Figure out the beginning and end coverage dates based on the filename of .ht files, returning datetime.Date objects in a (start, end) tuple. If a text file exists with the same basic filename as the determined newest summary, then it will be ignored and the next newest summary dates will be used. This is to avoid when an error comes up (usually during .ht->.html translation) and the summary must be recreated and the last .ht version was accidentally not deleted. """ summary_files = [filename[:-3] for filename in os.listdir(directory) if os.path.isfile(filename) and filename.endswith(".ht") and len(filename) == 24] assert len(summary_files) > 0 summary_files.sort() last_summary_name = summary_files[-1] if os.path.exists(last_summary_name + ".txt"): last_summary_name = summary_files[-2] return datesfromfilename(last_summary_name) def templatemapping(start_date, end_date, count="XXX", quip="XXX", group="XXX"): """Return a dict containing string values needed for string substitution based on the start and end dates for this newest summary (passed in as datetime.Date instances). Items in returned dict are: + 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_name Name of the summary in HTML format + html_prev_summary The expected name of the last summary when save as a .html file + count textual represention of what the # of summaries written by the current author is, inclusive + quip pithy little sentence to accompany summary count + group description of group """ mapping = {"count": count, "quip":quip, "group":group} prev_start_date, prev_end_date = lastsummarydates() mapping['start_ISO_date'] = start_date.strftime("%Y-%m-%d") mapping['end_ISO_date'] = end_date.strftime("%Y-%m-%d") mapping['start_traditional_date'] = start_date.strftime("%B %d, %Y") mapping['end_traditional_date'] = end_date.strftime("%B %d, %Y") mapping['ht_name'] = "%s_%s.ht" % (mapping['start_ISO_date'], mapping['end_ISO_date']) mapping['html_name'] = mapping['ht_name'] + "ml" mapping['html_prev_summary'] = ("%s_%s.html" % (prev_start_date.strftime("%Y-%m-%d"), prev_end_date.strftime("%Y-%m-%d"))) return mapping def createsummary(FILE, mapping, header, body, footer): """Create a new summary by writing to FILE the header, body, and footer with the proper string substitutions based on 'mapping' for the heder and footer. If any instance of the string 'XXX' is found, a warning will be printed to stderr. """ for text, name in ((header, "header"), (body, "body"), (footer, "footer")): if 'XXX' in text: print>>sys.stderr, "'XXX' found in", name print>>FILE, Template(header).substitute(mapping) print>>FILE, "\n" print>>FILE, body print>>FILE, "\n\n" print>>FILE, Template(footer).substitute(mapping) if __name__ == '__main__': summary_body = sys.argv[1] mapping_args = {} # Must also count dashes in date character count date_char_count = 4 + 1 + 2 + 1 + 2 body_filename = sys.argv[1] assert body_filename.endswith(".txt") and len(body_filename) == 25 start_date, end_date = datesfromfilename(body_filename) mapping_args['start_date'] = start_date mapping_args['end_date'] = end_date if len(sys.argv) >= 3: mapping_args['count'] = sys.argv[2] if len(sys.argv) >= 4: mapping_args['quip'] = sys.argv[3] if len(sys.argv) >= 5: mapping_args['group'] = sys.argv[4] mapping = templatemapping(**mapping_args) header_str = open("header.txt", "rU").read() footer_str = open("trailer.txt", "rU").read() body_str = open(body_filename, "rU").read() HT_FILE = open(body_filename[:body_filename.index('.')] + ".ht", "wU") try: createsummary(HT_FILE, mapping, header_str, body_str, footer_str) finally: HT_FILE.close()