Last active
August 1, 2018 15:36
-
-
Save caiofcm/18311486d0fef92872fd38109ffd2648 to your computer and use it in GitHub Desktop.
Utility to run Pweave passing options for output file name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Utility to run Pweave passing options: | |
| `python gen_report_pweave.py --help` | |
| usage: gen_report_pweave.py [-h] [-d OUTPUTDIR] [-t] [-r] [-o] source prefix | |
| Run pypublish to generate a pdf | |
| positional arguments: | |
| source python filename | |
| prefix prefix for generated pdf file name | |
| optional arguments: | |
| -h, --help show this help message and exit | |
| -d OUTPUTDIR, --outputdir OUTPUTDIR | |
| output directory for generated pdf | |
| -t, --time include the date and time in the file name | |
| -r, --removefigdir remove the figures folder | |
| -o, --overwrite overwrite file if exists | |
| """ | |
| import argparse | |
| import os | |
| import shutil | |
| import sys | |
| import time | |
| from pweave import publish | |
| import pweave | |
| description = """ | |
| Run pypublish to generate a pdf | |
| """ | |
| def run_pypublish(py_file, prefix, outputdir, | |
| showTime, removefigdir, | |
| overwrite_if_file_exists): | |
| file_name = py_file | |
| if showTime: | |
| output = '{}-{}-{}'.format(prefix, | |
| time.strftime("%H-%M-%S"), | |
| time.strftime("%d-%m-%Y")) | |
| else: | |
| output = prefix | |
| output_pdf = output + '.pdf' | |
| output_tex = output + '.tex' | |
| output_destination = os.path.join(outputdir, output_pdf) | |
| output_destination = os.path.abspath(output_destination) | |
| # config.rcParams['figdir'] = 'pweave_tmp_figures' | |
| # pweave.rcParams.update({'figdir': 'pweave_tmp_figures'}) | |
| # print(pweave.rcParams) | |
| publish(file_name, doc_format='pdf', output=output_tex) | |
| # clean intermediary files | |
| print('Cleaning intermediary files...') | |
| os.remove(output + '.log') | |
| os.remove(output + '.aux') | |
| os.remove(output + '.out') | |
| os.remove(output + '.tex') | |
| if removefigdir: | |
| shutil.rmtree('./figures') | |
| if overwrite_if_file_exists: | |
| os.replace(output_pdf, output_destination) | |
| else: | |
| os.rename(output_pdf, output_destination) | |
| def main(): | |
| ap = argparse.ArgumentParser(description=description) | |
| ap.add_argument("source", help="python filename") | |
| ap.add_argument("prefix", help="prefix for generated pdf file name") | |
| ap.add_argument('-d', '--outputdir', | |
| default=".", | |
| help="output directory for generated pdf") | |
| ap.add_argument('-t', '--time', | |
| action="store_true", | |
| help="include the date and time in the file name") | |
| ap.add_argument('-r', '--removefigdir', | |
| action="store_true", | |
| help="remove the figures folder") | |
| ap.add_argument('-o', '--overwrite', | |
| action="store_true", | |
| help="overwrite file if exists") | |
| args = ap.parse_args() | |
| # Process | |
| run_pypublish(args.source, args.prefix, args.outputdir, | |
| args.time, args.removefigdir, args.overwrite) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment