|
| 1 | +""" |
| 2 | +This file contains the command-line hooks for the dashboard. It is in a separate file so that we don't |
| 3 | +import Bokeh unless we need it. This greatly speeds up the command line. |
| 4 | +""" |
| 5 | + |
| 6 | +import argparse |
| 7 | +from pathlib import Path |
| 8 | +import shutil |
| 9 | +import zipfile |
| 10 | + |
| 11 | + |
| 12 | +def _dashboard_setup_parser(parser): |
| 13 | + """ |
| 14 | + Set up the aviary subparser for the 'aviary dashboard' command. |
| 15 | +
|
| 16 | + Parameters |
| 17 | + ---------- |
| 18 | + parser : argparse subparser |
| 19 | + The parser we're adding options to. |
| 20 | + """ |
| 21 | + parser.add_argument( |
| 22 | + 'script_name', |
| 23 | + type=str, |
| 24 | + nargs='*', |
| 25 | + help='Name of aviary script that was run (not including .py).', |
| 26 | + ) |
| 27 | + parser.add_argument( |
| 28 | + '--port', |
| 29 | + dest='port', |
| 30 | + type=int, |
| 31 | + default=0, |
| 32 | + help='dashboard server port ID (default is 0, which indicates get any free port)', |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + '-b', |
| 36 | + '--background', |
| 37 | + action='store_true', |
| 38 | + dest='run_in_background', |
| 39 | + help="Run the server in the background (don't automatically open the browser)", |
| 40 | + ) |
| 41 | + |
| 42 | + # For future use |
| 43 | + parser.add_argument( |
| 44 | + '-d', |
| 45 | + '--debug', |
| 46 | + action='store_true', |
| 47 | + dest='debug_output', |
| 48 | + help='show debugging output', |
| 49 | + ) |
| 50 | + |
| 51 | + parser.add_argument( |
| 52 | + '--save', |
| 53 | + nargs='?', |
| 54 | + const=True, |
| 55 | + default=False, |
| 56 | + help='Name of zip file in which dashboard files are saved. If no argument given, use the script name to name the zip file', |
| 57 | + ) |
| 58 | + |
| 59 | + parser.add_argument( |
| 60 | + '--force', |
| 61 | + action='store_true', |
| 62 | + help='When displaying data from a shared zip file, if the directory in the reports directory exists, overrite if this is True', |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def _dashboard_cmd(options, user_args): |
| 67 | + """ |
| 68 | + Run the dashboard command. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + options : argparse Namespace |
| 73 | + Command line options. |
| 74 | + user_args : list of str |
| 75 | + Args to be passed to the user script. |
| 76 | + """ |
| 77 | + if options.save and not options.script_name: |
| 78 | + if options.save is not True: |
| 79 | + options.script_name = options.save |
| 80 | + options.save = True |
| 81 | + |
| 82 | + if not options.script_name: |
| 83 | + raise argparse.ArgumentError('script_name argument missing') |
| 84 | + |
| 85 | + if isinstance(options.script_name, list): |
| 86 | + options.script_name = options.script_name[0] |
| 87 | + |
| 88 | + from aviary.visualization.dashboard import dashboard |
| 89 | + |
| 90 | + # check to see if options.script_name is a zip file |
| 91 | + # if yes, then unzip into reports directory and run dashboard on it |
| 92 | + if zipfile.is_zipfile(options.script_name): |
| 93 | + report_dir_name = Path(options.script_name).stem |
| 94 | + report_dir_path = Path(f'{report_dir_name}_out') |
| 95 | + # need to check to see if that directory already exists |
| 96 | + if not options.force and report_dir_path.is_dir(): |
| 97 | + raise RuntimeError( |
| 98 | + f'The reports directory {report_dir_path} already exists. If you wish ' |
| 99 | + 'to overwrite the existing directory, use the --force option' |
| 100 | + ) |
| 101 | + if ( |
| 102 | + report_dir_path.is_dir() |
| 103 | + ): # need to delete it. The unpacking will just add to what is there, not do a clean unpack |
| 104 | + shutil.rmtree(report_dir_path) |
| 105 | + |
| 106 | + shutil.unpack_archive(options.script_name, report_dir_path) |
| 107 | + dashboard( |
| 108 | + report_dir_name, |
| 109 | + # options.problem_recorder, |
| 110 | + # options.driver_recorder, |
| 111 | + options.port, |
| 112 | + options.run_in_background, |
| 113 | + ) |
| 114 | + return |
| 115 | + |
| 116 | + # Save the dashboard files to a zip file that can be shared with others |
| 117 | + if options.save is not False: |
| 118 | + if options.save is True: |
| 119 | + save_filename_stem = options.script_name |
| 120 | + else: |
| 121 | + save_filename_stem = Path(options.save).stem |
| 122 | + print(f'Saving to {save_filename_stem}.zip') |
| 123 | + shutil.make_archive(save_filename_stem, 'zip', f'{options.script_name}_out') |
| 124 | + return |
| 125 | + |
| 126 | + dashboard( |
| 127 | + options.script_name, |
| 128 | + # options.problem_recorder, |
| 129 | + # options.driver_recorder, |
| 130 | + options.port, |
| 131 | + options.run_in_background, |
| 132 | + ) |
0 commit comments