gst-qa-system: Make insanityweb.runner.Runner into a proper singleton

Tim Müller tpm at kemper.freedesktop.org
Thu Sep 8 08:34:26 PDT 2011


Module: gst-qa-system
Branch: master
Commit: 678dc32566c258d7f5aa1a005b27a7a66ef1dc94
URL:    http://cgit.freedesktop.org/gstreamer/gst-qa-system/commit/?id=678dc32566c258d7f5aa1a005b27a7a66ef1dc94

Author: David Laban <david.laban at collabora.co.uk>
Date:   Thu Sep  8 13:04:06 2011 +0100

Make insanityweb.runner.Runner into a proper singleton

I'm not a fan of imports with side-effects (yes: this is why I hate the
python gstreamer bindings too).

---

 web/insanityweb/management/commands/daemon.py |    9 ++++-----
 web/insanityweb/runner.py                     |   13 +++++++++++--
 web/insanityweb/views.py                      |    7 ++++---
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/web/insanityweb/management/commands/daemon.py b/web/insanityweb/management/commands/daemon.py
index ccad625..918c0b8 100644
--- a/web/insanityweb/management/commands/daemon.py
+++ b/web/insanityweb/management/commands/daemon.py
@@ -10,16 +10,15 @@ import gobject
 import sys
 import os
 
-# Need to initialise it immediately because insanity.Client runs its
-# own mainloop, creates DBus service, etc. We also need to properly
-# quit it before exiting
-from insanityweb.runner import runner
+from insanityweb.runner import get_runner
+from settings import DATA_PATH
 
 class Command(BaseRunserverCommand):
     args = ''
     help = 'Start the Insanity integrated web + test runner'
 
     def run(self, *args, **options):
+        runner = get_runner()
         try:
             server = WSGIServer((self.addr, int(self.port)), WSGIRequestHandler)
         except WSGIServerException, e:
@@ -27,7 +26,7 @@ class Command(BaseRunserverCommand):
             runner.quit()
             return
 
-        # alidate models
+        # validate models
         sys.stdout.write("Validating models...\n")
         self.validate(display_num_errors=True)
 
diff --git a/web/insanityweb/runner.py b/web/insanityweb/runner.py
index c6fbe79..422ee23 100644
--- a/web/insanityweb/runner.py
+++ b/web/insanityweb/runner.py
@@ -11,7 +11,7 @@ from insanity.log import debug
 
 import gobject
 
-# Custom insanity test client. Monkey-patches the
+# Custom insanity test client. Extends the
 # insanity TesterClient to split the "stop test" and "quit
 # testing environment" into two separate methods (stop() and
 # quit(), respectively), so there's only one environment
@@ -86,7 +86,12 @@ class Client(TesterClient):
 
 class Runner(object):
 
+    _singleton = None
+
     def __init__(self):
+        assert Runner._singleton is None, "Please use get_runner()."
+        Runner._singleton = self
+
         self.client = Client(self)
         self._clear_info()
 
@@ -152,4 +157,8 @@ class Runner(object):
     def quit(self):
         self.client.quit()
 
-runner = Runner()
+def get_runner():
+    """Get (or create) the Runner singleton."""
+    if Runner._singleton is None:
+        Runner()
+    return Runner._singleton
diff --git a/web/insanityweb/views.py b/web/insanityweb/views.py
index 416b82a..75f3b26 100644
--- a/web/insanityweb/views.py
+++ b/web/insanityweb/views.py
@@ -5,7 +5,7 @@ from django.conf import settings
 import time
 from datetime import date
 
-from insanityweb.runner import runner
+from insanityweb.runner import get_runner
 
 from functools import wraps
 from django.http import HttpResponse
@@ -180,10 +180,11 @@ def render_to_json(**jsonargs):
 @render_to_json()
 def current_progress(request):
     return {
-        'progress': runner.get_progress()
+        'progress': get_runner().get_progress()
     }
 
 def current(request):
+    runner = get_runner()
     test_names = runner.get_test_names()
     test_folders = settings.INSANITY_TEST_FOLDERS.items()
 
@@ -203,5 +204,5 @@ def current(request):
 
 def stop_current(request):
     if 'submit' in request.POST:
-        runner.stop_test()
+        get_runner().stop_test()
     return redirect('web.insanityweb.views.current')



More information about the gstreamer-commits mailing list