-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathloader.py
More file actions
57 lines (47 loc) · 1.94 KB
/
Copy pathloader.py
File metadata and controls
57 lines (47 loc) · 1.94 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
46
47
48
49
50
51
52
53
54
55
56
57
"""Loader helper functions."""
import importlib
import re
from functools import partial
from pathlib import Path
from typing import Any, Callable
import psycopg
def build_loader(load: Callable | str | Path) -> Callable:
"""Build a loader callable."""
if isinstance(load, Path):
return partial(sql, load)
elif isinstance(load, str):
loader_parts = re.split("[.:]", load, maxsplit=2)
import_path = ".".join(loader_parts[:-1])
loader_name = loader_parts[-1]
_temp_import = importlib.import_module(import_path, globals(), locals(), fromlist=[loader_name])
_loader: Callable = getattr(_temp_import, loader_name)
return _loader
else:
return load
def sql(sql_filename: Path, **kwargs: Any) -> None:
"""Database loader for sql files."""
with psycopg.connect(**kwargs) as db_connection:
with open(sql_filename, "r") as _fd:
with db_connection.cursor() as cur:
cur.execute(_fd.read())
db_connection.commit()
def build_loader_async(load: Callable | str | Path) -> Callable:
"""Build a loader callable."""
if isinstance(load, Path):
return partial(sql_async, load)
elif isinstance(load, str):
loader_parts = re.split("[.:]", load, maxsplit=2)
import_path = ".".join(loader_parts[:-1])
loader_name = loader_parts[-1]
_temp_import = importlib.import_module(import_path, globals(), locals(), fromlist=[loader_name])
_loader: Callable = getattr(_temp_import, loader_name)
return _loader
else:
return load
async def sql_async(sql_filename: Path, **kwargs: Any) -> None:
"""Async database loader for sql files."""
async with await psycopg.AsyncConnection.connect(**kwargs) as db_connection:
async with await db_connection.cursor() as cur:
with open(sql_filename, "r") as _fd:
await cur.execute(_fd.read())
await db_connection.commit()