1 #!/usr/bin/env python 2 3 """ 4 Generic Web framework interfaces. 5 """ 6 7 class ContentType: 8 9 "A container for content type information." 10 11 def __init__(self, content_type, charset): 12 self.content_type = content_type 13 self.charset = charset 14 15 class Transaction: 16 17 """ 18 A generic transaction interface containing framework-specific methods to be 19 overridden. 20 """ 21 22 # Utility methods. 23 24 def parse_content_type(self, content_type_field): 25 26 """ 27 Determine the content type and charset from the supplied 28 'content_type_field' string. 29 """ 30 31 if content_type_field is None: 32 return ContentType(None, "iso-8859-1") 33 34 t = content_type_field.split("; charset=") 35 if len(t) == 1: 36 return ContentType(t[0], "iso-8859-1") 37 else: 38 return ContentType(t[0], t[1]) 39 40 def parse_content_preferences(self, accept_preference): 41 42 """ 43 Returns the preferences as requested by the user agent. The preferences are 44 returned as a list of codes in the same order as they appeared in the 45 appropriate environment variable. In other words, the explicit weighting 46 criteria are ignored. 47 48 As the 'accept_preference' parameter, values for language and charset 49 preferences are appropriate. 50 """ 51 52 accept_defs = accept_preference.split(",") 53 accept_prefs = [] 54 for accept_def in accept_defs: 55 t = accept_def.split(";") 56 if len(t) >= 1: 57 accept_prefs.append(t[0].strip()) 58 return accept_prefs 59 60 # Request-related methods. 61 62 def get_request_stream(self): 63 64 """ 65 A framework-specific method which returns the request stream for 66 the transaction. 67 """ 68 69 raise NotImplementedError, "get_request_stream" 70 71 def get_request_method(self): 72 73 """ 74 A framework-specific method which gets the request method. 75 """ 76 77 raise NotImplementedError, "get_request_method" 78 79 def get_headers(self): 80 81 """ 82 A framework-specific method which returns the request headers. 83 NOTE: Experimental, since framework support varies somewhat. 84 """ 85 86 raise NotImplementedError, "get_headers" 87 88 def get_content_type(self): 89 90 """ 91 A framework-specific method which gets the content type specified on the 92 request, along with the charset employed. 93 """ 94 95 raise NotImplementedError, "get_content_type" 96 97 def get_content_charsets(self): 98 99 """ 100 Returns the character set preferences. 101 """ 102 103 raise NotImplementedError, "get_content_charsets" 104 105 def get_content_languages(self): 106 107 """ 108 A framework-specific method which extracts language information from 109 the transaction. 110 """ 111 112 raise NotImplementedError, "get_content_languages" 113 114 def get_path_info(self): 115 116 """ 117 A framework-specific method which gets the "path info" (the part of the 118 URL after the resource name handling the current request) from the 119 request. 120 """ 121 122 raise NotImplementedError, "get_path_info" 123 124 def get_fields(self): 125 126 """ 127 A framework-specific method which extracts the form fields from the 128 transaction. 129 """ 130 131 raise NotImplementedError, "get_fields" 132 133 def get_agent_information(self): 134 135 """ 136 A framework-specific method which extracts agent information from 137 the transaction. 138 """ 139 140 raise NotImplementedError, "get_agent_information" 141 142 # Response-related methods. 143 144 def get_response_stream(self): 145 146 """ 147 A framework-specific method which returns the response stream for 148 the transaction. 149 """ 150 151 raise NotImplementedError, "get_response_stream" 152 153 def set_content_type(self, content_type): 154 155 """ 156 A framework-specific method which sets the 'content_type' for the 157 response. 158 """ 159 160 raise NotImplementedError, "set_content_type" 161 162 class Resource: 163 164 "A generic resource interface." 165 166 pass 167 168 # vim: tabstop=4 expandtab shiftwidth=4