forked from databricks/databricks-sql-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
90 lines (61 loc) · 2.31 KB
/
__init__.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import datetime
from databricks.sql.exc import *
# PEP 249 module globals
apilevel = "2.0"
threadsafety = 1 # Threads may share the module, but not connections.
paramstyle = "named"
import re
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# Use this import purely for type annotations, a la https://mypy.readthedocs.io/en/latest/runtime_troubles.html#import-cycles
from .client import Connection
class RedactUrlQueryParamsFilter(logging.Filter):
pattern = re.compile(r"(\?|&)([\w-]+)=([^&]+)")
mask = r"\1\2=<REDACTED>"
def __init__(self):
super().__init__()
def redact(self, string):
return re.sub(self.pattern, self.mask, str(string))
def filter(self, record):
record.msg = self.redact(str(record.msg))
if isinstance(record.args, dict):
for k in record.args.keys():
record.args[k] = (
self.redact(record.args[k])
if isinstance(record.arg[k], str)
else record.args[k]
)
else:
record.args = tuple(
(self.redact(arg) if isinstance(arg, str) else arg)
for arg in record.args
)
return True
logging.getLogger("urllib3.connectionpool").addFilter(RedactUrlQueryParamsFilter())
class DBAPITypeObject(object):
def __init__(self, *values):
self.values = values
def __eq__(self, other):
return other in self.values
def __repr__(self):
return "DBAPITypeObject({})".format(self.values)
STRING = DBAPITypeObject("string")
BINARY = DBAPITypeObject("binary")
NUMBER = DBAPITypeObject(
"boolean", "tinyint", "smallint", "int", "bigint", "float", "double", "decimal"
)
DATETIME = DBAPITypeObject("timestamp")
DATE = DBAPITypeObject("date")
ROWID = DBAPITypeObject()
__version__ = "3.6.0"
USER_AGENT_NAME = "PyDatabricksSqlConnector"
# These two functions are pyhive legacy
Date = datetime.date
Timestamp = datetime.datetime
def DateFromTicks(ticks):
return Date(*time.localtime(ticks)[:3])
def TimestampFromTicks(ticks):
return Timestamp(*time.localtime(ticks)[:6])
def connect(server_hostname, http_path, access_token=None, **kwargs) -> "Connection":
from .client import Connection
return Connection(server_hostname, http_path, access_token, **kwargs)