-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionalMangledDLTContext.py
More file actions
45 lines (33 loc) · 1.35 KB
/
Copy pathOptionalMangledDLTContext.py
File metadata and controls
45 lines (33 loc) · 1.35 KB
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
import os
from typing import Optional
class BothENVAndConfigPresent(Exception):
def __init__(self, msg: str) -> None:
"""
:type msg: str
"""
super().__init__(msg)
class OptionalMangledDLTContext:
def _is_mangleddlt_envs_present(self) -> bool:
return all(os.environ.get(var) for var in ["DATABRICKS_HOST", "DATABRICKS_TOKEN", "DATABRICKS_WAREHOUSE_ID"])
def _is_databricks_runtime(self) -> bool:
"""Check if running on Databricks runtime."""
return 'DATABRICKS_RUNTIME_VERSION' in os.environ
def __init__(self, config: Optional[dict] = None) -> None:
self.config = config
def __enter__(self):
if self._is_databricks_runtime():
return self
from mangledlt import MangledDLT
if self.config is not None and self._is_mangleddlt_envs_present():
raise BothENVAndConfigPresent("Provide either config or ENV vars, not both")
if self.config is not None:
self.mdlt = MangledDLT(config=self.config)
self.mdlt.enable()
elif self._is_mangleddlt_envs_present():
self.mdlt = MangledDLT()
self.mdlt.enable()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self.mdlt is not None:
self.mdlt.disable()
return False # Propagate any exception