| {{/* | |
| // 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" -}} | |
| 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 -}} | |
| } | |
| {{ 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 }} | |
| ) -> collections.abc.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 }}" | |
| {{ 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 -}}collections.abc.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 -}} | |
| ret = self._send_two_way_fidl_request( | |
| {{ .Ordinal }}, "{{ $.Library }}", msg, {{ .PythonResponsePayload.PythonType.PythonName }}, | |
| ) | |
| return ret | |
| {{- else -}} | |
| ret = self._send_two_way_fidl_request( | |
| {{ .Ordinal }}, "{{ $.Library }}", msg, None, | |
| ) | |
| return ret | |
| {{- 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 -}} | |
| } | |
| {{ range .PythonMethods -}} | |
| {{- if not .HasRequest }} | |
| @abstractmethod | |
| def {{ .PythonName }}( | |
| self, | |
| {{- if .PythonResponsePayload }} | |
| request: {{ .PythonResponsePayload.PythonType.PythonName }}, | |
| {{- end }} | |
| ) -> collections.abc.Coroutine[typing.Any, typing.Any, None] | None: | |
| ... | |
| {{ end -}} | |
| {{- end }} | |
| {{ .PythonMarkerName }} = FidlProtocolMarker("{{ .Marker }}") | |
| {{ range .PythonMethods }} | |
| fidl.register_method( | |
| {{ .Ordinal }}, | |
| {{ if .PythonRequestPayload }}{{ .PythonRequestPayload.PythonType.PythonName }}{{ else }}None{{ end }}, | |
| {{ if .PythonResponsePayload }}{{ .PythonResponsePayload.PythonType.PythonName }}{{ else }}None{{ end }}, | |
| ) | |
| {{- end }} | |
| {{ end }} |