From d98c46eb657e52c3380caa9c1c3a5e23f8d1bc03 Mon Sep 17 00:00:00 2001 From: Ryan Northey Date: Sun, 18 Mar 2012 16:04:15 +0000 Subject: [PATCH 1/2] Added example of a twistd app --- examples/frameworks/twisted/twistd_app.py | 56 +++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 examples/frameworks/twisted/twistd_app.py diff --git a/examples/frameworks/twisted/twistd_app.py b/examples/frameworks/twisted/twistd_app.py new file mode 100644 index 000000000..f80b66674 --- /dev/null +++ b/examples/frameworks/twisted/twistd_app.py @@ -0,0 +1,56 @@ +from kivy.support import install_twisted_reactor +install_twisted_reactor() + +import os, sys + +from kivy.app import App +from kivy.uix.gridlayout import GridLayout +from kivy.uix.button import Button +from kivy.properties import BooleanProperty +from kivy.lang import Builder + +from twisted.scripts._twistd_unix import UnixApplicationRunner, ServerOptions +from twisted.application.service import IServiceCollection + +TWISTD = 'twistd web -p 8087' + +class AndroidApplicationRunner(UnixApplicationRunner): + def run(self): + self.preApplication() + self.application = self.createOrGetApplication() + self.logger.start(self.application) + sc = IServiceCollection(self.application) + + # reactor is already running, so we just start the service collection + sc.startService() + return self.application + +Builder.load_string(''' +: + cols: 1 + Button: + text: root.running and 'STOP' or 'START' + on_release: root.cb_twistd() +''') + +class TwistedTwistdWeb(GridLayout): + running = BooleanProperty(False) + def cb_twistd(self,*la): + if self.running: + IServiceCollection(self.app).stopService() + self.running = False + else: + sys.exc_clear() + sys.path.insert(0, os.path.abspath(os.getcwd())) + sys.argv = TWISTD.split(' ') + config = ServerOptions() + config.parseOptions() + self.app = AndroidApplicationRunner(config).run() + self.running = True + +class TwistedTwistdWebApp(App): + def build(self): + return TwistedTwistdWeb() + +if __name__ == '__main__': + TwistedTwistdWebApp().run() From 292f99048fa42154bf62ff9baae11c9fe4373344 Mon Sep 17 00:00:00 2001 From: Ryan Northey Date: Sun, 18 Mar 2012 16:09:19 +0000 Subject: [PATCH 2/2] Improved class names --- examples/frameworks/twisted/twistd_app.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/frameworks/twisted/twistd_app.py b/examples/frameworks/twisted/twistd_app.py index f80b66674..77415167b 100644 --- a/examples/frameworks/twisted/twistd_app.py +++ b/examples/frameworks/twisted/twistd_app.py @@ -26,14 +26,14 @@ class AndroidApplicationRunner(UnixApplicationRunner): return self.application Builder.load_string(''' -: +: cols: 1 Button: text: root.running and 'STOP' or 'START' on_release: root.cb_twistd() ''') -class TwistedTwistdWeb(GridLayout): +class TwistedTwistd(GridLayout): running = BooleanProperty(False) def cb_twistd(self,*la): if self.running: @@ -48,9 +48,9 @@ class TwistedTwistdWeb(GridLayout): self.app = AndroidApplicationRunner(config).run() self.running = True -class TwistedTwistdWebApp(App): +class TwistedTwistdApp(App): def build(self): - return TwistedTwistdWeb() + return TwistedTwistd() if __name__ == '__main__': - TwistedTwistdWebApp().run() + TwistedTwistdApp().run()