Skip to content

Commit cecbfdb

Browse files
committed
Rename build_defs_from_toplevel_components_folder to build_component_defs and make it less magical
1 parent 32e5be0 commit cecbfdb

File tree

10 files changed

+41
-31
lines changed

10 files changed

+41
-31
lines changed

examples/experimental/components/examples/hello_world_deployment/code_locations/hello_world/hello_world/definitions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from pathlib import Path
22

33
import dagster as dg
4-
from dagster_components import ComponentRegistry, build_defs_from_toplevel_components_folder
4+
from dagster_components import ComponentRegistry, build_component_defs
55
from dagster_components.lib.pipes_subprocess_script_collection import (
66
PipesSubprocessScriptCollection,
77
)
88

9-
defs = build_defs_from_toplevel_components_folder(
10-
path=Path(__file__).parent,
9+
defs = build_component_defs(
10+
code_location_root=Path(__file__).parent.parent,
1111
registry=ComponentRegistry(
1212
{"pipes_subprocess_script_collection": PipesSubprocessScriptCollection}
1313
),

python_modules/libraries/dagster-components/dagster_components/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
component as component,
77
)
88
from dagster_components.core.component_defs_builder import (
9-
build_defs_from_toplevel_components_folder as build_defs_from_toplevel_components_folder,
9+
build_component_defs as build_component_defs,
1010
)
1111
from dagster_components.version import __version__ as __version__

python_modules/libraries/dagster-components/dagster_components/cli/generate.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from dagster_components import ComponentRegistry
99
from dagster_components.core.deployment import (
1010
CodeLocationProjectContext,
11+
find_enclosing_code_location_root_path,
1112
is_inside_code_location_project,
1213
)
1314
from dagster_components.generate import generate_component_instance
@@ -39,8 +40,8 @@ def generate_component_command(
3940
)
4041
sys.exit(1)
4142

42-
context = CodeLocationProjectContext.from_path(
43-
Path.cwd(),
43+
context = CodeLocationProjectContext.from_code_location_path(
44+
find_enclosing_code_location_root_path(Path.cwd()),
4445
ComponentRegistry.from_entry_point_discovery(builtin_component_lib=builtin_component_lib),
4546
)
4647
if not context.has_component_type(component_type):

python_modules/libraries/dagster-components/dagster_components/cli/list.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from dagster_components.core.component import ComponentMetadata, ComponentRegistry
99
from dagster_components.core.deployment import (
1010
CodeLocationProjectContext,
11+
find_enclosing_code_location_root_path,
1112
is_inside_code_location_project,
1213
)
1314
from dagster_components.utils import CLI_BUILTIN_COMPONENT_LIB_KEY
@@ -31,8 +32,8 @@ def list_component_types_command(ctx: click.Context) -> None:
3132
)
3233
sys.exit(1)
3334

34-
context = CodeLocationProjectContext.from_path(
35-
Path.cwd(),
35+
context = CodeLocationProjectContext.from_code_location_path(
36+
find_enclosing_code_location_root_path(Path.cwd()),
3637
ComponentRegistry.from_entry_point_discovery(builtin_component_lib=builtin_component_lib),
3738
)
3839
output: Dict[str, Any] = {}

python_modules/libraries/dagster-components/dagster_components/core/component_decl_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ def path_to_decl_node(path: Path) -> Optional[ComponentDeclNode]:
4747
if component:
4848
subs.append(component)
4949

50-
return ComponentFolder(path=path, sub_decls=subs) if subs else None
50+
return ComponentFolder(path=path, sub_decls=subs)

python_modules/libraries/dagster-components/dagster_components/core/component_defs_builder.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,21 @@ def defs_from_components(
125125

126126
# Public method so optional Nones are fine
127127
@suppress_dagster_warnings
128-
def build_defs_from_toplevel_components_folder(
129-
path: Path,
128+
def build_component_defs(
129+
code_location_root: Path,
130130
resources: Optional[Mapping[str, object]] = None,
131131
registry: Optional["ComponentRegistry"] = None,
132132
) -> "Definitions":
133-
"""Build a Definitions object from an entire component hierarchy."""
133+
"""Build a Definitions object for all the component instances in a given code location.
134+
135+
Args:
136+
code_location_root (Path): The path to the code location root.
137+
The path must be a code location directory that has a pyproject.toml with a [dagster] section.
138+
"""
134139
from dagster._core.definitions.definitions_class import Definitions
135140

136-
context = CodeLocationProjectContext.from_path(
137-
path, registry or ComponentRegistry.from_entry_point_discovery()
141+
context = CodeLocationProjectContext.from_code_location_path(
142+
code_location_root, registry or ComponentRegistry.from_entry_point_discovery()
138143
)
139144

140145
all_defs: List[Definitions] = []

python_modules/libraries/dagster-components/dagster_components/core/deployment.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@
1515

1616
def is_inside_code_location_project(path: Path) -> bool:
1717
try:
18-
_resolve_code_location_root_path(path)
18+
find_enclosing_code_location_root_path(path)
1919
return True
2020
except DagsterError:
2121
return False
2222

2323

24-
def _resolve_code_location_root_path(path: Path) -> Path:
24+
def find_enclosing_code_location_root_path(path: Path) -> Path:
25+
"""Given a path, locate the code location root directory that contains it. It
26+
determines this by finding a pyproject.toml with a [tool.dagster] section.
27+
28+
Searches parent directory recursively until it finds it. It if navigates
29+
to the root directory, it throws an error.
30+
"""
2531
current_path = path.absolute()
2632
while not _is_code_location_root(current_path):
2733
current_path = current_path.parent
@@ -40,11 +46,15 @@ def _is_code_location_root(path: Path) -> bool:
4046

4147
class CodeLocationProjectContext:
4248
@classmethod
43-
def from_path(cls, path: Path, component_registry: "ComponentRegistry") -> Self:
44-
root_path = _resolve_code_location_root_path(path)
49+
def from_code_location_path(cls, path: Path, component_registry: "ComponentRegistry") -> Self:
50+
if not _is_code_location_root(path):
51+
raise DagsterError(
52+
f"Path {path} is not a code location root. Must have a pyproject.toml with a [tool.dagster] section."
53+
)
54+
4555
return cls(
46-
root_path=str(root_path),
47-
name=os.path.basename(root_path),
56+
root_path=str(path),
57+
name=os.path.basename(path),
4858
component_registry=component_registry,
4959
)
5060

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from pathlib import Path
22

33
from dagster._core.definitions.definitions_class import Definitions
4-
from dagster_components.core.component_defs_builder import (
5-
build_defs_from_toplevel_components_folder,
6-
)
4+
from dagster_components.core.component_defs_builder import build_component_defs
75

8-
defs = build_defs_from_toplevel_components_folder(path=Path(__file__).parent)
6+
defs = build_component_defs(code_location_root=Path(__file__).parent.parent)
97

108
if __name__ == "__main__":
119
Definitions.validate_loadable(defs)
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
from pathlib import Path
22

3-
from dagster_components.core.component_defs_builder import (
4-
build_defs_from_toplevel_components_folder,
5-
)
3+
from dagster_components import build_component_defs
64

7-
defs = build_defs_from_toplevel_components_folder(
8-
path=Path(__file__).parent,
9-
)
5+
defs = build_component_defs(code_location_root=Path(__file__).parent.parent)

python_modules/libraries/dagster-dg/dagster_dg/templates/COMPONENT_TYPE/COMPONENT_TYPE_NAME_PLACEHOLDER.py.jinja

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ from dagster_components import (
33
Component,
44
ComponentRegistry,
55
ComponentLoadContext,
6-
build_defs_from_toplevel_components_folder,
76
component,
87
)
98

0 commit comments

Comments
 (0)