-
Notifications
You must be signed in to change notification settings - Fork 1
/
soda_util.py
64 lines (51 loc) · 1.65 KB
/
soda_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from pathlib import Path
from typing import TypedDict
from urllib.parse import urlparse
import yaml
class SodaConnection(TypedDict):
type: str
host: str
port: int
username: str
password: str
database: str
schema: str
class SodaWarehouse(TypedDict):
name: str
connection: SodaConnection
def build_soda_warehouse(
warehouse_name: str,
database_name: str,
database_type: str = "postgres",
schema: str = "public",
port: int = 5432,
airflow_connection: str = "AIRFLOW_CONN_DEFAULT_POSTGRES",
) -> SodaWarehouse:
"""
Builds a Soda Warehouse dictionary object
"""
connection = urlparse(os.getenv(airflow_connection))
warehouse: SodaWarehouse = {
"name": warehouse_name,
"connection": {
"type": database_type,
"host": str(connection.hostname),
"port": connection.port or port,
"username": str(connection.username),
"password": str(connection.password),
"database": str(database_name),
"schema": schema,
},
}
return warehouse
def convert_templated_yml_to_dict(scan_path: Path, scan_file: str):
"""
Converts a templated yml file to a templated dictionary
Allows the use of templates in yml fields which are not currently templated by Soda SQL (e.g. table_name)
Enhancement request submitted to Soda SQL team and they are looking to implement in the coming weeks
Could alternatively just create the scan as a dict, but type safety and readability suffer
"""
path = scan_path / scan_file
with open(path) as file:
return yaml.safe_load(file)