# HG changeset patch # User paulb # Date 1124399409 0 # Node ID cb6c38a13971283947cce72ef2415a0855e8b1eb # Parent 8858fbc7a88ff7a77bd7329fb8f799d58eee768e [project @ 2005-08-18 21:09:56 by paulb] Fixed the calendar example (and added missing deployment programs). diff -r 8858fbc7a88f -r cb6c38a13971 examples/BaseHTTPRequestHandler/CalendarApp.py --- a/examples/BaseHTTPRequestHandler/CalendarApp.py Thu Aug 18 21:09:33 2005 +0000 +++ b/examples/BaseHTTPRequestHandler/CalendarApp.py Thu Aug 18 21:10:09 2005 +0000 @@ -1,9 +1,9 @@ #!/usr/bin/env python from WebStack.Adapters.BaseHTTPRequestHandler import deploy -from Calendar import DirectoryResource +from Calendar import CalendarResource print "Serving..." -deploy(DirectoryResource()) +deploy(CalendarResource()) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8858fbc7a88f -r cb6c38a13971 examples/BaseHTTPRequestHandler/DemoApp.py --- a/examples/BaseHTTPRequestHandler/DemoApp.py Thu Aug 18 21:09:33 2005 +0000 +++ b/examples/BaseHTTPRequestHandler/DemoApp.py Thu Aug 18 21:10:09 2005 +0000 @@ -18,6 +18,7 @@ from Simple import SimpleResource from Unicode import UnicodeResource from VerySimple import VerySimpleResource +from Calendar import CalendarResource # A very simple index page. @@ -41,6 +42,8 @@
You can run all of the examples independently - see the documentation in
the docs
directory, especially the subdirectories for each
@@ -59,6 +62,7 @@
"simple" : SimpleResource(),
"unicode" : UnicodeResource(),
"verysimple" : VerySimpleResource(),
+ "calendar" : CalendarResource(),
"" : DemoResource(),
})
diff -r 8858fbc7a88f -r cb6c38a13971 examples/CGI/CalendarHandler.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/CGI/CalendarHandler.py Thu Aug 18 21:10:09 2005 +0000
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+
+# NOTE: Path manipulation requires manual customisation.
+
+import sys
+sys.path.append("/home/paulb/Software/Python/WebStack")
+sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common")
+
+from WebStack.Adapters.CGI import deploy
+from Calendar import CalendarResource
+
+deploy(CalendarResource())
+
+# vim: tabstop=4 expandtab shiftwidth=4
diff -r 8858fbc7a88f -r cb6c38a13971 examples/Common/Calendar/__init__.py
--- a/examples/Common/Calendar/__init__.py Thu Aug 18 21:09:33 2005 +0000
+++ b/examples/Common/Calendar/__init__.py Thu Aug 18 21:10:09 2005 +0000
@@ -4,8 +4,9 @@
import WebStack.Generic
import time
+import os
-class DirectoryResource:
+class CalendarResource:
"""
A resource which handles incoming calendars and viewing requests.
@@ -14,6 +15,12 @@
calendars, yet also accepts incoming calendars.
"""
+ resource_dir = os.path.join(os.path.split(__file__)[0], "calendars")
+
+ def __init__(self):
+ if not os.path.exists(self.resource_dir):
+ os.mkdir(self.resource_dir)
+
def respond(self, trans):
"""
@@ -30,8 +37,10 @@
method = trans.get_request_method()
# NOTE: Some frameworks do not pass in the content type.
+ # NOTE: We always assume that calendar files are being uploaded.
content_type = trans.get_content_type()
+ calendar_name = trans.get_virtual_path_info().split("/")[-1]
# Handle uploads.
@@ -40,17 +49,14 @@
# Get the last path component as the name of the calendar.
# NOTE: This could be improved to permit hierarchical naming.
- calendar_name = trans.get_path_without_query().split("/")[-1]
input = trans.get_request_stream()
data = input.read()
- # Store the calendar in the session.
+ # Store the calendar in the directory.
- session["calendar name"] = calendar_name
- session["media type"] = content_type.media_type
- session["calendar data"] = data
- session["calendar size"] = len(data)
- session["calendar time"] = time.strftime("%Y-%m-%dT%T")
+ f = open(os.path.join(self.resource_dir, calendar_name), "wb")
+ f.write(data)
+ f.close()
# Handle directory browsing.
@@ -62,7 +68,8 @@
Media type: %s
-Calendar size: %s
-%s- - -""" % (session.get("calendar name"), session.get("media type"), session.get("calendar size"), - session.get("calendar data", "").replace("&", "&").replace("<", "<").replace(">", ">"))) + f = open(os.path.join(self.resource_dir, calendar_name)) + out.write(f.read()) + f.close() + + # Handle deletion. + + elif method == "DELETE": + try: + os.remove(os.path.join(self.resource_dir, calendar_name)) + except OSError: + trans.set_response_code(500) + + # Handle renaming. + + elif method in ("MOVE", "COPY"): + destinations = trans.get_header_values("Destination") + if len(destinations) != 1: + trans.set_response_code(500) + else: + try: + # Convert the URL into a filename. + # NOTE: Assume that the URL references the same "directory". + + destination = destinations[0].split("/")[-1] + + if method == "MOVE": + os.rename(os.path.join(self.resource_dir, calendar_name), os.path.join(self.resource_dir, destination)) + elif method == "COPY": + f_old = open(os.path.join(self.resource_dir, calendar_name), "rb") + f_new = open(os.path.join(self.resource_dir, destination), "wb") + f_new.write(f_old.read()) + f_new.close() + f_old.close() + + # NOTE: We do not observe the rules regarding overwriting + # NOTE: and the appropriate status codes. + + trans.set_header_value("Location", destinations[0]) + trans.set_response_code(201) + + except OSError: + trans.set_response_code(500) # Disallow other methods. diff -r 8858fbc7a88f -r cb6c38a13971 examples/JavaServlet/CalendarApp.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/JavaServlet/CalendarApp.py Thu Aug 18 21:10:09 2005 +0000 @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +from WebStack.Adapters import JavaServlet +from Calendar import CalendarResource +from javax.servlet.http import HttpServlet + +class CalendarApp(HttpServlet): + def __init__(self): + HttpServlet.__init__(self) + self.dispatcher = JavaServlet.Dispatcher(CalendarResource()) + + def service(self, request, response): + self.dispatcher.service(request, response) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 8858fbc7a88f -r cb6c38a13971 examples/ModPython/CalendarApp/CalendarHandler.py --- a/examples/ModPython/CalendarApp/CalendarHandler.py Thu Aug 18 21:09:33 2005 +0000 +++ b/examples/ModPython/CalendarApp/CalendarHandler.py Thu Aug 18 21:10:09 2005 +0000 @@ -7,11 +7,11 @@ sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") from WebStack.Adapters import ModPython -from Calendar import DirectoryResource +from Calendar import CalendarResource # NOTE: Not sure if the resource should be maintained in a resource pool. -resource = DirectoryResource() +resource = CalendarResource() def handler(req): global resource diff -r 8858fbc7a88f -r cb6c38a13971 examples/Twisted/CalendarApp.py --- a/examples/Twisted/CalendarApp.py Thu Aug 18 21:09:33 2005 +0000 +++ b/examples/Twisted/CalendarApp.py Thu Aug 18 21:10:09 2005 +0000 @@ -1,9 +1,9 @@ #!/usr/bin/env python from WebStack.Adapters.Twisted import deploy -from Calendar import DirectoryResource +from Calendar import CalendarResource print "Serving..." -deploy(DirectoryResource()) +deploy(CalendarResource()) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8858fbc7a88f -r cb6c38a13971 examples/WSGI/CalendarHandler.py --- a/examples/WSGI/CalendarHandler.py Thu Aug 18 21:09:33 2005 +0000 +++ b/examples/WSGI/CalendarHandler.py Thu Aug 18 21:10:09 2005 +0000 @@ -7,8 +7,8 @@ sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") from WebStack.Adapters.WSGI import deploy -from Calendar import DirectoryResource +from Calendar import CalendarResource -deploy(DirectoryResource()) +deploy(CalendarResource()) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8858fbc7a88f -r cb6c38a13971 examples/Webware/CalendarApp/__init__.py --- a/examples/Webware/CalendarApp/__init__.py Thu Aug 18 21:09:33 2005 +0000 +++ b/examples/Webware/CalendarApp/__init__.py Thu Aug 18 21:10:09 2005 +0000 @@ -7,15 +7,15 @@ __version__ = "0.1" from WebStack.Adapters.Webware import WebStackServletFactory -from Calendar import DirectoryResource +from Calendar import CalendarResource # NOTE: Initialising a shared resource. -resource = DirectoryResource() +resource = CalendarResource() def InstallInWebKit(appServer): global resource app = appServer.application() - app.addServletFactory(WebStackServletFactory(app, resource, [".ics"])) + app.addServletFactory(WebStackServletFactory(app, resource, [".cal"])) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8858fbc7a88f -r cb6c38a13971 examples/Webware/CalendarContext/__init__.py --- a/examples/Webware/CalendarContext/__init__.py Thu Aug 18 21:09:33 2005 +0000 +++ b/examples/Webware/CalendarContext/__init__.py Thu Aug 18 21:10:09 2005 +0000 @@ -5,11 +5,11 @@ """ from WebStack.Adapters.Webware import WebStackURLParser -from Calendar import DirectoryResource +from Calendar import CalendarResource # NOTE: Initialising a shared resource. -resource = DirectoryResource() +resource = CalendarResource() urlParser = WebStackURLParser(resource) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8858fbc7a88f -r cb6c38a13971 examples/Zope/CalendarProduct/__init__.py --- a/examples/Zope/CalendarProduct/__init__.py Thu Aug 18 21:09:33 2005 +0000 +++ b/examples/Zope/CalendarProduct/__init__.py Thu Aug 18 21:10:09 2005 +0000 @@ -2,14 +2,14 @@ "A Zope calendar product." -from Calendar import DirectoryResource +from Calendar import CalendarResource from WebStack.Adapters.Zope import WebStackAdapterProduct from Globals import InitializeClass class CalendarProduct(WebStackAdapterProduct): meta_type = "Calendar product" def __init__(self, id): - WebStackAdapterProduct.__init__(self, id, DirectoryResource()) + WebStackAdapterProduct.__init__(self, id, CalendarResource()) InitializeClass(CalendarProduct)