-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathlist.py
339 lines (272 loc) · 10.8 KB
/
list.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import json
from collections.abc import Sequence
from pathlib import Path
from typing import Optional
import click
from dagster_shared import check
from dagster_shared.record import as_dict
from dagster_shared.serdes import deserialize_value
from dagster_shared.serdes.objects import PluginObjectSnap
from dagster_shared.serdes.objects.definition_metadata import (
DgAssetCheckMetadata,
DgAssetMetadata,
DgJobMetadata,
DgScheduleMetadata,
DgSensorMetadata,
)
from rich.console import Console
from rich.table import Table
from rich.text import Text
from dagster_dg.cli.shared_options import dg_global_options
from dagster_dg.component import PluginObjectFeature, RemotePluginRegistry
from dagster_dg.config import normalize_cli_config
from dagster_dg.context import DgContext
from dagster_dg.env import ProjectEnvVars
from dagster_dg.utils import DgClickCommand, DgClickGroup
from dagster_dg.utils.telemetry import cli_telemetry_wrapper
@click.group(name="list", cls=DgClickGroup)
def list_group():
"""Commands for listing Dagster entities."""
# ########################
# ##### HELPERS
# ########################
def DagsterInnerTable(columns: Sequence[str]) -> Table:
table = Table(border_style="dim", show_lines=True)
table.add_column(columns[0], style="bold cyan", no_wrap=True)
for column in columns[1:]:
table.add_column(column, style="bold")
return table
def DagsterOuterTable(columns: Sequence[str]) -> Table:
table = Table(border_style="dim")
for column in columns:
table.add_column(column, style="bold")
return table
# ########################
# ##### PROJECT
# ########################
@list_group.command(name="project", cls=DgClickCommand)
@dg_global_options
@cli_telemetry_wrapper
def list_project_command(**global_options: object) -> None:
"""List projects in the current workspace."""
cli_config = normalize_cli_config(global_options, click.get_current_context())
dg_context = DgContext.for_workspace_environment(Path.cwd(), cli_config)
for project in dg_context.project_specs:
click.echo(project.path)
# ########################
# ##### COMPONENT
# ########################
@list_group.command(name="component", cls=DgClickCommand)
@dg_global_options
@cli_telemetry_wrapper
def list_component_command(**global_options: object) -> None:
"""List Dagster component instances defined in the current project."""
cli_config = normalize_cli_config(global_options, click.get_current_context())
dg_context = DgContext.for_project_environment(Path.cwd(), cli_config)
for component_instance_name in dg_context.get_component_instance_names():
click.echo(component_instance_name)
# ########################
# ##### PLUGINS
# ########################
FEATURE_COLOR_MAP = {"component": "deep_sky_blue3", "scaffold-target": "khaki1"}
def _pretty_features(obj: PluginObjectSnap) -> Text:
text = Text()
for entry_type in obj.features:
if len(text) > 0:
text += Text(", ")
text += Text(entry_type, style=FEATURE_COLOR_MAP.get(entry_type, ""))
text = Text("[") + text + Text("]")
return text
def _plugin_object_table(entries: Sequence[PluginObjectSnap]) -> Table:
sorted_entries = sorted(entries, key=lambda x: x.key.to_typename())
table = DagsterInnerTable(["Symbol", "Summary", "Features"])
for entry in sorted_entries:
table.add_row(entry.key.to_typename(), entry.summary, _pretty_features(entry))
return table
def _all_plugins_object_table(
registry: RemotePluginRegistry, name_only: bool, feature: Optional[PluginObjectFeature]
) -> Table:
table = DagsterOuterTable(["Plugin"] if name_only else ["Plugin", "Objects"])
for package in sorted(registry.packages):
if not name_only:
objs = registry.get_objects(package, feature)
inner_table = _plugin_object_table(objs)
table.add_row(package, inner_table)
else:
table.add_row(package)
return table
@list_group.command(name="plugins", cls=DgClickCommand)
@click.option(
"--name-only",
is_flag=True,
default=False,
help="Only display the names of the plugin packages.",
)
@click.option(
"--plugin",
"-p",
help="Filter by plugin name.",
)
@click.option(
"--feature",
"-f",
type=click.Choice(["component", "scaffold-target"]),
help="Filter by object type.",
)
@click.option(
"--json",
"output_json",
is_flag=True,
default=False,
help="Output as JSON instead of a table.",
)
@dg_global_options
@cli_telemetry_wrapper
def list_plugins_command(
name_only: bool,
plugin: Optional[str],
feature: Optional[PluginObjectFeature],
output_json: bool,
**global_options: object,
) -> None:
"""List dg plugins and their corresponding objects in the current Python environment."""
cli_config = normalize_cli_config(global_options, click.get_current_context())
dg_context = DgContext.for_defined_registry_environment(Path.cwd(), cli_config)
registry = RemotePluginRegistry.from_dg_context(dg_context)
if output_json:
output: list[dict[str, object]] = []
for entry in sorted(registry.get_objects(), key=lambda x: x.key.to_typename()):
output.append(
{
"key": entry.key.to_typename(),
"summary": entry.summary,
"features": entry.features,
}
)
click.echo(json.dumps(output, indent=4))
else: # table output
if plugin:
table = _plugin_object_table(registry.get_objects(plugin, feature))
else:
table = _all_plugins_object_table(registry, name_only, feature=feature)
Console().print(table)
# ########################
# ##### DEFS
# ########################
def _get_assets_table(assets: Sequence[DgAssetMetadata]) -> Table:
table = DagsterInnerTable(["Key", "Group", "Deps", "Kinds", "Description"])
table.columns[-1].max_width = 100
for asset in sorted(assets, key=lambda x: x.key):
description = Text(asset.description or "")
description.truncate(max_width=100, overflow="ellipsis")
table.add_row(
asset.key,
asset.group,
"\n".join(asset.deps),
"\n".join(asset.kinds),
description,
)
return table
def _get_asset_checks_table(asset_checks: Sequence[DgAssetCheckMetadata]) -> Table:
table = DagsterInnerTable(["Key", "Additional Deps", "Description"])
table.columns[-1].max_width = 100
for asset_check in sorted(asset_checks, key=lambda x: x.key):
description = Text(asset_check.description or "")
description.truncate(max_width=100, overflow="ellipsis")
table.add_row(
asset_check.key,
"\n".join(asset_check.additional_deps),
description,
)
return table
def _get_jobs_table(jobs: Sequence[DgJobMetadata]) -> Table:
table = DagsterInnerTable(["Name"])
for job in sorted(jobs, key=lambda x: x.name):
table.add_row(job.name)
return table
def _get_schedules_table(schedules: Sequence[DgScheduleMetadata]) -> Table:
table = DagsterInnerTable(["Name", "Cron schedule"])
for schedule in sorted(schedules, key=lambda x: x.name):
table.add_row(schedule.name, schedule.cron_schedule)
return table
def _get_sensors_table(sensors: Sequence[DgSensorMetadata]) -> Table:
table = DagsterInnerTable(["Name"])
for sensor in sorted(sensors, key=lambda x: x.name):
table.add_row(sensor.name)
return table
@list_group.command(name="defs", cls=DgClickCommand)
@click.option(
"--json",
"output_json",
is_flag=True,
default=False,
help="Output as JSON instead of a table.",
)
@dg_global_options
@cli_telemetry_wrapper
def list_defs_command(output_json: bool, **global_options: object) -> None:
"""List registered Dagster definitions in the current project environment."""
cli_config = normalize_cli_config(global_options, click.get_current_context())
dg_context = DgContext.for_project_environment(Path.cwd(), cli_config)
result = dg_context.external_components_command(
[
"list",
"definitions",
"-m",
dg_context.code_location_target_module_name,
],
# Sets the "--location" option for "dagster-components definitions list"
# using the click auto-envvar prefix for backwards compatibility on older versions
# before that option was added
additional_env={"DG_CLI_LIST_DEFINITIONS_LOCATION": dg_context.code_location_name},
)
definitions = check.is_list(deserialize_value(result))
# JSON
if output_json: # pass it straight through
json_output = [as_dict(defn) for defn in definitions]
click.echo(json.dumps(json_output, indent=4))
# TABLE
else:
assets = [item for item in definitions if isinstance(item, DgAssetMetadata)]
asset_checks = [item for item in definitions if isinstance(item, DgAssetCheckMetadata)]
jobs = [item for item in definitions if isinstance(item, DgJobMetadata)]
schedules = [item for item in definitions if isinstance(item, DgScheduleMetadata)]
sensors = [item for item in definitions if isinstance(item, DgSensorMetadata)]
if len(definitions) == 0:
click.echo("No definitions are defined for this project.")
console = Console()
table = Table(border_style="dim")
table.add_column("Section", style="bold")
table.add_column("Definitions")
if assets:
table.add_row("Assets", _get_assets_table(assets))
if asset_checks:
table.add_row("Asset Checks", _get_asset_checks_table(asset_checks))
if jobs:
table.add_row("Jobs", _get_jobs_table(jobs))
if schedules:
table.add_row("Schedules", _get_schedules_table(schedules))
if sensors:
table.add_row("Sensors", _get_sensors_table(sensors))
console.print(table)
# ########################
# ##### ENVIRONMENT
# ########################
@list_group.command(name="env", cls=DgClickCommand)
@dg_global_options
@cli_telemetry_wrapper
def list_env_command(**global_options: object) -> None:
"""List environment variables from the .env file of the current project."""
cli_config = normalize_cli_config(global_options, click.get_current_context())
dg_context = DgContext.for_project_environment(Path.cwd(), cli_config)
env = ProjectEnvVars.from_ctx(dg_context)
if not env.values:
click.echo("No environment variables are defined for this project.")
return
table = Table(border_style="dim")
table.add_column("Env Var")
table.add_column("Value")
for key, value in env.values.items():
table.add_row(key, value)
console = Console()
console.print(table)