Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Globally applied ruff formatting changes #275

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions datacontract/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class ImportFormat(str, Enum):
glue = "glue"
bigquery = "bigquery"
jsonschema = "jsonschema"
odcs="odcs"
odcs = "odcs"
unity = "unity"


Expand All @@ -228,12 +228,16 @@ def import_(
help="List of table ids to import from the bigquery API (repeat for multiple table ids, leave empty for all tables in the dataset)."
),
] = None,
unity_table_full_name: Annotated[Optional[str], typer.Option(help="Full name of a table in the unity catalog")] = None,
unity_table_full_name: Annotated[
Optional[str], typer.Option(help="Full name of a table in the unity catalog")
] = None,
):
"""
Create a data contract from the given source location. Prints to stdout.
"""
result = DataContract().import_from_source(format, source, glue_table, bigquery_table, bigquery_project, bigquery_dataset, unity_table_full_name)
result = DataContract().import_from_source(
format, source, glue_table, bigquery_table, bigquery_project, bigquery_dataset, unity_table_full_name
)
console.print(result.to_yaml())


Expand Down
6 changes: 2 additions & 4 deletions datacontract/data_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def import_from_source(
bigquery_tables: typing.Optional[typing.List[str]] = None,
bigquery_project: typing.Optional[str] = None,
bigquery_dataset: typing.Optional[str] = None,
unity_table_full_name: typing.Optional[str] = None
unity_table_full_name: typing.Optional[str] = None,
) -> DataContractSpecification:
data_contract_specification = DataContract.init()

Expand All @@ -357,9 +357,7 @@ def import_from_source(
if source is not None:
data_contract_specification = import_unity_from_json(data_contract_specification, source)
else:
data_contract_specification = import_unity_from_api(
data_contract_specification, unity_table_full_name
)
data_contract_specification = import_unity_from_api(data_contract_specification, unity_table_full_name)
else:
print(f"Import format {format} not supported.")

Expand Down
16 changes: 4 additions & 12 deletions datacontract/imports/glue_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,9 @@ def get_glue_tables(database_name: str) -> List[str]:
table_names = []
try:
# Paginate through the tables
for page in paginator.paginate(
DatabaseName=database_name, PaginationConfig={"PageSize": 100}
):
for page in paginator.paginate(DatabaseName=database_name, PaginationConfig={"PageSize": 100}):
# Add the tables from the current page to the list
table_names.extend(
[table["Name"] for table in page["TableList"] if "Name" in table]
)
table_names.extend([table["Name"] for table in page["TableList"] if "Name" in table])
except glue.exceptions.EntityNotFoundException:
print(f"Database {database_name} not found.")
return []
Expand Down Expand Up @@ -137,9 +133,7 @@ def import_glue(
table_names = get_glue_tables(source)

data_contract_specification.servers = {
"production": Server(
type="glue", account=catalogid, database=source, location=location_uri
),
"production": Server(type="glue", account=catalogid, database=source, location=location_uri),
}

for table_name in table_names:
Expand Down Expand Up @@ -192,9 +186,7 @@ def create_typed_field(dtype: str) -> Field:
elif dtype.startswith("struct"):
field.type = "struct"
for f in split_struct(orig_dtype[7:-1]):
field.fields[f.split(":", 1)[0].strip()] = create_typed_field(
f.split(":", 1)[1]
)
field.fields[f.split(":", 1)[0].strip()] = create_typed_field(f.split(":", 1)[1])
else:
field.type = map_type_from_sql(dtype)
return field
Expand Down
18 changes: 9 additions & 9 deletions datacontract/imports/unity_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datacontract.model.data_contract_specification import DataContractSpecification, Model, Field
from datacontract.model.exceptions import DataContractException


def import_unity_from_json(
data_contract_specification: DataContractSpecification, source: str
) -> DataContractSpecification:
Expand All @@ -22,37 +23,36 @@ def import_unity_from_json(
)
return convert_unity_schema(data_contract_specification, unity_schema)


def import_unity_from_api(
data_contract_specification: DataContractSpecification,
unity_table_full_name: typing.Optional[str] = None
data_contract_specification: DataContractSpecification, unity_table_full_name: typing.Optional[str] = None
) -> DataContractSpecification:
databricks_instance = os.getenv('DATABRICKS_IMPORT_INSTANCE')
access_token = os.getenv('DATABRICKS_IMPORT_ACCESS_TOKEN')
databricks_instance = os.getenv("DATABRICKS_IMPORT_INSTANCE")
access_token = os.getenv("DATABRICKS_IMPORT_ACCESS_TOKEN")

if not databricks_instance or not access_token:
print("Missing environment variables for Databricks instance or access token.")
print("Both, $DATABRICKS_IMPORT_INSTANCE and $DATABRICKS_IMPORT_ACCESS_TOKEN must be set.")
exit(1) # Exit if variables are not set

api_url = f'{databricks_instance}/api/2.1/unity-catalog/tables/{unity_table_full_name}'
api_url = f"{databricks_instance}/api/2.1/unity-catalog/tables/{unity_table_full_name}"

headers = {
'Authorization': f'Bearer {access_token}'
}
headers = {"Authorization": f"Bearer {access_token}"}
response = requests.get(api_url, headers=headers)

if response.status_code != 200:
raise DataContractException(
type="schema",
name="Retrieve unity catalog schema",
reason=f"Failed to retrieve unity catalog schema from databricks instance: {response.status_code} {response.text}",
engine="datacontract"
engine="datacontract",
)

convert_unity_schema(data_contract_specification, response.json())

return data_contract_specification


def convert_unity_schema(
data_contract_specification: DataContractSpecification, unity_schema: dict
) -> DataContractSpecification:
Expand Down
1 change: 1 addition & 0 deletions tests/test_import_unity_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

logging.basicConfig(level=logging.DEBUG, force=True)


def test_cli():
print("running test_cli")
runner = CliRunner()
Expand Down