|
| 1 | +import asyncio |
| 2 | +from importlib import metadata |
| 3 | +from pathlib import Path |
| 4 | +from typing import Annotated |
| 5 | +from text_2_sql_core.utils.database import DatabaseEngine |
| 6 | + |
| 7 | +import typer |
| 8 | +from rich import print as rich_print |
| 9 | + |
| 10 | +cli = typer.Typer(pretty_exceptions_show_locals=False, no_args_is_help=True) |
| 11 | + |
| 12 | +__version__ = metadata.version(__package__) |
| 13 | + |
| 14 | + |
| 15 | +def version_callback(value: bool) -> None: |
| 16 | + """Print the version of the CLI.""" |
| 17 | + if value: |
| 18 | + print(__version__) |
| 19 | + raise typer.Exit() |
| 20 | + |
| 21 | + |
| 22 | +@cli.callback() |
| 23 | +def callback( |
| 24 | + _: bool = typer.Option(None, "--version", "-v", callback=version_callback) |
| 25 | +) -> None: |
| 26 | + """Text2SQL Data Dictionary Creator CLI.""" |
| 27 | + |
| 28 | + |
| 29 | +@cli.command() |
| 30 | +def create( |
| 31 | + engine: DatabaseEngine, |
| 32 | + output_directory: Annotated[ |
| 33 | + Path | None, |
| 34 | + typer.Option( |
| 35 | + "--output-directory", |
| 36 | + "-o", |
| 37 | + help="Optional directory that the script will write the output files to.", |
| 38 | + show_default=False, |
| 39 | + exists=True, |
| 40 | + file_okay=True, |
| 41 | + dir_okay=False, |
| 42 | + writable=True, |
| 43 | + readable=True, |
| 44 | + resolve_path=True, |
| 45 | + ), |
| 46 | + ] = None, |
| 47 | + single_file: Annotated[ |
| 48 | + bool, |
| 49 | + typer.Option( |
| 50 | + "--single_file", |
| 51 | + "-s", |
| 52 | + help="Optional flag that writes all schemas to a single file.", |
| 53 | + ), |
| 54 | + ] = False, |
| 55 | +) -> None: |
| 56 | + """Execute a Text2SQL Data Dictionary Creator YAML file. |
| 57 | +
|
| 58 | + Args: |
| 59 | + ---- |
| 60 | + engine (DatabaseEngine): |
| 61 | +
|
| 62 | + Returns: |
| 63 | + ------- |
| 64 | + None |
| 65 | +
|
| 66 | + """ |
| 67 | + |
| 68 | + try: |
| 69 | + if engine == DatabaseEngine.DATABRICKS: |
| 70 | + from text_2_sql_core.data_dictionary.databricks_data_dictionary_creator import ( |
| 71 | + DatabricksDataDictionaryCreator, |
| 72 | + ) |
| 73 | + |
| 74 | + data_dictionary_creator = DatabricksDataDictionaryCreator() |
| 75 | + elif engine == DatabaseEngine.SNOWFLAKE: |
| 76 | + from text_2_sql_core.data_dictionary.snowflake_data_dictionary_creator import ( |
| 77 | + SnowflakeDataDictionaryCreator, |
| 78 | + ) |
| 79 | + |
| 80 | + data_dictionary_creator = SnowflakeDataDictionaryCreator() |
| 81 | + elif engine == DatabaseEngine.TSQL: |
| 82 | + from text_2_sql_core.data_dictionary.tsql_data_dictionary_creator import ( |
| 83 | + TSQLDataDictionaryCreator, |
| 84 | + ) |
| 85 | + |
| 86 | + data_dictionary_creator = TSQLDataDictionaryCreator() |
| 87 | + except ImportError: |
| 88 | + detailed_error = f"""Failed to import { |
| 89 | + engine.value} Data Dictionary Creator. Check you have installed the optional dependencies for this database engine.""" |
| 90 | + rich_print("Text2SQL Data Dictionary Creator Failed ❌") |
| 91 | + rich_print(detailed_error) |
| 92 | + |
| 93 | + raise typer.Exit(code=1) |
| 94 | + |
| 95 | + try: |
| 96 | + asyncio.run(data_dictionary_creator.create_data_dictionary()) |
| 97 | + except Exception as e: |
| 98 | + rich_print("Text2SQL Data Dictionary Creator Failed ❌") |
| 99 | + |
| 100 | + rich_print(f"Error Messages: {e}") |
| 101 | + |
| 102 | + raise typer.Exit(code=1) |
| 103 | + else: |
| 104 | + rich_print("Text2SQL Data Dictionary Creator Completed Successfully ✅") |
0 commit comments