-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdatabase.py
More file actions
51 lines (42 loc) · 1.52 KB
/
database.py
File metadata and controls
51 lines (42 loc) · 1.52 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
"""Contains hooks related to the database."""
from __future__ import annotations
from pathlib import Path
from typing import Any
from sqlalchemy.engine import make_url
from _pytask.database_utils import create_database
from _pytask.pluginmanager import hookimpl
@hookimpl
def pytask_parse_config(config: dict[str, Any]) -> None:
"""Parse the configuration."""
database_url = config["database_url"]
# Set default.
if not database_url:
config["database_url"] = make_url(
f"sqlite:///{config['root'].joinpath('.pytask').as_posix()}/pytask.sqlite3"
)
elif isinstance(database_url, str):
config["database_url"] = make_url(database_url)
if (
config["database_url"].drivername == "sqlite"
and config["database_url"].database
) and not Path(config["database_url"].database).is_absolute():
if config["config"]:
full_path = (
config["config"]
.parent.joinpath(config["database_url"].database)
.resolve()
)
else:
full_path = (
config["root"].joinpath(config["database_url"].database).resolve()
)
config["database_url"] = config["database_url"]._replace(
database=full_path.as_posix()
)
@hookimpl
def pytask_post_parse(config: dict[str, Any]) -> None:
"""Post-parse the configuration."""
command = config.get("command")
if command not in (None, "build"):
return
create_database(config["database_url"])