From f87f5f25cf9d78f06082cd6b15718e034792f3d0 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sat, 18 Jun 2011 14:09:27 -0400 Subject: [PATCH] libsaria: Run server based on stored stetting I have a toggle_state() function to toggle the server and save the state for the next session. --- libsaria/__init__.py | 8 ++------ libsaria/server/__init__.py | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/libsaria/__init__.py b/libsaria/__init__.py index 1addd497..4560c606 100644 --- a/libsaria/__init__.py +++ b/libsaria/__init__.py @@ -7,10 +7,7 @@ import path import sources import controls - -server = None -if version.__dev__ == True: - import server +import server from path import lastfm #plugin = None @@ -27,8 +24,7 @@ def startup(): def shutdown(): audio.shutdown() - if server != None: - server.shutdown() + server.shutdown() ## import plugin ## plugin.quit() diff --git a/libsaria/server/__init__.py b/libsaria/server/__init__.py index 90319370..de383f9c 100644 --- a/libsaria/server/__init__.py +++ b/libsaria/server/__init__.py @@ -3,19 +3,39 @@ from BaseHTTPServer import HTTPServer import threading +from libsaria import prefs import request server = None +PREF_KEY="libsaria.server.enabled" +prefs.init(PREF_KEY, False) def setup_server(): global server + if prefs.get(PREF_KEY) == False: + return try: server = HTTPServer(('', 4242), request.Handler) server.serve_forever() except Exception,e: print "Error starting server:", e -threading.Thread(target=setup_server).start() + +def startup(): + threading.Thread(target=setup_server).start() +startup() def shutdown(): + global server if server != None: server.shutdown() + server = None + +def get_state(): + return prefs.get(PREF_KEY) + +def toggle_state(active): + prefs.set(PREF_KEY, active) + if active == True: + startup() + else: + shutdown()