Skip to content

Commit 76f1e08

Browse files
aquamatthiasmeln1k
andauthored
[fixlib][feature] Add the source of resource kind (#2218)
Co-authored-by: Nikita Melkozerov <nikita@some.engineering>
1 parent a285340 commit 76f1e08

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

fixlib/fixlib/core/model_export.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ def model_name(clazz: Union[type, Tuple[Any], None]) -> str:
137137
return "any"
138138

139139

140+
def model_source(module_name: str) -> Optional[str]:
141+
if module_name.startswith("fix_plugin_"): # Naming scheme for all fix plugins: fix_plugin_<source>.xxx
142+
return module_name.split(".")[0][11:]
143+
elif module_name.startswith("fixlib.baseresources"): # All base kinds are defined in this package
144+
return "base"
145+
return None
146+
147+
140148
# define if a field should be exported or not.
141149
# Use python default: hide props starting with underscore.
142150
def should_export(field: Attribute) -> bool: # type: ignore
@@ -267,6 +275,8 @@ def export_data_class(clazz: type) -> None:
267275
and isinstance(s, str)
268276
):
269277
metadata["description"] = s
278+
if root and (source := model_source(clazz.__module__)):
279+
metadata["source"] = source
270280

271281
model.append(
272282
{

fixlib/test/core/model_export_test.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from attrs import define, field
66

7-
from fixlib.baseresources import ModelReference
7+
from fixlib.baseresources import ModelReference, BaseAccessKey
88
from fixlib.core.model_export import (
99
is_collection,
1010
type_arg,
@@ -14,6 +14,7 @@
1414
dataclasses_to_fixcore_model,
1515
model_name,
1616
dynamic_object_to_fixcore_model,
17+
model_source,
1718
)
1819

1920

@@ -208,3 +209,11 @@ def test_config_export():
208209
# All global config properties are defined
209210
config = {a["name"] for a in result_dict["config"]["properties"]}
210211
assert config == {"aws", "gcp"}
212+
213+
214+
def test_module_source() -> None:
215+
assert model_source(BaseAccessKey.__module__) == "base"
216+
assert model_source(DataClassBase.__module__) is None
217+
assert model_source("fix_plugin_aws.resource.sagemaker.AwsSagemakerAlgorithm") == "aws"
218+
assert model_source("fix_plugin_digitalocean.resources.DigitalOceanApp") == "digitalocean"
219+
assert model_source("fix_plugin_azure.resource.machinelearning.AzureMachineLearningCompute") == "azure"

0 commit comments

Comments
 (0)