diff --git a/libsaria/__init__.py b/libsaria/__init__.py index 7043576b..6ca9cf47 100644 --- a/libsaria/__init__.py +++ b/libsaria/__init__.py @@ -6,6 +6,9 @@ import audio import path +if version.__dev__ == True: + import server + #plugin = None lastfm = None controls = None @@ -36,6 +39,8 @@ def startup(): def shutdown(): audio.shutdown() + if version.__dev__ == True: + server.shutdown() ## import plugin ## plugin.quit() diff --git a/libsaria/server/__init__.py b/libsaria/server/__init__.py new file mode 100644 index 00000000..08ab8748 --- /dev/null +++ b/libsaria/server/__init__.py @@ -0,0 +1,17 @@ +# Bryan Schumaker (5 / 15 / 2011) + +from BaseHTTPServer import HTTPServer +import threading + +import request + +server = None + +def setup_server(): + global server + server = HTTPServer(('', 4242), request.Handler) + server.serve_forever() +threading.Thread(target=setup_server).start() + +def shutdown(): + server.shutdown() diff --git a/libsaria/server/files.py b/libsaria/server/files.py new file mode 100644 index 00000000..5d4fae9d --- /dev/null +++ b/libsaria/server/files.py @@ -0,0 +1,15 @@ +# Bryan Schumaker (5 / 15 / 2011) + +import pages + +def format_path(path): + if path == "/": + path = "/index.html" + path = path.split("/") + return [cmp for cmp in path if cmp != ''] + +def find_file(path): + return pages.has_file(path) + +def open_file(path): + return pages.page_text(path) diff --git a/libsaria/server/pages/__init__.py b/libsaria/server/pages/__init__.py new file mode 100644 index 00000000..0852f0b5 --- /dev/null +++ b/libsaria/server/pages/__init__.py @@ -0,0 +1,26 @@ +# Bryan Schumaker (5 / 15 / 2011) + +import root + +docs = {} +docs.update(root.docs) + +types = { + "html":"text/html" +} + +def lookup(file): + doc = docs + print doc, file, doc.get(file[0], None) + for cmp in file: + doc = doc.get(cmp, None) + if doc == None: + break + return doc + +def has_file(file): + return lookup(file) != None + +def page_text(file): + func, type = lookup(file) + return func(), types[type] diff --git a/libsaria/server/pages/root.py b/libsaria/server/pages/root.py new file mode 100644 index 00000000..4d3e49ee --- /dev/null +++ b/libsaria/server/pages/root.py @@ -0,0 +1,11 @@ +# Bryan Schumaker (5 / 15 / 2011) + +def index(): + text = " \ + Hello, world! \ + " + return text + +docs = { + "index.html":(index, "html") +} diff --git a/libsaria/server/request.py b/libsaria/server/request.py new file mode 100644 index 00000000..aa711a8f --- /dev/null +++ b/libsaria/server/request.py @@ -0,0 +1,17 @@ +# Bryan Schumaker (5 / 15 / 2011) + +from BaseHTTPServer import BaseHTTPRequestHandler +import files + +class Handler(BaseHTTPRequestHandler): + def do_GET(self): + path = files.format_path(self.path) + if not files.find_file(path): + self.send_error(404, "File Not Found: %s" % path) + return + text, type = files.open_file(path) + + self.send_response(200) + self.send_header('Content-type', type) + self.end_headers() + self.wfile.write(text)