Skip to content

Commit 2c47873

Browse files
committed
x
1 parent 3d2adab commit 2c47873

4 files changed

Lines changed: 61 additions & 53 deletions

File tree

alt_core_api/tools/protoc_gen_arduinoif/common.py

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import types
88
from functools import lru_cache
99
from pathlib import Path
10-
from typing import List, NamedTuple, Tuple
10+
from typing import TYPE_CHECKING, List, NamedTuple, Tuple
1111

1212
from google.protobuf.descriptor_pb2 import EnumDescriptorProto, ServiceDescriptorProto
1313

@@ -17,46 +17,12 @@
1717
"full_service_name",
1818
"get_arduino_opts_pb2",
1919
"ServiceDescriptor",
20-
"OptionsView",
2120
"MethodSpec",
22-
"PlannedMethod",
2321
"ServiceModel",
2422
]
2523

26-
27-
class OptionsView:
28-
def __init__(self, options) -> None:
29-
self._options = options
30-
31-
def has(self, extension) -> bool:
32-
try:
33-
return self._options.HasExtension(extension)
34-
except (AttributeError, KeyError):
35-
return False
36-
37-
def string(self, extension) -> str:
38-
if not self.has(extension):
39-
return ""
40-
return str(self._options.Extensions[extension])
41-
42-
def string_list(self, extension) -> List[str]:
43-
try:
44-
values = self._options.Extensions[extension]
45-
except (AttributeError, KeyError):
46-
return []
47-
return [text for text in (str(value).strip() for value in values) if text]
48-
49-
def bool(self, extension, default: bool = False) -> bool:
50-
if not self.has(extension):
51-
return default
52-
return bool(self._options.Extensions[extension])
53-
54-
class PlannedMethod(NamedTuple):
55-
spec: MethodSpec
56-
in_ifc: bool
57-
in_api: bool
58-
in_service: bool
59-
in_service_impl: bool
24+
if TYPE_CHECKING:
25+
from .service_model_builder import _PlannedMethod
6026

6127

6228
class ServiceModel(NamedTuple):
@@ -76,7 +42,7 @@ class ServiceModel(NamedTuple):
7642
api_header: str
7743
service_header: str
7844
service_impl_header: str
79-
methods: List[PlannedMethod]
45+
methods: List["_PlannedMethod"]
8046
generate_api: bool
8147
generate_service: bool
8248
generate_service_impl: bool

alt_core_api/tools/protoc_gen_arduinoif/service_model_builder.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
from .common import (
1111
MethodSpec,
12-
OptionsView,
13-
PlannedMethod,
1412
ServiceModel,
1513
full_service_name,
1614
get_arduino_opts_pb2,
@@ -22,6 +20,42 @@
2220
]
2321

2422

