WebStack

examples/Common/Simple/__init__.py

660:e1211a95a44f
2007-09-27 paulb [project @ 2007-09-27 17:48:43 by paulb] Tidied up the javadoc strings.
     1 #!/usr/bin/env python     2      3 "A simple application for test purposes."     4      5 import WebStack.Generic     6      7 class SimpleResource:     8      9     "A simple resource."    10     11     def respond(self, trans):    12         trans.set_content_type(WebStack.Generic.ContentType("text/html", "utf-8"))    13     14         # Get some information.    15     16         out = trans.get_response_stream()    17         if trans.get_content_type():    18             content_type = trans.get_content_type().media_type    19             charset = trans.get_content_type().charset    20         else:    21             content_type, charset = None, None    22     23         # Use Unicode strings for correct character encoding.    24     25         out.write(u"""    26 <html>    27   <head>    28     <title>Simple Example</title>    29   </head>    30   <body>    31     <h1>Test</h1>    32     <ul>    33       <li>Path: %s</li>    34       <li>Path without query: %s</li>    35       <li>Path info: %s</li>    36       <li>Virtual path info: %s</li>    37       <li>Processed virtual path info: %s</li>    38       <li>Query string: %s</li>    39       <li>Query string (decoded): %s</li>    40       <li>Server name: %s</li>    41       <li>Server port: %s</li>    42       <li>Request method: %s</li>    43       <li>User: %s</li>    44       <li>Content type: %s</li>    45       <li>Charset: %s</li>    46       <li>Languages:    47         <ul>    48         %s    49         </ul>    50       </li>    51       <li>Charsets:    52         <ul>    53         %s    54         </ul>    55       </li>    56       <li>Headers:    57         <dl>    58         %s    59         </dl>    60       </li>    61       <li>User-Agent:    62         <ul>    63         %s    64         </ul>    65       </li>    66       <li>user-agent:    67         <ul>    68         %s    69         </ul>    70       </li>    71       <li>Fields from path:    72         <ul>    73         %s    74         </ul>    75       </li>    76       <li>Fields from path (explicit encoding):    77         <ul>    78         %s    79         </ul>    80       </li>    81       <li>Fields from body:    82         <ul>    83         %s    84         </ul>    85       </li>    86       <li>Fields from path and body:    87         <ul>    88         %s    89         </ul>    90       </li>    91       <li>Cookies:    92         <ul>    93         %s    94         </ul>    95       </li>    96     </ul>    97   </body>    98 </html>    99 """ % (   100     trans.get_path("utf-8"),   101     trans.get_path_without_query("utf-8"),   102     trans.get_path_info("utf-8"),   103     trans.get_virtual_path_info("utf-8"),   104     trans.get_processed_virtual_path_info("utf-8"),   105     trans.get_query_string(),   106     trans.decode_path(trans.get_query_string(), "utf-8"),   107     trans.get_server_name(),   108     trans.get_server_port(),   109     trans.get_request_method(),   110     trans.get_user(),   111     content_type,   112     charset,   113     self._format_list(trans.get_content_languages()),   114     self._format_list(trans.get_content_charsets()),   115     self._format_dict(trans.get_headers()),   116     self._format_list(trans.get_header_values("User-Agent")),   117     self._format_list(trans.get_header_values("user-agent")),   118     self._format_fields(trans.get_fields_from_path()),   119     self._format_fields(trans.get_fields_from_path("utf-8")),   120     self._format_fields(trans.get_fields_from_body()),   121     self._format_fields(trans.get_fields("utf-8")),   122     self._format_cookies(trans.get_cookies()),   123 ))   124    125     def _format_dict(self, d):   126         return "".join([   127             "<dt>%s</dt><dd>%s</dd>" % (key, value)   128             for key, value in d.items()   129         ])   130    131     def _format_fields(self, d):   132         return "".join([   133             "<li>%s<ul>%s</ul></li>" % (key, self._format_list(value))   134             for key, value in d.items()   135         ])   136    137     def _format_cookies(self, d):   138         return "".join([   139             "<dt>%s</dt><dd>%s</dd>" % (key, value.value)   140             for key, value in d.items()   141         ])   142    143     def _format_list(self, l):   144         return "".join([   145             "<li>%s</li>" % (value or "<em>empty</em>")   146             for value in l   147         ])   148    149 # vim: tabstop=4 expandtab shiftwidth=4