GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — cli/python/codegen/codegen/generated_wiring.py at main · databricks/cli

Expand file tree

/

generated_wiring.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
160 lines (123 loc) · 5.1 KB
/

generated_wiring.py

Copy path

File metadata and controls

160 lines (123 loc) · 5.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""
Generates the per-resource wiring in databricks.bundles.core that used to be
hand-written. All output is rendered from the *.py.tmpl templates in this
directory.
Each wired resource gets its own file _generated/<plural>.py (rendered from
wiring_resource.py.tmpl): the add_* method + collection property mixin, the
*_mutator decorator, and the _ResourceType entry. The generated
_generated/__init__.py (generated_init.py.tmpl) collects them into
_GeneratedResources (mixed into Resources), _all_resource_types(), and the
mutator re-exports. The core package __init__ (core_init.py.tmpl) is generated
too (static exports plus the generated mutators).
"""
from dataclasses import dataclass
from pathlib import Path
from string import Template
import codegen.packages as packages
HEADER = "# Code generated by pydabs-codegen. DO NOT EDIT.\n\n"
def _load_template(name: str) -> Template:
return Template((Path(__file__).parent / name).read_text())
_RESOURCE_TEMPLATE = _load_template("wiring_resource.py.tmpl")
_GENERATED_INIT_TEMPLATE = _load_template("generated_init.py.tmpl")
_CORE_INIT_TEMPLATE = _load_template("core_init.py.tmpl")
@dataclass(frozen=True)
class _WiredResource:
class_name: str
"""Resource dataclass name, e.g. "Job"."""
singular_name: str
"""Singular name used in methods and messages, e.g. "job"."""
plural_name: str
"""Plural name, the same as the "resources" bundle section, e.g. "jobs"."""
model_module: str
"""Module the resource dataclass lives in, e.g. "databricks.bundles.jobs._models.job"."""
def _wired_resources() -> list[_WiredResource]:
# Every namespaced resource is wired. RESOURCE_NAMESPACE is the single source
# of truth for both model generation and wiring.
resources = []
for ref, namespace in packages.RESOURCE_NAMESPACE.items():
class_name = packages.get_class_name(ref)
resources.append(
_WiredResource(
class_name=class_name,
singular_name=packages.to_snake_case(class_name),
plural_name=namespace,
model_module=packages.get_package(namespace, ref),
)
)
resources.sort(key=lambda r: r.plural_name)
return resources
def write_wiring(output: str):
resources = _wired_resources()
core_path = Path(output) / "databricks" / "bundles" / "core"
generated_path = core_path / "_generated"
generated_path.mkdir(parents=True, exist_ok=True)
for r in resources:
code = _RESOURCE_TEMPLATE.substitute(
{
"class": r.class_name,
"singular": r.singular_name,
"plural": r.plural_name,
"model_module": r.model_module,
}
)
(generated_path / f"{r.plural_name}.py").write_text(HEADER + code)
(generated_path / "__init__.py").write_text(HEADER + _collector_code(resources))
(core_path / "__init__.py").write_text(HEADER + _core_init_code(resources))
print(f"Writing wiring into {generated_path}")
def _collector_code(resources: list[_WiredResource]) -> str:
imports = "\n".join(
f"from databricks.bundles.core._generated.{r.plural_name} import "
f"_{r.class_name}Resources, {r.singular_name}_mutator"
for r in resources
)
mutator_names = [f"{r.singular_name}_mutator" for r in resources]
exports = sorted(["_GeneratedResources", "_all_resource_types", *mutator_names])
all_block = "\n".join(f' "{name}",' for name in exports)
mixins = "\n".join(f" _{r.class_name}Resources," for r in resources)
module_imports = "\n".join(f" {r.plural_name}," for r in resources)
type_entries = "\n".join(
f" {r.plural_name}._resource_type()," for r in resources
)
return _GENERATED_INIT_TEMPLATE.substitute(
{
"imports": imports,
"all_block": all_block,
"mixins": mixins,
"module_imports": module_imports,
"type_entries": type_entries,
}
)
# Static, non-resource exports of databricks.bundles.core, used to build the
# sorted __all__ (the resource mutators are appended). Must stay in sync with the
# import statements in core_init.py.tmpl.
_CORE_INIT_STATIC_EXPORTS = [
"Bundle",
"Diagnostic",
"Diagnostics",
"Location",
"Resource",
"ResourceMutator",
"Resources",
"Severity",
"Variable",
"VariableOr",
"VariableOrDict",
"VariableOrList",
"VariableOrOptional",
"load_resources_from_current_package_module",
"load_resources_from_module",
"load_resources_from_modules",
"load_resources_from_package_module",
"variables",
]
def _core_init_code(resources: list[_WiredResource]) -> str:
mutator_names = [f"{r.singular_name}_mutator" for r in resources]
all_exports = sorted(_CORE_INIT_STATIC_EXPORTS + mutator_names)
all_block = "\n".join(f' "{name}",' for name in all_exports)
mutator_imports = ",\n".join(f" {name}" for name in sorted(mutator_names))
return _CORE_INIT_TEMPLATE.substitute(
{
"all_block": all_block,
"mutator_imports": mutator_imports,
}
)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.