|
9 | 9 |
|
10 | 10 | from .common import ( |
11 | 11 | MethodSpec, |
12 | | - OptionsView, |
13 | | - PlannedMethod, |
14 | 12 | ServiceModel, |
15 | 13 | full_service_name, |
16 | 14 | get_arduino_opts_pb2, |
|
22 | 20 | ] |
23 | 21 |
|
24 | 22 |
|
| 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 | + |
25 | 59 | class ServiceModelBuilder: |
26 | 60 | class _ResolvedServiceOptions(NamedTuple): |
27 | 61 | ifc_name: str |
@@ -58,7 +92,7 @@ def build( |
58 | 92 | return cls(service, package_name, proto_enums, context)._build() |
59 | 93 |
|
60 | 94 | def _build(self) -> ServiceModel: |
61 | | - service_opts = OptionsView(self._service.options) |
| 95 | + service_opts = _OptionsView(self._service.options) |
62 | 96 |
|
63 | 97 | ifc_header = ( |
64 | 98 | service_opts.string(self._opts_pb2.ifc_header_name).strip() |
@@ -134,7 +168,7 @@ def _build(self) -> ServiceModel: |
134 | 168 | base_ifc_headers: List[str] = [] |
135 | 169 | for ancestor_full_name in lineage[:-1]: |
136 | 170 | ancestor_service, _ = self._context.service_index[ancestor_full_name] |
137 | | - ancestor_opts = OptionsView(ancestor_service.options) |
| 171 | + ancestor_opts = _OptionsView(ancestor_service.options) |
138 | 172 | base_ifc_names.append( |
139 | 173 | ancestor_opts.string(self._opts_pb2.ifc_class_name).strip() |
140 | 174 | or f"{ancestor_service.name}Interface" |
@@ -269,7 +303,7 @@ def _collect_service_lineage( |
269 | 303 |
|
270 | 304 | service, package_name = entry |
271 | 305 | inherited: List[str] = [] |
272 | | - for base_ref in OptionsView(service.options).string_list( |
| 306 | + for base_ref in _OptionsView(service.options).string_list( |
273 | 307 | self._opts_pb2.base_services |
274 | 308 | ): |
275 | 309 | 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]: |
309 | 343 | for service_full_name in lineage: |
310 | 344 | service, _ = self._context.service_index[service_full_name] |
311 | 345 | includes.extend( |
312 | | - OptionsView(service.options).string_list(self._opts_pb2.extra_includes) |
| 346 | + _OptionsView(service.options).string_list(self._opts_pb2.extra_includes) |
313 | 347 | ) |
314 | 348 | return includes |
315 | 349 |
|
@@ -338,12 +372,12 @@ def _build_planned_methods( |
338 | 372 | lineage_method_specs, |
339 | 373 | own_virtual_decls, |
340 | 374 | service_impl_callable, |
341 | | - ) -> List[PlannedMethod]: |
342 | | - methods: List[PlannedMethod] = [] |
| 375 | + ) -> List[_PlannedMethod]: |
| 376 | + methods: List[_PlannedMethod] = [] |
343 | 377 | for spec in lineage_method_specs: |
344 | 378 | in_service = spec.emit_service |
345 | 379 | methods.append( |
346 | | - PlannedMethod( |
| 380 | + _PlannedMethod( |
347 | 381 | spec=spec, |
348 | 382 | in_ifc=spec.decl in own_virtual_decls, |
349 | 383 | in_api=spec.emit_api and spec.source_virtual, |
|
0 commit comments