Skip to content

Commit 337956d

Browse files
committed
Move protocols template out of ctl dir
1 parent f3334a6 commit 337956d

File tree

5 files changed

+37
-32
lines changed

5 files changed

+37
-32
lines changed

infrahub_sdk/ctl/cli_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from .. import __version__ as sdk_version
2222
from ..async_typer import AsyncTyper
23-
from ..code_generator import CodeGenerator
2423
from ..ctl import config
2524
from ..ctl.branch import app as branch_app
2625
from ..ctl.check import run as run_check
@@ -42,6 +41,7 @@
4241
)
4342
from ..ctl.validate import app as validate_app
4443
from ..exceptions import GraphQLError, ModuleImportError
44+
from ..protocols_generator.generator import CodeGenerator
4545
from ..schema import MainSchemaTypesAll, SchemaRoot
4646
from ..template import Jinja2Template
4747
from ..template.exceptions import JinjaTemplateError

infrahub_sdk/protocols_generator/__init__.py

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
TEMPLATE_FILE_NAME = "template.j2"
2+
3+
ATTRIBUTE_KIND_MAP = {
4+
"ID": "String",
5+
"Text": "String",
6+
"TextArea": "String",
7+
"DateTime": "DateTime",
8+
"Email": "String",
9+
"Password": "String",
10+
"HashedPassword": "HashedPassword",
11+
"URL": "URL",
12+
"File": "String",
13+
"MacAddress": "MacAddress",
14+
"Color": "String",
15+
"Dropdown": "Dropdown",
16+
"Number": "Integer",
17+
"Bandwidth": "Integer",
18+
"IPHost": "IPHost",
19+
"IPNetwork": "IPNetwork",
20+
"Boolean": "Boolean",
21+
"Checkbox": "Boolean",
22+
"List": "ListAttribute",
23+
"JSON": "JSONAttribute",
24+
"Any": "AnyAttribute",
25+
}

infrahub_sdk/code_generator.py renamed to infrahub_sdk/protocols_generator/generator.py

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from __future__ import annotations
22

33
from collections.abc import Mapping
4+
from pathlib import Path
45
from typing import Any
56

67
import jinja2
78

8-
from . import protocols as sdk_protocols
9-
from .ctl.constants import PROTOCOLS_TEMPLATE
10-
from .schema import (
9+
from .. import protocols as sdk_protocols
10+
from ..schema import (
1111
AttributeSchemaAPI,
1212
GenericSchema,
1313
GenericSchemaAPI,
@@ -17,30 +17,12 @@
1717
ProfileSchemaAPI,
1818
RelationshipSchemaAPI,
1919
)
20+
from .constants import ATTRIBUTE_KIND_MAP, TEMPLATE_FILE_NAME
2021

21-
ATTRIBUTE_KIND_MAP = {
22-
"ID": "String",
23-
"Text": "String",
24-
"TextArea": "String",
25-
"DateTime": "DateTime",
26-
"Email": "String",
27-
"Password": "String",
28-
"HashedPassword": "HashedPassword",
29-
"URL": "URL",
30-
"File": "String",
31-
"MacAddress": "MacAddress",
32-
"Color": "String",
33-
"Dropdown": "Dropdown",
34-
"Number": "Integer",
35-
"Bandwidth": "Integer",
36-
"IPHost": "IPHost",
37-
"IPNetwork": "IPNetwork",
38-
"Boolean": "Boolean",
39-
"Checkbox": "Boolean",
40-
"List": "ListAttribute",
41-
"JSON": "JSONAttribute",
42-
"Any": "AnyAttribute",
43-
}
22+
23+
def load_template() -> str:
24+
path = Path(__file__).parent / TEMPLATE_FILE_NAME
25+
return path.read_text()
4426

4527

4628
class CodeGenerator:
@@ -78,7 +60,7 @@ def render(self, sync: bool = True) -> str:
7860
jinja2_env.filters["render_attribute"] = self._jinja2_filter_render_attribute
7961
jinja2_env.filters["render_relationship"] = self._jinja2_filter_render_relationship
8062

81-
template = jinja2_env.from_string(PROTOCOLS_TEMPLATE)
63+
template = jinja2_env.from_string(load_template())
8264
return template.render(
8365
generics=self.sorted_generics,
8466
nodes=self.sorted_nodes,

infrahub_sdk/ctl/constants.py renamed to infrahub_sdk/protocols_generator/template.j2

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
PROTOCOLS_TEMPLATE = """#
1+
#
22
# Generated by "infrahubctl protocols"
33
#
44

55
from __future__ import annotations
66

77
from typing import TYPE_CHECKING, Optional
88

9-
from infrahub_sdk.protocols import CoreNode, {{ base_protocols | join(', ') }}
9+
from infrahub_sdk.protocols import CoreNode, CoreNodeSync, {{ base_protocols | join(', ') }}
1010

1111
if TYPE_CHECKING:
1212
{% if sync %}
@@ -111,5 +111,3 @@ class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or "Cor
111111
{% endif %}
112112
{% endif %}
113113
{% endfor %}
114-
115-
"""

0 commit comments

Comments
 (0)