-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.py
67 lines (51 loc) · 1.6 KB
/
common.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
import os
import sys
from pathlib import Path
from typing import Iterable
import funcy
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
from gql.transport.exceptions import TransportServerError
QUERIES = Path(__file__).parent / 'queries'
env_file = Path(__file__).parent / '.env'
if env_file.is_file():
lines = env_file.read_text().splitlines()
for line in lines:
name, value = line.split('=')
os.environ[name] = value
token = os.environ.get('DATAFOLD_API_KEY')
if token is None:
print('Please set DATAFOLD_API_KEY environment variable.\n')
print(
'You can do that by writing it into `.env` file '
'in the root directory of this project. It will be .gitignore-d.'
)
sys.exit(-1)
host = os.environ.get('DATAFOLD_HOST', 'https://app.datafold.com')
transport = AIOHTTPTransport(
url=host + '/api/graphql',
headers={'Authorization': 'Key ' + token},
)
client = Client(
transport=transport,
fetch_schema_from_transport=False,
execute_timeout=20 * 60,
)
def load_query(filename):
with open(filename) as f:
return gql(f.read())
@funcy.retry(errors=TransportServerError, tries=3, timeout=1)
def execute_stored_query(
file_path: Path,
**variables: str | int | list[int] | None,
):
return client.execute(
gql(file_path.read_text()),
variables,
)
def sql_quote_path(path: Iterable[str]) -> str:
"""Quote table path in SQL-99 compliant way."""
quoted = ['"{}"'.format(x.replace('"', '""')) for x in path]
return ".".join(quoted)
class NotFoundError(Exception):
pass