forked from AltimateAI/vscode-dbt-power-user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbt_cloud_integration.py
58 lines (49 loc) · 1.39 KB
/
dbt_cloud_integration.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
from decimal import Decimal
import os
import sys
import contextlib
from collections.abc import Iterable
from datetime import date, datetime, time
from typing import (
Dict,
List,
)
@contextlib.contextmanager
def add_path(path):
sys.path.append(path)
try:
yield
finally:
sys.path.remove(path)
def validate_sql(
sql: str,
dialect: str,
models: List[Dict],
):
try:
ALTIMATE_PACKAGE_PATH = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "altimate_packages"
)
with add_path(ALTIMATE_PACKAGE_PATH):
from altimate.validate_sql import validate_sql_from_models
return validate_sql_from_models(sql, dialect, models)
except Exception as e:
raise Exception(str(e))
def to_dict(obj):
if isinstance(obj, str):
return obj
if isinstance(obj, Decimal):
return float(obj)
if isinstance(obj, (datetime, date, time)):
return obj.isoformat()
elif isinstance(obj, dict):
return dict((key, to_dict(val)) for key, val in obj.items())
elif isinstance(obj, Iterable):
return [to_dict(val) for val in obj]
elif hasattr(obj, "__dict__"):
return to_dict(vars(obj))
elif hasattr(obj, "__slots__"):
return to_dict(
dict((name, getattr(obj, name)) for name in getattr(obj, "__slots__"))
)
return obj