From c02bffb6526a91e98e88ab794c1649dc96af17e1 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sun, 15 May 2011 15:39:14 -0400 Subject: [PATCH] libsaria: Web server controls playback I can choose songs to play by clicking rows in an html table. I also write text to the client as it becomes available, rather than doing it all at once. --- html/controls.js | 7 +++++- html/index.html | 14 +++++------ libsaria/server/files.py | 7 ++++-- libsaria/server/pages/__init__.py | 23 ++++++++++++----- libsaria/server/pages/root.py | 41 +++++++++++++++++++++++++++---- libsaria/server/pages/rpc.py | 14 ++++++++--- libsaria/server/request.py | 6 +++-- 7 files changed, 85 insertions(+), 27 deletions(-) diff --git a/html/controls.js b/html/controls.js index 77af1b7d..fcb74eb9 100644 --- a/html/controls.js +++ b/html/controls.js @@ -2,7 +2,7 @@ function control(action) { - send_request("controls.py?a=" + action); + send_request("RPC/" + action); } function play() @@ -39,3 +39,8 @@ function rewind() { control("rewind"); } + +function play_id(id) +{ + control("play_id/" + id) +} diff --git a/html/index.html b/html/index.html index 2f3712f2..f2434f13 100644 --- a/html/index.html +++ b/html/index.html @@ -1,20 +1,20 @@ - - + - - + +
Library Browser
Library Browser 2
Library Browser
---> diff --git a/libsaria/server/files.py b/libsaria/server/files.py index 5d4fae9d..5e1d60be 100644 --- a/libsaria/server/files.py +++ b/libsaria/server/files.py @@ -11,5 +11,8 @@ def format_path(path): def find_file(path): return pages.has_file(path) -def open_file(path): - return pages.page_text(path) +def get_type(path): + return pages.file_type(path) + +def send_file(write, path): + pages.page_text(write, path) diff --git a/libsaria/server/pages/__init__.py b/libsaria/server/pages/__init__.py index ef80baa9..801639b3 100644 --- a/libsaria/server/pages/__init__.py +++ b/libsaria/server/pages/__init__.py @@ -9,19 +9,30 @@ docs.update(rpc.docs) types = { "html":"text/html", + "js":"text/javascript", } def lookup(file): doc = docs - for cmp in file: + for i, cmp in enumerate(file): doc = doc.get(cmp, None) if doc == None: break - return doc + elif len(doc) == 2: + break + return doc, i + 1 def has_file(file): - return lookup(file) != None + doc, index = lookup(file) + return doc != None -def page_text(file): - func, type = lookup(file) - return func(), types[type] +def file_type(file): + (func, type), index = lookup(file) + return type + +def page_text(write, file): + (func, type), index = lookup(file) + if len(file) > index: + res = func(write, file[index:]) + else: + res = func(write) diff --git a/libsaria/server/pages/root.py b/libsaria/server/pages/root.py index 50bda57d..9a8308d8 100644 --- a/libsaria/server/pages/root.py +++ b/libsaria/server/pages/root.py @@ -1,11 +1,42 @@ # Bryan Schumaker (5 / 15 / 2011) -def index(): - text = " \ - Hello, world! \ - " - return text +from libsaria import sources + +SCRIPT = "" + +def write_file(write, file): + f = open(file) + write(f.read()) + f.close() + +def index(write): + write_file(write, "html/index.html") + +def library(write): + write("") + write(SCRIPT % "controls.js") + write(SCRIPT % "utils.js") + write("\n") + color = "white" + for id, title, artist, album in sources.library.walk_library("id", "title", "artist", "album"): + write("" % (color, id)) + write("" % (id, id, title)) + write("\n" % (artist, album)) + if color == "white": + color = "aliceblue" + else: + color = "white" + write("
%s%s%s
") + +def controls(write): + write_file(write, "html/controls.js") + +def utils(write): + write_file(write, "html/utils.js") docs = { "index.html":(index, "html"), + "library.html":(library, "html"), + "controls.js":(controls, "js"), + "utils.js":(utils, "js"), } diff --git a/libsaria/server/pages/rpc.py b/libsaria/server/pages/rpc.py index 43f924fc..25dfb73b 100644 --- a/libsaria/server/pages/rpc.py +++ b/libsaria/server/pages/rpc.py @@ -1,18 +1,24 @@ # Bryan Schumaker (5 / 15 / 2011) +from libsaria import sources from libsaria import controls -def play(): +def play(write): controls.play() - return "" + write("") -def pause(): +def pause(write): controls.pause() - return "" + write("") + +def play_id(write, id): + sources.play_id(long(id[0])) + write("") rpc = { "play.html":(play, "html"), "pause.html":(pause, "html"), + "play_id":(play_id, "html"), } docs = { diff --git a/libsaria/server/request.py b/libsaria/server/request.py index aa711a8f..12a67cdf 100644 --- a/libsaria/server/request.py +++ b/libsaria/server/request.py @@ -9,9 +9,11 @@ class Handler(BaseHTTPRequestHandler): if not files.find_file(path): self.send_error(404, "File Not Found: %s" % path) return - text, type = files.open_file(path) + type = files.get_type(path) self.send_response(200) self.send_header('Content-type', type) self.end_headers() - self.wfile.write(text) + + files.send_file(self.wfile.write, path) + self.wfile.close()