def select_datasource(context: DataContext, datasource_name: str = None) -> Datasource: """Select a datasource interactively.""" # TODO consolidate all the myriad CLI tests into this data_source = None if datasource_name is None: data_sources = sorted(context.list_datasources(), key=lambda x: x["name"]) if len(data_sources) == 0: cli_message( "<red>No datasources found in the context. To add a datasource, run `great_expectations datasource new`</red>" ) elif len(data_sources) == 1: datasource_name = data_sources[0]["name"] else: choices = "\n".join( [ " {}. {}".format(i, data_source["name"]) for i, data_source in enumerate(data_sources, 1) ] ) option_selection = click.prompt( "Select a datasource" + "\n" + choices + "\n", type=click.Choice( [str(i) for i, data_source in enumerate(data_sources, 1)] ), show_choices=False, ) datasource_name = data_sources[int(option_selection) - 1]["name"] if datasource_name is not None: data_source = context.get_datasource(datasource_name) return data_source
def select_datasource(context: DataContext, datasource_name: str = None) -> BaseDatasource: """Select a datasource interactively.""" # TODO consolidate all the myriad CLI tests into this data_source: Optional[BaseDatasource] = None if datasource_name is None: data_sources: List[BaseDatasource] = cast( List[BaseDatasource], list(sorted(context.datasources.values(), key=lambda x: x.name), ), ) if len(data_sources) == 0: cli_message( string= "<red>No datasources found in the context. To add a datasource, run `great_expectations datasource new`</red>" ) elif len(data_sources) == 1: datasource_name = data_sources[0].name else: choices: str = "\n".join([ f" {i}. {data_source.name}" for i, data_source in enumerate(data_sources, 1) ]) option_selection: str = click.prompt( "Select a datasource" + "\n" + choices + "\n", type=click.Choice( [str(i) for i, data_source in enumerate(data_sources, 1)]), show_choices=False, ) datasource_name = data_sources[int(option_selection) - 1].name if datasource_name is not None: data_source = context.get_datasource(datasource_name=datasource_name) return data_source