-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathpackages.py
More file actions
86 lines (64 loc) · 1.72 KB
/
packages.py
File metadata and controls
86 lines (64 loc) · 1.72 KB
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
import re
from typing import Optional
# All supported resource types and their namespace
RESOURCE_NAMESPACE = {
"resources.Job": "jobs",
"resources.Pipeline": "pipelines",
"resources.Catalog": "catalogs",
"resources.Schema": "schemas",
"resources.Volume": "volumes",
}
RESOURCE_TYPES = list(RESOURCE_NAMESPACE.keys())
# Namespaces to load from OpenAPI spec.
#
# We can't load all types because of errors while loading some of them.
LOADED_NAMESPACES = [
"compute",
"jobs",
"pipelines",
"resources",
"catalog",
]
RENAMES = {
"string": "str",
"boolean": "bool",
"integer": "int",
"number": "float",
"int64": "int",
"float64": "float",
}
PRIMITIVES = [
"string",
"boolean",
"integer",
"number",
"bool",
"int",
"int64",
"float64",
]
def get_class_name(ref: str) -> str:
name = ref.split("/")[-1]
name = name.split(".")[-1]
return RENAMES.get(name, name)
def is_resource(ref: str) -> bool:
return ref in RESOURCE_TYPES
def should_load_ref(ref: str) -> bool:
name = ref.split("/")[-1]
for namespace in LOADED_NAMESPACES:
if name.startswith(f"{namespace}."):
return True
return name in PRIMITIVES
def get_root_package(namespace: str) -> str:
return f"databricks.bundles.{namespace}"
def get_package(namespace: str, ref: str) -> Optional[str]:
"""
Returns Python package for a given OpenAPI ref.
Returns None for builtin types.
"""
full_name = ref.split("/")[-1]
if full_name in PRIMITIVES:
return None
[_, name] = full_name.split(".")
package_name = re.sub(r"(?<!^)(?=[A-Z])", "_", name).lower()
return f"{get_root_package(namespace)}._models.{package_name}"