11import os
22from typing import Any , Dict , Optional
33
4- from dlt ._workspace .exceptions import WorkspaceRunContextNotAvailable
5- from dlt ._workspace .profile import DEFAULT_PROFILE , read_profile_pin
64from dlt .common .configuration import plugins as _plugins
75from dlt .common .configuration .specs .pluggable_run_context import RunContextBase
8- from dlt .common .runtime .run_context import RunContext
9-
10- from dlt ._workspace .run_context import default_name
11- from dlt ._workspace ._workspace_context import (
12- WorkspaceRunContext ,
13- is_workspace_dir ,
14- )
6+ from dlt .common .runtime .run_context import DOT_DLT , RunContext
157
168__all__ = ["plug_workspace_context_impl" ]
179
1810
11+ def is_workspace_dir (run_dir : str ) -> bool :
12+ """Checks if `run_dir` contains dlt workspace, this is true if a config file is found"""
13+ return os .path .isfile (os .path .join (run_dir , DOT_DLT , ".workspace" ))
14+
15+
1916@_plugins .hookimpl (specname = "plug_run_context" , trylast = False )
2017def plug_workspace_context_impl (
2118 run_dir : Optional [str ], runtime_kwargs : Optional [Dict [str , Any ]]
@@ -24,12 +21,19 @@ def plug_workspace_context_impl(
2421 # if runtime_kwargs.get("_look_recursive")
2522 run_dir = os .path .abspath (run_dir or "." )
2623 if is_workspace_dir (run_dir ):
24+ # import workspace only when context detected
25+ from dlt ._workspace .profile import DEFAULT_PROFILE , read_profile_pin
26+ from dlt ._workspace .run_context import default_name
27+ from dlt ._workspace ._workspace_context import WorkspaceRunContext
28+
2729 profile : str = None
2830 if runtime_kwargs :
2931 profile = runtime_kwargs .get ("profile" )
3032 profile = profile or read_profile_pin (RunContext (run_dir )) or DEFAULT_PROFILE
3133 return WorkspaceRunContext (default_name (run_dir ), run_dir , profile )
3234 elif runtime_kwargs and runtime_kwargs .get ("_required" ) == "WorkspaceRunContext" :
35+ from dlt ._workspace .exceptions import WorkspaceRunContextNotAvailable
36+
3337 raise WorkspaceRunContextNotAvailable (run_dir )
3438
3539 return None
0 commit comments