| {{/* |
| // Copyright 2025 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| */}} |
| |
| {{ define "ProtocolDeclaration" -}} |
| from fidl._client import EventHandlerBase, FidlClient |
| from fidl._server import ServerBase |
| from fidl._fidl_common import ( |
| DomainError, FrameworkError, MethodInfo, FidlProtocolMarker, normalize_identifier |
| ) |
| |
| from abc import abstractmethod, ABC |
| |
| class {{ .PythonServerName }}(ServerBase, ABC): |
| {{- if .DocComments }} |
| """ |
| {{- range .DocComments }} |
| {{ . | escapeQuotes | trimSpace | indentNonEmpty4 }} |
| {{- end }} |
| """ |
| {{- end }} |
| __fidl_kind__ = "server" |
| library = "{{ .Library }}" |
| method_map: typing.Dict[int, MethodInfo] = { |
| {{- range .PythonMethods }} |
| {{- if .HasRequest -}} |
| {{ .Ordinal }}: MethodInfo( |
| name="{{ .PythonName }}", |
| request_ident="{{ .PythonRequestIdent }}", |
| requires_response={{ if .HasResponse }}True{{ else }}False{{ end }}, |
| empty_response={{ if .EmptyResponse }}True{{ else }}False{{ end }}, |
| has_result={{ if (or .HasFrameworkError .HasError) }}True{{ else }}False{{ end }}, |
| response_identifier="{{ .PythonResponseIdentifier }}", |
| ), |
| {{- end -}} |
| {{- end -}} |
| } |
| |
| @staticmethod |
| def construct_response_object( |
| response_ident: str, response_obj: typing.Any |
| ) -> typing.Any: |
| import fidl._construct |
| return fidl._construct.construct_response_object(response_ident, response_obj) |
| |
| {{ range .PythonMethods }} |
| {{ if .HasRequest -}} |
| {{ .PythonResponseAlias }}: typing.TypeAlias = {{ if (or .HasFrameworkError .HasError) }}typing.Union[ |
| {{- if .HasFrameworkError -}} |
| FrameworkError, |
| {{ end -}} |
| {{- if .HasError -}} |
| DomainError, |
| {{ end -}} |
| {{- if .PythonResponseSuccessPayload -}} |
| {{ .PythonResponseSuccessPayload.PythonType.PythonName }}, |
| {{ else -}} |
| {{ .PythonResponsePayload.PythonType.PythonName }}, |
| {{ end -}} |
| ]{{ else }} |
| {{- if .PythonResponsePayload }} |
| {{- .PythonResponsePayload.PythonType.PythonName -}} |
| {{- else -}} |
| None |
| {{- end }} |
| {{- end }} |
| |
| @abstractmethod |
| def {{ .PythonName }}( |
| self, |
| {{- if .PythonRequestPayload }} |
| request: {{ .PythonRequestPayload.PythonType.PythonName }}, |
| {{- end }} |
| ) -> typing.Coroutine[typing.Any, typing.Any, {{ .PythonResponseAlias }}] | {{ .PythonResponseAlias }}: |
| ... |
| {{ else -}} |
| def {{ .PythonName }}( |
| self, |
| {{- if and .PythonResponsePayload .PythonResponsePayload.PythonParameters }} |
| *, |
| {{- range .PythonResponsePayload.PythonParameters }} |
| {{ .PythonName }}: {{ .PythonType.PythonName }}{{- if .PythonNoneDefault }} | None=None{{- end }}, |
| {{- end }} |
| {{- end }} |
| ) -> None: |
| {{- if .PythonResponsePayload }} |
| {{- if .PythonResponsePayload.PythonParameters }} |
| msg = {{ .PythonResponsePayload.PythonType.PythonName }}( |
| {{ range .PythonResponsePayload.PythonParameters -}} |
| {{ .PythonName }}, |
| {{- end }} |
| ) |
| {{- else }} |
| msg = {{ .PythonResponsePayload.PythonType.PythonName }}() |
| {{- end }} |
| {{- else }} |
| msg = None |
| {{- end }} |
| self._send_event({{ .Ordinal }}, "{{ $.Library }}", msg) |
| {{ end -}} |
| {{- end }} |
| |
| class {{ .PythonClientName }}(FidlClient): |
| {{- if .DocComments }} |
| """ |
| {{- range .DocComments }} |
| {{ . | escapeQuotes | trimSpace | indentNonEmpty4 }} |
| {{- end }} |
| """ |
| {{- end }} |
| __fidl_kind__ = "client" |
| library = "{{ .Library }}" |
| |
| @staticmethod |
| def construct_response_object( |
| response_ident: str, response_obj: typing.Any |
| ) -> typing.Any: |
| import fidl._construct |
| return fidl._construct.construct_response_object(response_ident, response_obj) |
| |
| {{ range .PythonMethods -}} |
| {{- if .HasRequest }} |
| def {{ .PythonName }}( |
| self, |
| {{- if and .PythonRequestPayload .PythonRequestPayload.PythonParameters }} |
| *, |
| {{- range .PythonRequestPayload.PythonParameters }} |
| {{ .PythonName }}: {{ .PythonType.PythonName }}{{- if .PythonNoneDefault }} | None=None{{- end }}, |
| {{- end }} |
| {{- end }} |
| ) -> {{ if .HasResponse -}}typing.Coroutine[ |
| typing.Any, |
| typing.Any, |
| {{ if .PythonResponsePayload -}} |
| {{ .PythonResponsePayload.PythonType.PythonName }} |
| {{- else -}} |
| None |
| {{- end }} |
| ] |
| {{- else -}}None{{ end }}: |
| {{- if .PythonRequestPayload }} |
| {{- if .PythonRequestPayload.PythonParameters }} |
| msg = {{ .PythonRequestPayload.PythonType.PythonName }}( |
| {{ range .PythonRequestPayload.PythonParameters -}} |
| {{ .PythonName }}, |
| {{- end }} |
| ) |
| {{- else }} |
| msg = {{ .PythonRequestPayload.PythonType.PythonName }}() |
| {{- end }} |
| {{- else }} |
| msg = None |
| {{- end }} |
| {{ if .HasResponse -}} |
| {{- if .PythonResponsePayload -}} |
| return self._send_two_way_fidl_request( |
| {{ .Ordinal }}, "{{ $.Library }}", msg, normalize_identifier("{{ .PythonResponsePayload.PythonType.Identifier }}"), |
| ) |
| {{- else -}} |
| return self._send_two_way_fidl_request( |
| {{ .Ordinal }}, "{{ $.Library }}", msg, "", |
| ) |
| {{- end -}} |
| {{- else }} |
| self._send_one_way_fidl_request( |
| 0, {{ .Ordinal }}, "{{ $.Library }}", msg, |
| ) |
| {{- end }} |
| {{ end -}} |
| {{- end }} |
| |
| |
| class {{ .PythonEventHandlerName }}(EventHandlerBase, ABC): |
| {{- if .DocComments }} |
| """ |
| {{- range .DocComments }} |
| {{ . | escapeQuotes | trimSpace | indentNonEmpty4 }} |
| {{- end }} |
| """ |
| {{- end }} |
| __fidl_kind__ = "event_handler" |
| library = "{{ .Library }}" |
| method_map: typing.Dict[int, MethodInfo] = { |
| {{- range .PythonMethods }} |
| {{- if not .HasRequest -}} |
| {{ .Ordinal }}: MethodInfo( |
| name="{{ .PythonName }}", |
| request_ident="{{ .PythonResponseIdentifier }}", |
| requires_response=False, |
| empty_response=False, |
| has_result=False, |
| response_identifier=None, |
| ), |
| {{- end -}} |
| {{- end -}} |
| } |
| |
| @staticmethod |
| def construct_response_object( |
| response_ident: str, response_obj: typing.Any |
| ) -> typing.Any: |
| import fidl._construct |
| return fidl._construct.construct_response_object(response_ident, response_obj) |
| |
| {{ range .PythonMethods -}} |
| {{- if not .HasRequest }} |
| @abstractmethod |
| def {{ .PythonName }}( |
| self, |
| {{- if .PythonResponsePayload }} |
| request: {{ .PythonResponsePayload.PythonType.PythonName }}, |
| {{- end }} |
| ) -> typing.Coroutine[typing.Any, typing.Any, None] | None: |
| ... |
| {{ end -}} |
| {{- end }} |
| |
| |
| {{ .PythonMarkerName }} = FidlProtocolMarker("{{ .Marker }}") |
| |
| |
| {{ end }} |