def load_data_context_with_error_handling( directory: str, from_cli_upgrade_command: bool = False ) -> DataContext: """Return a DataContext with good error handling and exit codes.""" try: context: DataContext = DataContext(context_root_dir=directory) ge_config_version: int = context.get_config().config_version if ( from_cli_upgrade_command and int(ge_config_version) < CURRENT_GE_CONFIG_VERSION ): directory = directory or context.root_directory ( increment_version, exception_occurred, ) = upgrade_project_one_version_increment( context_root_dir=directory, ge_config_version=ge_config_version, continuation_message=EXIT_UPGRADE_CONTINUATION_MESSAGE, from_cli_upgrade_command=from_cli_upgrade_command, ) if not exception_occurred and increment_version: context = DataContext(context_root_dir=directory) return context except ge_exceptions.UnsupportedConfigVersionError as err: directory = directory or DataContext.find_context_root_dir() ge_config_version = DataContext.get_ge_config_version( context_root_dir=directory ) upgrade_helper_class = ( GE_UPGRADE_HELPER_VERSION_MAP.get(int(ge_config_version)) if ge_config_version else None ) if upgrade_helper_class and ge_config_version < CURRENT_GE_CONFIG_VERSION: upgrade_project( context_root_dir=directory, ge_config_version=ge_config_version, from_cli_upgrade_command=from_cli_upgrade_command, ) else: cli_message(f"<red>{err.message}</red>") sys.exit(1) except ( ge_exceptions.ConfigNotFoundError, ge_exceptions.InvalidConfigError, ) as err: cli_message(f"<red>{err.message}</red>") sys.exit(1) except ge_exceptions.PluginModuleNotFoundError as err: cli_message(err.cli.v012_colored_message) sys.exit(1) except ge_exceptions.PluginClassNotFoundError as err: cli_message(err.cli.v012_colored_message) sys.exit(1) except ge_exceptions.InvalidConfigurationYamlError as err: cli_message(f"<red>{str(err)}</red>") sys.exit(1)
def load_data_context_with_error_handling( directory: str, from_cli_upgrade_command: bool = False) -> Optional[DataContext]: """Return a DataContext with good error handling and exit codes.""" context: Optional[DataContext] ge_config_version: float try: directory = directory or DataContext.find_context_root_dir() context = DataContext(context_root_dir=directory) ge_config_version = context.get_config().config_version if from_cli_upgrade_command: if ge_config_version < CURRENT_GE_CONFIG_VERSION: context = upgrade_project_one_or_multiple_versions_increment( directory=directory, context=context, ge_config_version=ge_config_version, from_cli_upgrade_command=from_cli_upgrade_command, ) elif ge_config_version > CURRENT_GE_CONFIG_VERSION: raise ge_exceptions.UnsupportedConfigVersionError( f"""Invalid config version ({ge_config_version}).\n The maximum valid version is \ {CURRENT_GE_CONFIG_VERSION}. """) else: context = upgrade_project_zero_versions_increment( directory=directory, context=context, ge_config_version=ge_config_version, from_cli_upgrade_command=from_cli_upgrade_command, ) return context except ge_exceptions.UnsupportedConfigVersionError as err: directory = directory or DataContext.find_context_root_dir() ge_config_version = DataContext.get_ge_config_version( context_root_dir=directory) context = upgrade_project_strictly_multiple_versions_increment( directory=directory, ge_config_version=ge_config_version, from_cli_upgrade_command=from_cli_upgrade_command, ) if context: return context else: cli_message(string=f"<red>{err.message}</red>") sys.exit(1) except ( ge_exceptions.ConfigNotFoundError, ge_exceptions.InvalidConfigError, ) as err: cli_message(string=f"<red>{err.message}</red>") sys.exit(1) except ge_exceptions.PluginModuleNotFoundError as err: cli_message(string=err.cli_colored_message) sys.exit(1) except ge_exceptions.PluginClassNotFoundError as err: cli_message(string=err.cli_colored_message) sys.exit(1) except ge_exceptions.InvalidConfigurationYamlError as err: cli_message(string=f"<red>{str(err)}</red>") sys.exit(1)