Chromium Code Reviews| Index: examples/python/__mojo__.py |
| diff --git a/examples/python/__mojo__.py b/examples/python/__mojo__.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..946f8d37c4e98d9a6bfa2e53a7ccc83713912ba5 |
| --- /dev/null |
| +++ b/examples/python/__mojo__.py |
| @@ -0,0 +1,61 @@ |
| +# Copyright 2015 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Example python application implementing the Echo service.""" |
| + |
| +import application_mojom |
| +import shell_mojom |
| +import service_provider_mojom |
| +import example_service_mojom |
|
qsr
2015/01/12 17:37:45
Alphabetize
etiennej
2015/01/14 00:54:04
Done.
|
| + |
| +import mojo_bindings.reflection |
|
qsr
2015/01/12 17:37:44
This is (luckily) not used. We do not really want
etiennej
2015/01/14 00:54:04
Done.
|
| +import mojo_system |
| + |
| +class ApplicationImpl(application_mojom.Application): |
| + def __init__(self): |
| + self._providers = [] |
| + |
| + def Initialize(self, args): |
| + pass |
| + |
| + def AcceptConnection(self, requestor_url, provider): |
| + # We keep a reference to ServiceProviderImpl to ensure neither it nor |
| + # provider gets garbage collected. |
| + service_provider = ServiceProviderImpl(provider) |
| + service_provider.AddService("mojo::ExampleService", |
|
qsr
2015/01/12 17:37:45
Isn't this available somewhere from the example_se
etiennej
2015/01/14 00:54:04
Done.
|
| + PyServiceImpl) |
| + provider.client = service_provider |
| + self._providers.append(provider) |
| + |
| + |
| +class ServiceProviderImpl(service_provider_mojom.ServiceProvider): |
| + def __init__(self, provider): |
| + self._provider = provider |
| + self._name_to_service_connector = {} |
| + |
| + def AddService(self, name, service_class): |
| + self._name_to_service_connector[name] = service_class |
| + |
| + def ConnectToService(self, interface_name, pipe): |
| + if interface_name in self._name_to_service_connector: |
| + service = self._name_to_service_connector[interface_name] |
| + service.manager.Bind(service(), pipe) |
|
qsr
2015/01/12 17:37:45
Maybe some logging if the class is not registered.
etiennej
2015/01/14 00:54:04
Done.
|
| + |
| + |
| +class PyServiceImpl(example_service_mojom.ExampleService): |
|
qsr
2015/01/12 17:37:44
What about calling this ExampleServiceImpl or some
etiennej
2015/01/14 00:54:03
Done.
|
| + def Ping(self, ping_value): |
| + self.client.Pong(ping_value) |
| + |
| + def RunCallback(self): |
| + return {} |
| + |
| +def MojoMain(shell_handle): |
| + """MojoMain is the entry point for a python Mojo module.""" |
| + loop = mojo_system.RunLoop() |
| + |
| + shell = shell_mojom.Shell.manager.Proxy(mojo_system.Handle(shell_handle)) |
| + shell.client = ApplicationImpl() |
| + shell.manager.AddOnErrorCallback(loop.Quit) |
| + |
| + loop.Run() |