|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | + |
1 | 4 | import dbt.adapters.factory |
2 | 5 |
|
3 | 6 | dbt.adapters.factory.get_adapter = lambda config: config.adapter |
|
27 | 30 | disable_tracking() |
28 | 31 |
|
29 | 32 |
|
| 33 | +def default_project_dir() -> Path: |
| 34 | + if "DBT_PROJECT_DIR" in os.environ: |
| 35 | + return Path(os.environ["DBT_PROJECT_DIR"]).resolve() |
| 36 | + paths = list(Path.cwd().parents) |
| 37 | + paths.insert(0, Path.cwd()) |
| 38 | + return next((x for x in paths if (x / "dbt_project.yml").exists()), Path.cwd()) |
| 39 | + |
| 40 | + |
| 41 | +def default_profiles_dir() -> Path: |
| 42 | + if "DBT_PROFILES_DIR" in os.environ: |
| 43 | + return Path(os.environ["DBT_PROFILES_DIR"]).resolve() |
| 44 | + return Path.cwd() if (Path.cwd() / "profiles.yml").exists() else Path.home() / ".dbt" |
| 45 | + |
| 46 | + |
| 47 | +DEFAULT_PROFILES_DIR = str(default_profiles_dir()) |
| 48 | +DEFAULT_PROJECT_DIR = str(default_project_dir()) |
| 49 | + |
| 50 | + |
30 | 51 | class DbtProject: |
31 | 52 | def __init__(self, project_dir, target=None): |
32 | 53 | args = Args(project_dir=project_dir, target=target) |
@@ -133,7 +154,8 @@ class Args(BaseModel): |
133 | 154 | Minimal mock to dbt config arguments |
134 | 155 | """ |
135 | 156 |
|
136 | | - project_dir: str |
| 157 | + project_dir: str = DEFAULT_PROJECT_DIR |
| 158 | + profiles_dir: str = DEFAULT_PROFILES_DIR |
137 | 159 | profile: str = None |
138 | 160 | target: Optional[str] = None |
139 | 161 | threads: Optional[int] = 1 |
0 commit comments