23+
class _OptionsView:
24+
def __init__(self, options) -> None:
25+
self._options = options
26+
27+
def has(self, extension) -> bool:
28+
try:
29+
return self._options.HasExtension(extension)
30+
except (AttributeError, KeyError):
31+
return False
32+
33+
def string(self, extension) -> str:
34+
if not self.has(extension):
35+
return ""
36+
return str(self._options.Extensions[extension])
37+
38+
def string_list(self, extension) -> List[str]:
39+
try:
40+
values = self._options.Extensions[extension]
41+
except (AttributeError, KeyError):
42+
return []
43+
return [text for text in (str(value).strip() for value in values) if text]
44+
45+
def bool(self, extension, default: bool = False) -> bool:
46+
if not self.has(extension):
47+
return default
48+
return bool(self._options.Extensions[extension])
49+
50+
51+
class _PlannedMethod(NamedTuple):
52+
spec: MethodSpec
53+
in_ifc: bool
54+
in_api: bool
55+
in_service: bool
56+
in_service_impl: bool
57+
58+
2559
class ServiceModelBuilder:
2660
class _ResolvedServiceOptions(NamedTuple):
2761
ifc_name: str
@@ -58,7 +92,7 @@ def build(
5892
return cls(service, package_name, proto_enums, context)._build()
5993

6094
def _build(self) -> ServiceModel:
61-
service_opts = OptionsView(self._service.options)
95+
service_opts = _OptionsView(self._service.options)
6296

6397
ifc_header = (
6498
service_opts.string(self._opts_pb2.ifc_header_name).strip()
@@ -134,7 +168,7 @@ def _build(self) -> ServiceModel:
134168
base_ifc_headers: List[str] = []
135169
for ancestor_full_name in lineage[:-1]:
136170
ancestor_service, _ = self._context.service_index[ancestor_full_name]
137-
ancestor_opts = OptionsView(ancestor_service.options)
171+
ancestor_opts = _OptionsView(ancestor_service.options)
138172
base_ifc_names.append(
139173
ancestor_opts.string(self._opts_pb2.ifc_class_name).strip()
140174
or f"{ancestor_service.name}Interface"
@@ -269,7 +303,7 @@ def _collect_service_lineage(
269303

270304
service, package_name = entry
271305
inherited: List[str] = []
272-
for base_ref in OptionsView(service.options).string_list(
306+
for base_ref in _OptionsView(service.options).string_list(
273307
self._opts_pb2.base_services
274308
):
275309
base_full_name = self._resolve_service_reference(base_ref, package_name)
@@ -309,7 +343,7 @@ def _collect_lineage_includes(self, lineage: List[str]) -> List[str]:
309343
for service_full_name in lineage:
310344
service, _ = self._context.service_index[service_full_name]
311345
includes.extend(
312-
OptionsView(service.options).string_list(self._opts_pb2.extra_includes)
346+
_OptionsView(service.options).string_list(self._opts_pb2.extra_includes)
313347
)
314348
return includes
315349

@@ -338,12 +372,12 @@ def _build_planned_methods(
338372
lineage_method_specs,
339373
own_virtual_decls,
340374
service_impl_callable,
341-
) -> List[PlannedMethod]:
342-
methods: List[PlannedMethod] = []
375+
) -> List[_PlannedMethod]:
376+
methods: List[_PlannedMethod] = []
343377
for spec in lineage_method_specs:
344378
in_service = spec.emit_service
345379
methods.append(
346-
PlannedMethod(
380+
_PlannedMethod(
347381
spec=spec,
348382
in_ifc=spec.decl in own_virtual_decls,
349383
in_api=spec.emit_api and spec.source_virtual,

alt_core_api/tools/protoc_gen_arduinoif/tests/test_renderer_templates.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import sys
66
from pathlib import Path
7+
from typing import NamedTuple
78

89
from google.protobuf.descriptor_pb2 import EnumDescriptorProto
910

@@ -12,12 +13,19 @@
1213

1314
from protoc_gen_arduinoif.common import ( # noqa: E402
1415
MethodSpec,
15-
PlannedMethod,
1616
ServiceModel,
1717
)
1818
from protoc_gen_arduinoif.service_renderer import ServiceRenderer # noqa: E402
1919

2020

21+
class _PlannedMethod(NamedTuple):
22+
spec: MethodSpec
23+
in_ifc: bool
24+
in_api: bool
25+
in_service: bool
26+
in_service_impl: bool
27+
28+
2129
def _sample_model() -> ServiceModel:
2230
enum_desc = EnumDescriptorProto(name="Mode")
2331
enum_desc.value.add(name="MODE_A", number=0)
@@ -54,9 +62,9 @@ def _sample_model() -> ServiceModel:
5462
)
5563

5664
methods = [
57-
PlannedMethod(foo_spec, True, True, True, True),
58-
PlannedMethod(bar_spec, True, True, True, True),
59-
PlannedMethod(baz_spec, True, True, True, True),
65+
_PlannedMethod(foo_spec, True, True, True, True),
66+
_PlannedMethod(bar_spec, True, True, True, True),
67+
_PlannedMethod(baz_spec, True, True, True, True),
6068
]
6169

6270
return ServiceModel(

documentation/idl_codegen.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ Only `RequestContext` is global. Service models are built and emitted one-by-one
4040
Module visibility is intentionally constrained:
4141

4242
1. `request_context.py` exposes `RequestContext`.
43-
2. `common.py` exposes shared utilities/types (`full_service_name`, `get_arduino_opts_pb2`, `ServiceModel`, `MethodSpec`, `PlannedMethod`).
44-
3. `service_model_builder.py` exposes `ServiceModelBuilder` as the model-construction entry point.
43+
2. `common.py` exposes shared utilities/types (`full_service_name`, `get_arduino_opts_pb2`, `ServiceModel`, `MethodSpec`).
44+
3. `service_model_builder.py` exposes `ServiceModelBuilder`; helper types used there are private.
4545
4. `service_renderer.py` exposes `ServiceRenderer` for header rendering.
4646

4747
Typical generated header names (per service):

0 commit comments

Comments
 (0)