WebStack

Changeset

416:cb6c38a13971
2005-08-18 paulb raw files shortlog changelog graph [project @ 2005-08-18 21:09:56 by paulb] Fixed the calendar example (and added missing deployment programs).
examples/BaseHTTPRequestHandler/CalendarApp.py (file) examples/BaseHTTPRequestHandler/DemoApp.py (file) examples/CGI/CalendarHandler.py (file) examples/Common/Calendar/__init__.py (file) examples/JavaServlet/CalendarApp.py (file) examples/ModPython/CalendarApp/CalendarHandler.py (file) examples/Twisted/CalendarApp.py (file) examples/WSGI/CalendarHandler.py (file) examples/Webware/CalendarApp/__init__.py (file) examples/Webware/CalendarContext/__init__.py (file) examples/Zope/CalendarProduct/__init__.py (file)
     1.1 --- a/examples/BaseHTTPRequestHandler/CalendarApp.py	Thu Aug 18 21:09:33 2005 +0000
     1.2 +++ b/examples/BaseHTTPRequestHandler/CalendarApp.py	Thu Aug 18 21:10:09 2005 +0000
     1.3 @@ -1,9 +1,9 @@
     1.4  #!/usr/bin/env python
     1.5  
     1.6  from WebStack.Adapters.BaseHTTPRequestHandler import deploy
     1.7 -from Calendar import DirectoryResource
     1.8 +from Calendar import CalendarResource
     1.9  
    1.10  print "Serving..."
    1.11 -deploy(DirectoryResource())
    1.12 +deploy(CalendarResource())
    1.13  
    1.14  # vim: tabstop=4 expandtab shiftwidth=4
     2.1 --- a/examples/BaseHTTPRequestHandler/DemoApp.py	Thu Aug 18 21:09:33 2005 +0000
     2.2 +++ b/examples/BaseHTTPRequestHandler/DemoApp.py	Thu Aug 18 21:10:09 2005 +0000
     2.3 @@ -18,6 +18,7 @@
     2.4  from Simple import SimpleResource
     2.5  from Unicode import UnicodeResource
     2.6  from VerySimple import VerySimpleResource
     2.7 +from Calendar import CalendarResource
     2.8  
     2.9  # A very simple index page.
    2.10  
    2.11 @@ -41,6 +42,8 @@
    2.12        <li><a href="simple">Simple test</a></li>
    2.13        <li><a href="unicode">Unicode test</a></li>
    2.14        <li><a href="verysimple">Very simple test</a></li>
    2.15 +      <li><a href="webdav://localhost:8080/calendar/">Calendar store example</a> - <strong>requires a WebDAV-capable browser</strong><br />
    2.16 +          Copy calendar files into the directory, view them, delete them, and so on.</li>
    2.17      </ul>
    2.18      <p>You can run all of the examples independently - see the documentation in
    2.19         the <code>docs</code> directory, especially the subdirectories for each
    2.20 @@ -59,6 +62,7 @@
    2.21      "simple" : SimpleResource(),
    2.22      "unicode" : UnicodeResource(),
    2.23      "verysimple" : VerySimpleResource(),
    2.24 +    "calendar" : CalendarResource(),
    2.25      "" : DemoResource(),
    2.26      })
    2.27  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/examples/CGI/CalendarHandler.py	Thu Aug 18 21:10:09 2005 +0000
     3.3 @@ -0,0 +1,14 @@
     3.4 +#!/usr/bin/env python
     3.5 +
     3.6 +# NOTE: Path manipulation requires manual customisation.
     3.7 +
     3.8 +import sys
     3.9 +sys.path.append("/home/paulb/Software/Python/WebStack")
    3.10 +sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common")
    3.11 +
    3.12 +from WebStack.Adapters.CGI import deploy
    3.13 +from Calendar import CalendarResource
    3.14 +
    3.15 +deploy(CalendarResource())
    3.16 +
    3.17 +# vim: tabstop=4 expandtab shiftwidth=4
     4.1 --- a/examples/Common/Calendar/__init__.py	Thu Aug 18 21:09:33 2005 +0000
     4.2 +++ b/examples/Common/Calendar/__init__.py	Thu Aug 18 21:10:09 2005 +0000
     4.3 @@ -4,8 +4,9 @@
     4.4  
     4.5  import WebStack.Generic
     4.6  import time
     4.7 +import os
     4.8  
     4.9 -class DirectoryResource:
    4.10 +class CalendarResource:
    4.11  
    4.12      """
    4.13      A resource which handles incoming calendars and viewing requests.
    4.14 @@ -14,6 +15,12 @@
    4.15      calendars, yet also accepts incoming calendars.
    4.16      """
    4.17  
    4.18 +    resource_dir = os.path.join(os.path.split(__file__)[0], "calendars")
    4.19 +
    4.20 +    def __init__(self):
    4.21 +        if not os.path.exists(self.resource_dir):
    4.22 +            os.mkdir(self.resource_dir)
    4.23 +
    4.24      def respond(self, trans):
    4.25  
    4.26          """
    4.27 @@ -30,8 +37,10 @@
    4.28          method = trans.get_request_method()
    4.29  
    4.30          # NOTE: Some frameworks do not pass in the content type.
    4.31 +        # NOTE: We always assume that calendar files are being uploaded.
    4.32  
    4.33          content_type = trans.get_content_type()
    4.34 +        calendar_name = trans.get_virtual_path_info().split("/")[-1]
    4.35  
    4.36          # Handle uploads.
    4.37  
    4.38 @@ -40,17 +49,14 @@
    4.39              # Get the last path component as the name of the calendar.
    4.40              # NOTE: This could be improved to permit hierarchical naming.
    4.41  
    4.42 -            calendar_name = trans.get_path_without_query().split("/")[-1]
    4.43              input = trans.get_request_stream()
    4.44              data = input.read()
    4.45  
    4.46 -            # Store the calendar in the session.
    4.47 +            # Store the calendar in the directory.
    4.48  
    4.49 -            session["calendar name"] = calendar_name
    4.50 -            session["media type"] = content_type.media_type
    4.51 -            session["calendar data"] = data
    4.52 -            session["calendar size"] = len(data)
    4.53 -            session["calendar time"] = time.strftime("%Y-%m-%dT%T")
    4.54 +            f = open(os.path.join(self.resource_dir, calendar_name), "wb")
    4.55 +            f.write(data)
    4.56 +            f.close()
    4.57  
    4.58          # Handle directory browsing.
    4.59  
    4.60 @@ -62,7 +68,8 @@
    4.61  <D:multistatus xmlns:D="DAV:">
    4.62  """)
    4.63  
    4.64 -            if trans.get_path_info() == "/":
    4.65 +            if trans.get_virtual_path_info() == "/":
    4.66 +                time_now = time.strftime("%Y-%m-%dT%TZ", time.gmtime(time.time()))
    4.67                  out.write("""
    4.68    <D:response>
    4.69      <D:href>%s</D:href>
    4.70 @@ -77,9 +84,12 @@
    4.71        <D:status>HTTP/1.1 200 OK</D:status>
    4.72      </D:propstat>
    4.73    </D:response>
    4.74 -""" % (trans.get_path_without_query(), time.strftime("%Y-%m-%dT%T"), trans.get_path_without_query()))
    4.75 +""" % (trans.get_path_without_query(), time_now, trans.get_path_without_query()))
    4.76  
    4.77 -            if session.has_key("calendar name"):
    4.78 +            for filename in os.listdir(self.resource_dir):
    4.79 +                pathname = os.path.join(self.resource_dir, filename)
    4.80 +                created = time.strftime("%Y-%m-%dT%TZ", time.gmtime(os.path.getctime(pathname)))
    4.81 +                size = os.path.getsize(pathname)
    4.82                  out.write("""
    4.83    <D:response>
    4.84      <D:href>%s%s</D:href>
    4.85 @@ -89,12 +99,12 @@
    4.86          <D:displayname>%s</D:displayname>
    4.87          <D:resourcetype/>
    4.88          <D:getcontenttype>%s</D:getcontenttype>
    4.89 +        <D:getcontentlength>%s</D:getcontentlength>
    4.90        </D:prop>
    4.91        <D:status>HTTP/1.1 200 OK</D:status>
    4.92      </D:propstat>
    4.93    </D:response>
    4.94 -""" % (trans.get_path_without_query(), session.get("calendar name"), session.get("calendar time") or time.strftime("%Y-%m-%dT%T"),
    4.95 -    session.get("calendar name"), session.get("media type") or ""))
    4.96 +""" % (trans.get_path_without_query(), filename, created, filename, "text/calendar", size))
    4.97  
    4.98              out.write("""
    4.99  </D:multistatus>
   4.100 @@ -105,20 +115,48 @@
   4.101          elif method == "GET":
   4.102              trans.set_content_type(WebStack.Generic.ContentType("text/html"))
   4.103              out = trans.get_response_stream()
   4.104 -            out.write("""
   4.105 -<html>
   4.106 -  <head>
   4.107 -    <title>Last Uploaded Calendar</title>
   4.108 -  </head>
   4.109 -  <body>
   4.110 -    <h1>Calendar %s</h1>
   4.111 -    <p>Media type: %s</p>
   4.112 -    <p>Calendar size: %s</p>
   4.113 -    <pre>%s</pre>
   4.114 -  </body>
   4.115 -</html>
   4.116 -""" % (session.get("calendar name"), session.get("media type"), session.get("calendar size"),
   4.117 -    session.get("calendar data", "").replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")))
   4.118 +            f = open(os.path.join(self.resource_dir, calendar_name))
   4.119 +            out.write(f.read())
   4.120 +            f.close()
   4.121 +
   4.122 +        # Handle deletion.
   4.123 +
   4.124 +        elif method == "DELETE":
   4.125 +            try:
   4.126 +                os.remove(os.path.join(self.resource_dir, calendar_name))
   4.127 +            except OSError:
   4.128 +                trans.set_response_code(500)
   4.129 +
   4.130 +        # Handle renaming.
   4.131 +
   4.132 +        elif method in ("MOVE", "COPY"):
   4.133 +            destinations = trans.get_header_values("Destination")
   4.134 +            if len(destinations) != 1:
   4.135 +                trans.set_response_code(500)
   4.136 +            else:
   4.137 +                try:
   4.138 +                    # Convert the URL into a filename.
   4.139 +                    # NOTE: Assume that the URL references the same "directory".
   4.140 +
   4.141 +                    destination = destinations[0].split("/")[-1]
   4.142 +
   4.143 +                    if method == "MOVE":
   4.144 +                        os.rename(os.path.join(self.resource_dir, calendar_name), os.path.join(self.resource_dir, destination))
   4.145 +                    elif method == "COPY":
   4.146 +                        f_old = open(os.path.join(self.resource_dir, calendar_name), "rb")
   4.147 +                        f_new = open(os.path.join(self.resource_dir, destination), "wb")
   4.148 +                        f_new.write(f_old.read())
   4.149 +                        f_new.close()
   4.150 +                        f_old.close()
   4.151 +
   4.152 +                    # NOTE: We do not observe the rules regarding overwriting
   4.153 +                    # NOTE: and the appropriate status codes.
   4.154 +
   4.155 +                    trans.set_header_value("Location", destinations[0])
   4.156 +                    trans.set_response_code(201)
   4.157 +
   4.158 +                except OSError:
   4.159 +                    trans.set_response_code(500)
   4.160  
   4.161          # Disallow other methods.
   4.162  
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/examples/JavaServlet/CalendarApp.py	Thu Aug 18 21:10:09 2005 +0000
     5.3 @@ -0,0 +1,15 @@
     5.4 +#!/usr/bin/env python
     5.5 +
     5.6 +from WebStack.Adapters import JavaServlet
     5.7 +from Calendar import CalendarResource
     5.8 +from javax.servlet.http import HttpServlet
     5.9 +
    5.10 +class CalendarApp(HttpServlet):
    5.11 +    def __init__(self):
    5.12 +        HttpServlet.__init__(self)
    5.13 +        self.dispatcher = JavaServlet.Dispatcher(CalendarResource())
    5.14 +
    5.15 +    def service(self, request, response):
    5.16 +        self.dispatcher.service(request, response)
    5.17 +
    5.18 +# vim: tabstop=4 expandtab shiftwidth=4
     6.1 --- a/examples/ModPython/CalendarApp/CalendarHandler.py	Thu Aug 18 21:09:33 2005 +0000
     6.2 +++ b/examples/ModPython/CalendarApp/CalendarHandler.py	Thu Aug 18 21:10:09 2005 +0000
     6.3 @@ -7,11 +7,11 @@
     6.4  sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common")
     6.5  
     6.6  from WebStack.Adapters import ModPython
     6.7 -from Calendar import DirectoryResource
     6.8 +from Calendar import CalendarResource
     6.9  
    6.10  # NOTE: Not sure if the resource should be maintained in a resource pool.
    6.11  
    6.12 -resource = DirectoryResource()
    6.13 +resource = CalendarResource()
    6.14  
    6.15  def handler(req):
    6.16      global resource
     7.1 --- a/examples/Twisted/CalendarApp.py	Thu Aug 18 21:09:33 2005 +0000
     7.2 +++ b/examples/Twisted/CalendarApp.py	Thu Aug 18 21:10:09 2005 +0000
     7.3 @@ -1,9 +1,9 @@
     7.4  #!/usr/bin/env python
     7.5  
     7.6  from WebStack.Adapters.Twisted import deploy
     7.7 -from Calendar import DirectoryResource
     7.8 +from Calendar import CalendarResource
     7.9  
    7.10  print "Serving..."
    7.11 -deploy(DirectoryResource())
    7.12 +deploy(CalendarResource())
    7.13  
    7.14  # vim: tabstop=4 expandtab shiftwidth=4
     8.1 --- a/examples/WSGI/CalendarHandler.py	Thu Aug 18 21:09:33 2005 +0000
     8.2 +++ b/examples/WSGI/CalendarHandler.py	Thu Aug 18 21:10:09 2005 +0000
     8.3 @@ -7,8 +7,8 @@
     8.4  sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common")
     8.5  
     8.6  from WebStack.Adapters.WSGI import deploy
     8.7 -from Calendar import DirectoryResource
     8.8 +from Calendar import CalendarResource
     8.9  
    8.10 -deploy(DirectoryResource())
    8.11 +deploy(CalendarResource())
    8.12  
    8.13  # vim: tabstop=4 expandtab shiftwidth=4
     9.1 --- a/examples/Webware/CalendarApp/__init__.py	Thu Aug 18 21:09:33 2005 +0000
     9.2 +++ b/examples/Webware/CalendarApp/__init__.py	Thu Aug 18 21:10:09 2005 +0000
     9.3 @@ -7,15 +7,15 @@
     9.4  __version__ = "0.1"
     9.5  
     9.6  from WebStack.Adapters.Webware import WebStackServletFactory
     9.7 -from Calendar import DirectoryResource
     9.8 +from Calendar import CalendarResource
     9.9  
    9.10  # NOTE: Initialising a shared resource.
    9.11  
    9.12 -resource = DirectoryResource()
    9.13 +resource = CalendarResource()
    9.14  
    9.15  def InstallInWebKit(appServer):
    9.16      global resource
    9.17      app = appServer.application()
    9.18 -    app.addServletFactory(WebStackServletFactory(app, resource, [".ics"]))
    9.19 +    app.addServletFactory(WebStackServletFactory(app, resource, [".cal"]))
    9.20  
    9.21  # vim: tabstop=4 expandtab shiftwidth=4
    10.1 --- a/examples/Webware/CalendarContext/__init__.py	Thu Aug 18 21:09:33 2005 +0000
    10.2 +++ b/examples/Webware/CalendarContext/__init__.py	Thu Aug 18 21:10:09 2005 +0000
    10.3 @@ -5,11 +5,11 @@
    10.4  """
    10.5  
    10.6  from WebStack.Adapters.Webware import WebStackURLParser
    10.7 -from Calendar import DirectoryResource
    10.8 +from Calendar import CalendarResource
    10.9  
   10.10  # NOTE: Initialising a shared resource.
   10.11  
   10.12 -resource = DirectoryResource()
   10.13 +resource = CalendarResource()
   10.14  urlParser = WebStackURLParser(resource)
   10.15  
   10.16  # vim: tabstop=4 expandtab shiftwidth=4
    11.1 --- a/examples/Zope/CalendarProduct/__init__.py	Thu Aug 18 21:09:33 2005 +0000
    11.2 +++ b/examples/Zope/CalendarProduct/__init__.py	Thu Aug 18 21:10:09 2005 +0000
    11.3 @@ -2,14 +2,14 @@
    11.4  
    11.5  "A Zope calendar product."
    11.6  
    11.7 -from Calendar import DirectoryResource
    11.8 +from Calendar import CalendarResource
    11.9  from WebStack.Adapters.Zope import WebStackAdapterProduct
   11.10  from Globals import InitializeClass
   11.11  
   11.12  class CalendarProduct(WebStackAdapterProduct):
   11.13      meta_type = "Calendar product"
   11.14      def __init__(self, id):
   11.15 -        WebStackAdapterProduct.__init__(self, id, DirectoryResource())
   11.16 +        WebStackAdapterProduct.__init__(self, id, CalendarResource())
   11.17  
   11.18  InitializeClass(CalendarProduct)
   11.19