def main(argv): # get dir of currently executing script: code_root = os.path.dirname(os.path.realpath(__file__)) # Cache log info in memory until log file is set up logs.initial_log_config() # poor man's subparser: argparse's subparser doesn't handle this # use case easily, so just dispatch on first argument if len(argv) == 1 or \ len(argv) == 2 and argv[1].lower() in ('-h', '--help'): # case where we print CLI help cli_obj = cli.MDTFTopLevelArgParser(code_root) cli_obj.print_help() return 0 # will actually exit from print_help elif argv[1].lower() == 'info': # case where we print command-line info on PODs from src import mdtf_info mdtf_info.InfoCLIHandler(code_root, argv[2:]) return 0 # will actually exit from print_help else: # case where we run the actual framework print(f"=== Starting {os.path.realpath(__file__)}\n") validate_base_environment() # not printing help or info, setup CLI normally cli_obj = cli.MDTFTopLevelArgParser(code_root, argv=argv) framework = cli_obj.dispatch() exit_code = framework.main() return exit_code
def setUp_config_singletons(config=None, paths=None, pods=None, unittest=True): cwd = os.path.dirname(os.path.realpath(__file__)) code_root = os.path.dirname(os.path.dirname(cwd)) cli_obj = cli.MDTFTopLevelArgParser( code_root, skip_defaults=True, argv=f"-f {os.path.join(cwd, 'dummy_config.json')}") cli_obj.config = vars(cli_obj.parse_args()) if config: cli_obj.config.update(config) PodDataTuple = collections.namedtuple('PodDataTuple', 'sorted_lists pod_data realm_data') dummy_pod_data = PodDataTuple(pod_data=pods, realm_data=dict(), sorted_lists={ 'pods': [], 'realms': [] }) _ = core.ConfigManager(cli_obj, dummy_pod_data, unittest=unittest) pm = core.PathManager(cli_obj, unittest=unittest) if paths: pm.update(paths) _ = core.VariableTranslator(code_root, unittest=unittest) _ = core.TempDirManager(None, unittest=unittest)
def test_parse_dummy_input(self): # get dir of currently executing script: cwd = os.path.dirname(os.path.realpath(__file__)) code_root = os.path.dirname(os.path.dirname(cwd)) cli_obj = cli.MDTFTopLevelArgParser( code_root, skip_defaults=True, argv=f"-f {os.path.join(cwd, 'dummy_config.json')}") config = vars(cli_obj.parse_args()) self.assertEqual(config["OBS_DATA_ROOT"], "/DUMMY/PATH/OBS_DATA_ROOT") self.assertEqual(config["MODEL_DATA_ROOT"], "/DUMMY/PATH/MODEL_DATA_ROOT") self.assertEqual(config["WORKING_DIR"], "/DUMMY/PATH/WORKING_DIR") self.assertEqual(config["OUTPUT_DIR"], "/DUMMY/PATH/OUTPUT_DIR")