paulb@46 | 1 | #!/usr/bin/env python |
paulb@46 | 2 | |
paulb@46 | 3 | """ |
paulb@46 | 4 | Request helper classes. |
paulb@403 | 5 | |
paul@773 | 6 | Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@489 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@46 | 21 | """ |
paulb@46 | 22 | |
paulb@46 | 23 | class MessageBodyStream: |
paulb@46 | 24 | |
paulb@46 | 25 | """ |
paulb@46 | 26 | A naive stream class, providing a non-blocking stream for transactions when |
paulb@46 | 27 | reading the message body. According to the HTTP standard, the following |
paulb@46 | 28 | things decide how long the message is: |
paulb@46 | 29 | |
paulb@46 | 30 | * Use of the Content-Length header field (see 4.4 Message Length). |
paulb@46 | 31 | * Use of the Transfer-Coding header field (see 3.6 Transfer Codings), |
paulb@46 | 32 | particularly when the "chunked" coding is used. |
paulb@46 | 33 | |
paulb@46 | 34 | NOTE: For now, we don't support the Transfer-Coding business. |
paulb@46 | 35 | """ |
paulb@46 | 36 | |
paulb@46 | 37 | def __init__(self, stream, headers): |
paulb@46 | 38 | |
paulb@46 | 39 | """ |
paulb@46 | 40 | Initialise the object with the given underlying 'stream'. The supplied |
paulb@46 | 41 | 'headers' in a dictionary-style object are used to examine the nature of |
paulb@46 | 42 | the request. |
paulb@46 | 43 | """ |
paulb@46 | 44 | |
paulb@46 | 45 | self.stream = stream |
paulb@46 | 46 | self.headers = headers |
paulb@46 | 47 | self.length = int(headers.get("Content-Length") or 0) |
paulb@46 | 48 | |
paulb@46 | 49 | def read(self, limit=None): |
paulb@46 | 50 | |
paulb@46 | 51 | "Reads all remaining data from the message body." |
paulb@46 | 52 | |
paulb@46 | 53 | if limit is not None: |
paulb@46 | 54 | limit = min(limit, self.length) |
paulb@46 | 55 | else: |
paulb@46 | 56 | limit = self.length |
paulb@46 | 57 | data = self.stream.read(limit) |
paulb@46 | 58 | self.length = self.length - len(data) |
paulb@46 | 59 | return data |
paulb@46 | 60 | |
paulb@714 | 61 | def readline(self, n=None): |
paulb@46 | 62 | |
paulb@46 | 63 | "Reads a single line of data from the message body." |
paulb@46 | 64 | |
paulb@46 | 65 | data = [] |
paulb@46 | 66 | while self.length > 0: |
paulb@46 | 67 | data.append(self.read(1)) |
paulb@46 | 68 | if data[-1] == "\n": |
paulb@46 | 69 | break |
paulb@46 | 70 | return "".join(data) |
paulb@46 | 71 | |
paulb@46 | 72 | def readlines(self): |
paulb@46 | 73 | |
paulb@46 | 74 | """ |
paulb@46 | 75 | Reads all remaining data from the message body, splitting it into lines |
paulb@46 | 76 | and returning the data as a list of lines. |
paulb@46 | 77 | """ |
paulb@46 | 78 | |
paulb@46 | 79 | lines = self.read().split("\n") |
paulb@46 | 80 | for i in range(0, len(lines) - 1): |
paulb@46 | 81 | lines[i] = lines[i] + "\n" |
paulb@46 | 82 | return lines |
paulb@46 | 83 | |
paulb@46 | 84 | def close(self): |
paulb@46 | 85 | |
paulb@46 | 86 | "Closes the stream." |
paulb@46 | 87 | |
paulb@46 | 88 | self.stream.close() |
paulb@46 | 89 | |
paulb@724 | 90 | class HeaderDict: |
paulb@724 | 91 | |
paulb@724 | 92 | "A dictionary for headers." |
paulb@724 | 93 | |
paulb@724 | 94 | def __init__(self, headers=None): |
paulb@724 | 95 | self.headers = {} |
paulb@724 | 96 | if headers is not None: |
paulb@724 | 97 | self.update(headers) |
paulb@724 | 98 | |
paulb@724 | 99 | # Lower-case-string-coercing methods. |
paulb@724 | 100 | |
paulb@724 | 101 | def __getitem__(self, key): |
paulb@724 | 102 | return self.headers[str(key).lower()] |
paulb@724 | 103 | |
paulb@724 | 104 | def __setitem__(self, key, value): |
paulb@724 | 105 | self.headers[str(key).lower()] = value |
paulb@724 | 106 | |
paulb@724 | 107 | def get(self, key, default=None): |
paulb@724 | 108 | return self.headers.get(str(key).lower(), default) |
paulb@724 | 109 | |
paulb@724 | 110 | def has_key(self, key): |
paulb@724 | 111 | return self.headers.has_key(str(key).lower()) |
paulb@724 | 112 | |
paulb@724 | 113 | # Forwarding methods. |
paulb@724 | 114 | |
paulb@724 | 115 | def keys(self): |
paulb@724 | 116 | return self.headers.keys() |
paulb@724 | 117 | |
paulb@724 | 118 | def values(self): |
paulb@724 | 119 | return self.headers.values() |
paulb@724 | 120 | |
paulb@724 | 121 | def items(self): |
paulb@724 | 122 | return self.headers.items() |
paulb@724 | 123 | |
paulb@724 | 124 | # Derived from the above. |
paulb@724 | 125 | |
paulb@724 | 126 | def __contains__(self, key): |
paulb@724 | 127 | return self.has_key(key) |
paulb@724 | 128 | |
paulb@724 | 129 | def update(self, other): |
paulb@724 | 130 | for k, v in other.items(): |
paulb@724 | 131 | self[k] = v |
paulb@724 | 132 | |
paulb@724 | 133 | def __repr__(self): |
paulb@724 | 134 | return "HeaderDict(%r)" % self.headers |
paulb@724 | 135 | |
paulb@464 | 136 | class HeaderValue: |
paulb@464 | 137 | |
paulb@464 | 138 | "A container for header information." |
paulb@464 | 139 | |
paulb@464 | 140 | def __init__(self, principal_value, **attributes): |
paulb@464 | 141 | |
paulb@464 | 142 | """ |
paulb@464 | 143 | Initialise the container with the given 'principal_value' and optional |
paulb@464 | 144 | keyword attributes representing the key=value pairs which accompany the |
paulb@464 | 145 | 'principal_value'. |
paulb@464 | 146 | """ |
paulb@464 | 147 | |
paulb@464 | 148 | self.principal_value = principal_value |
paulb@464 | 149 | self.attributes = attributes |
paulb@464 | 150 | |
paulb@464 | 151 | def __getattr__(self, name): |
paulb@464 | 152 | if self.attributes.has_key(name): |
paulb@464 | 153 | return self.attributes[name] |
paulb@464 | 154 | else: |
paulb@464 | 155 | raise AttributeError, name |
paulb@464 | 156 | |
paulb@724 | 157 | def __repr__(self): |
paulb@724 | 158 | return "HeaderValue(%r)" % str(self) |
paulb@724 | 159 | |
paulb@464 | 160 | def __str__(self): |
paulb@464 | 161 | |
paulb@464 | 162 | """ |
paulb@464 | 163 | Format the header value object, producing a string suitable for the |
paulb@464 | 164 | response header field. |
paulb@464 | 165 | """ |
paulb@464 | 166 | |
paulb@464 | 167 | l = [] |
paulb@464 | 168 | if self.principal_value: |
paulb@464 | 169 | l.append(self.principal_value) |
paulb@464 | 170 | for name, value in self.attributes.items(): |
paulb@464 | 171 | l.append("; ") |
paulb@464 | 172 | l.append("%s=%s" % (name, value)) |
paulb@464 | 173 | |
paulb@464 | 174 | # Make sure that only ASCII is used. |
paulb@464 | 175 | |
paulb@464 | 176 | return "".join(l).encode("US-ASCII") |
paulb@464 | 177 | |
paulb@464 | 178 | class ContentType(HeaderValue): |
paulb@464 | 179 | |
paulb@464 | 180 | "A container for content type information." |
paulb@464 | 181 | |
paulb@464 | 182 | def __init__(self, media_type, charset=None, **attributes): |
paulb@464 | 183 | |
paulb@464 | 184 | """ |
paulb@464 | 185 | Initialise the container with the given 'media_type', an optional |
paulb@464 | 186 | 'charset', and optional keyword attributes representing the key=value |
paulb@464 | 187 | pairs which qualify content types. |
paulb@464 | 188 | """ |
paulb@464 | 189 | |
paulb@464 | 190 | if charset is not None: |
paulb@464 | 191 | attributes["charset"] = charset |
paulb@464 | 192 | HeaderValue.__init__(self, media_type, **attributes) |
paulb@464 | 193 | |
paulb@464 | 194 | def __getattr__(self, name): |
paulb@464 | 195 | if name == "media_type": |
paulb@464 | 196 | return self.principal_value |
paulb@464 | 197 | elif name == "charset": |
paulb@464 | 198 | return self.attributes.get("charset") |
paulb@464 | 199 | elif self.attributes.has_key(name): |
paulb@464 | 200 | return self.attributes[name] |
paulb@464 | 201 | else: |
paulb@464 | 202 | raise AttributeError, name |
paulb@464 | 203 | |
paulb@105 | 204 | class Cookie: |
paulb@105 | 205 | |
paulb@105 | 206 | """ |
paulb@105 | 207 | A simple cookie class for frameworks which do not return cookies in |
paulb@555 | 208 | structured form. Instances of this class contain the following attributes: |
paulb@555 | 209 | |
paulb@555 | 210 | * name - the name associated with the cookie |
paulb@555 | 211 | * value - the value retained by the cookie |
paulb@105 | 212 | """ |
paulb@105 | 213 | |
paulb@105 | 214 | def __init__(self, name, value): |
paulb@105 | 215 | self.name = name |
paulb@105 | 216 | self.value = value |
paulb@105 | 217 | |
paul@772 | 218 | class FileTooLargeError(Exception): |
paul@772 | 219 | |
paul@772 | 220 | "An exception indicating that an uploaded file was too large." |
paul@772 | 221 | |
paul@772 | 222 | pass |
paul@772 | 223 | |
paulb@464 | 224 | class FileContent: |
paulb@464 | 225 | |
paulb@464 | 226 | """ |
paulb@464 | 227 | A simple class representing uploaded file content. This is useful in holding |
paulb@464 | 228 | metadata as well as being an indicator of such content in environments such |
paulb@464 | 229 | as Jython where it is not trivial to differentiate between plain strings and |
paulb@464 | 230 | Unicode in a fashion also applicable to CPython. |
paulb@551 | 231 | |
paulb@551 | 232 | Instances of this class contain the following attributes: |
paulb@551 | 233 | |
paul@770 | 234 | * stream - a stream object through which the content of an uploaded file |
paul@770 | 235 | may be accessed |
paul@770 | 236 | * content - a plain string containing the contents of the uploaded file |
paul@770 | 237 | * filename - a plain string containing the supplied filename of the |
paul@770 | 238 | uploaded file |
paul@770 | 239 | * headers - a dictionary containing the headers associated with the |
paul@770 | 240 | uploaded file |
paul@772 | 241 | * limit - a limit, if previously specified, on the size of uploaded |
paul@772 | 242 | content |
paulb@464 | 243 | """ |
paulb@464 | 244 | |
paul@772 | 245 | def __init__(self, stream, headers=None, limit=None): |
paulb@464 | 246 | |
paulb@464 | 247 | """ |
paulb@714 | 248 | Initialise the object with a 'stream' through which the file can be |
paul@772 | 249 | read, along with optional 'headers' describing the content. An optional |
paul@772 | 250 | 'limit' can be specified to state the maximum number of bytes that may |
paul@772 | 251 | be read before the content is considered too large. |
paulb@464 | 252 | """ |
paulb@464 | 253 | |
paulb@714 | 254 | self.stream = stream |
paulb@724 | 255 | self.headers = headers or HeaderDict() |
paul@772 | 256 | self.limit = limit |
paulb@714 | 257 | self.cache = None |
paulb@714 | 258 | |
paulb@714 | 259 | def __getattr__(self, name): |
paulb@721 | 260 | |
paulb@721 | 261 | """ |
paul@770 | 262 | Provides a property value when 'name' is specified as "content" or as |
paul@770 | 263 | "filename". |
paulb@721 | 264 | """ |
paulb@721 | 265 | |
paul@770 | 266 | if name == "content": |
paulb@721 | 267 | |
paul@770 | 268 | if self.cache is not None: |
paul@770 | 269 | return self.cache |
paulb@721 | 270 | |
paul@770 | 271 | if self.reset(): |
paul@772 | 272 | return self._read() |
paul@770 | 273 | else: |
paul@772 | 274 | self.cache = self._read() |
paul@770 | 275 | return self.cache |
paul@770 | 276 | |
paul@770 | 277 | elif name == "filename": |
paul@770 | 278 | try: |
paul@770 | 279 | content_disposition = self.headers["Content-Disposition"] |
paul@773 | 280 | # NOTE: Always seem to need to remove quotes. |
paul@773 | 281 | return content_disposition.filename[1:-1] |
paul@770 | 282 | except (KeyError, AttributeError): |
paul@770 | 283 | return None |
paul@770 | 284 | |
paulb@714 | 285 | else: |
paul@770 | 286 | raise AttributeError, name |
paulb@464 | 287 | |
paul@772 | 288 | def _read(self): |
paul@772 | 289 | |
paul@772 | 290 | """ |
paul@772 | 291 | Read from the stream up to any limit, raising an exception if the |
paul@772 | 292 | limit is exceeded. |
paul@772 | 293 | """ |
paul@772 | 294 | |
paul@772 | 295 | if self.limit is not None: |
paul@772 | 296 | s = self.stream.read(self.limit) |
paul@772 | 297 | if self.stream.read(1): |
paul@772 | 298 | raise FileTooLargeError |
paul@772 | 299 | else: |
paul@772 | 300 | return s |
paul@772 | 301 | else: |
paul@772 | 302 | return self.stream.read() |
paul@772 | 303 | |
paulb@721 | 304 | def reset(self): |
paulb@721 | 305 | |
paulb@721 | 306 | "Reset the stream providing the data, returning whether this succeeded." |
paulb@721 | 307 | |
paulb@721 | 308 | # Python file objects. |
paulb@721 | 309 | |
paulb@721 | 310 | if hasattr(self.stream, "seek"): |
paulb@721 | 311 | self.stream.seek(0) |
paulb@721 | 312 | return 1 |
paulb@721 | 313 | |
paulb@721 | 314 | # Java input streams. |
paulb@721 | 315 | |
paulb@721 | 316 | elif hasattr(self.stream, "reset"): |
paulb@721 | 317 | self.stream.reset() |
paulb@721 | 318 | return 1 |
paulb@721 | 319 | |
paulb@721 | 320 | # Other streams. |
paulb@721 | 321 | |
paulb@721 | 322 | else: |
paulb@721 | 323 | return 0 |
paulb@721 | 324 | |
paulb@464 | 325 | def __str__(self): |
paulb@464 | 326 | return self.content |
paulb@464 | 327 | |
paulb@464 | 328 | def parse_header_value(header_class, header_value_str): |
paulb@464 | 329 | |
paulb@464 | 330 | """ |
paulb@464 | 331 | Create an object of the given 'header_class' by determining the details |
paulb@464 | 332 | of the given 'header_value_str' - a string containing the value of a |
paulb@464 | 333 | particular header. |
paulb@464 | 334 | """ |
paulb@464 | 335 | |
paulb@464 | 336 | if header_value_str is None: |
paulb@464 | 337 | return header_class(None) |
paulb@464 | 338 | |
paulb@464 | 339 | l = header_value_str.split(";") |
paulb@464 | 340 | attributes = {} |
paulb@464 | 341 | |
paulb@464 | 342 | # Find the attributes. |
paulb@464 | 343 | |
paulb@464 | 344 | principal_value, attributes_str = l[0].strip(), l[1:] |
paulb@464 | 345 | |
paulb@464 | 346 | for attribute_str in attributes_str: |
paulb@464 | 347 | t = attribute_str.split("=") |
paulb@464 | 348 | if len(t) > 1: |
paulb@464 | 349 | name, value = t[0].strip(), t[1].strip() |
paulb@464 | 350 | attributes[name] = value |
paulb@464 | 351 | |
paulb@464 | 352 | return header_class(principal_value, **attributes) |
paulb@464 | 353 | |
paulb@464 | 354 | def parse_headers(headers): |
paulb@464 | 355 | |
paulb@464 | 356 | """ |
paulb@464 | 357 | Parse the given 'headers' dictionary (containing names mapped to values), |
paulb@464 | 358 | returing a dictionary mapping names to HeaderValue objects. |
paulb@464 | 359 | """ |
paulb@464 | 360 | |
paulb@724 | 361 | new_headers = HeaderDict() |
paulb@464 | 362 | for name, value in headers.items(): |
paulb@464 | 363 | new_headers[name] = parse_header_value(HeaderValue, value) |
paulb@464 | 364 | return new_headers |
paulb@464 | 365 | |
paulb@199 | 366 | def get_storage_items(storage_body): |
paulb@199 | 367 | |
paulb@199 | 368 | """ |
paulb@199 | 369 | Return the items (2-tuples of the form key, values) from the 'storage_body'. |
paulb@199 | 370 | This is used in conjunction with FieldStorage objects. |
paulb@199 | 371 | """ |
paulb@199 | 372 | |
paulb@199 | 373 | items = [] |
paulb@199 | 374 | for key in storage_body.keys(): |
paulb@199 | 375 | items.append((key, storage_body[key])) |
paulb@199 | 376 | return items |
paulb@199 | 377 | |
paulb@199 | 378 | def get_body_fields(field_items, encoding): |
paulb@199 | 379 | |
paulb@199 | 380 | """ |
paulb@199 | 381 | Returns a dictionary mapping field names to lists of field values for all |
paulb@199 | 382 | entries in the given 'field_items' (2-tuples of the form key, values) using |
paulb@199 | 383 | the given 'encoding'. |
paulb@199 | 384 | This is used in conjunction with FieldStorage objects. |
paulb@199 | 385 | """ |
paulb@199 | 386 | |
paulb@199 | 387 | fields = {} |
paulb@199 | 388 | |
paulb@199 | 389 | for field_name, field_values in field_items: |
paulb@440 | 390 | field_name = decode_value(field_name, encoding) |
paulb@440 | 391 | |
paulb@199 | 392 | if type(field_values) == type([]): |
paulb@199 | 393 | fields[field_name] = [] |
paulb@199 | 394 | for field_value in field_values: |
paulb@460 | 395 | fields[field_name].append(get_body_field_or_file(field_value, encoding)) |
paulb@199 | 396 | else: |
paulb@460 | 397 | fields[field_name] = [get_body_field_or_file(field_values, encoding)] |
paulb@199 | 398 | |
paulb@199 | 399 | return fields |
paulb@199 | 400 | |
paulb@460 | 401 | def get_body_field_or_file(field_value, encoding): |
paulb@460 | 402 | |
paulb@460 | 403 | """ |
paulb@460 | 404 | Returns the appropriate value for the given 'field_value' either for a |
paulb@460 | 405 | normal form field (thus employing the given 'encoding') or for a file |
paulb@460 | 406 | upload field (returning a plain string). |
paulb@460 | 407 | """ |
paulb@460 | 408 | |
paulb@460 | 409 | if hasattr(field_value, "headers") and field_value.headers.has_key("content-type"): |
paulb@460 | 410 | |
paulb@460 | 411 | # Detect stray FileUpload objects (eg. with Zope). |
paulb@460 | 412 | |
paulb@460 | 413 | if hasattr(field_value, "read"): |
paulb@714 | 414 | return FileContent(field_value, parse_headers(field_value.headers)) |
paulb@460 | 415 | else: |
paulb@714 | 416 | return FileContent(field_value.file, parse_headers(field_value.headers)) |
paulb@460 | 417 | else: |
paulb@460 | 418 | return get_body_field(field_value, encoding) |
paulb@460 | 419 | |
paulb@199 | 420 | def get_body_field(field_str, encoding): |
paulb@199 | 421 | |
paulb@199 | 422 | """ |
paulb@199 | 423 | Returns the appropriate value for the given 'field_str' string using the |
paulb@199 | 424 | given 'encoding'. |
paulb@199 | 425 | """ |
paulb@199 | 426 | |
paulb@460 | 427 | # Detect stray FieldStorage objects (eg. with Webware). |
paulb@199 | 428 | |
paulb@199 | 429 | if hasattr(field_str, "value"): |
paulb@199 | 430 | return get_body_field(field_str.value, encoding) |
paulb@440 | 431 | else: |
paulb@440 | 432 | return decode_value(field_str, encoding) |
paulb@440 | 433 | |
paulb@440 | 434 | def decode_value(s, encoding): |
paulb@440 | 435 | if encoding is not None: |
paulb@250 | 436 | try: |
paulb@440 | 437 | return unicode(s, encoding) |
paulb@250 | 438 | except UnicodeError: |
paulb@440 | 439 | pass |
paulb@440 | 440 | # NOTE: Hacks to permit graceful failure. |
paulb@440 | 441 | return unicode(s, "iso-8859-1") |
paulb@199 | 442 | |
paulb@229 | 443 | def get_fields_from_query_string(query_string, decoder): |
paulb@229 | 444 | |
paulb@229 | 445 | """ |
paulb@229 | 446 | Returns a dictionary mapping field names to lists of values for the data |
paulb@229 | 447 | encoded in the given 'query_string'. Use the given 'decoder' function or |
paulb@229 | 448 | method to process the URL-encoded values. |
paulb@229 | 449 | """ |
paulb@229 | 450 | |
paulb@229 | 451 | fields = {} |
paulb@229 | 452 | |
paulb@229 | 453 | for pair in query_string.split("&"): |
paulb@229 | 454 | t = pair.split("=") |
paulb@229 | 455 | name = decoder(t[0]) |
paulb@229 | 456 | |
paulb@229 | 457 | if len(t) == 2: |
paulb@229 | 458 | value = decoder(t[1]) |
paulb@229 | 459 | else: |
paulb@229 | 460 | value = "" |
paulb@229 | 461 | |
paulb@289 | 462 | # NOTE: Remove empty names. |
paulb@289 | 463 | |
paulb@289 | 464 | if name: |
paulb@289 | 465 | if not fields.has_key(name): |
paulb@289 | 466 | fields[name] = [] |
paulb@289 | 467 | fields[name].append(value) |
paulb@229 | 468 | |
paulb@229 | 469 | return fields |
paulb@229 | 470 | |
paulb@250 | 471 | def filter_fields(all_fields, fields_from_path): |
paulb@250 | 472 | |
paulb@250 | 473 | """ |
paulb@250 | 474 | Taking items from the 'all_fields' dictionary, produce a new dictionary |
paulb@250 | 475 | which does not contain items from the 'fields_from_path' dictionary. |
paulb@250 | 476 | Return a new dictionary. |
paulb@250 | 477 | """ |
paulb@250 | 478 | |
paulb@250 | 479 | fields = {} |
paulb@250 | 480 | for field_name, field_values in all_fields.items(): |
paulb@250 | 481 | |
paulb@250 | 482 | # Find the path values for this field (for filtering below). |
paulb@250 | 483 | |
paulb@250 | 484 | if fields_from_path.has_key(field_name): |
paulb@250 | 485 | field_from_path_values = fields_from_path[field_name] |
paulb@250 | 486 | if type(field_from_path_values) != type([]): |
paulb@250 | 487 | field_from_path_values = [field_from_path_values] |
paulb@250 | 488 | else: |
paulb@250 | 489 | field_from_path_values = [] |
paulb@250 | 490 | |
paulb@250 | 491 | fields[field_name] = [] |
paulb@250 | 492 | for field_value in field_values: |
paulb@250 | 493 | |
paulb@250 | 494 | # Filter path values. |
paulb@250 | 495 | |
paulb@250 | 496 | if field_value not in field_from_path_values: |
paulb@250 | 497 | fields[field_name].append(field_value) |
paulb@250 | 498 | |
paulb@250 | 499 | # Remove filtered fields. |
paulb@250 | 500 | |
paulb@250 | 501 | if fields[field_name] == []: |
paulb@250 | 502 | del fields[field_name] |
paulb@250 | 503 | |
paulb@250 | 504 | return fields |
paulb@250 | 505 | |
paulb@46 | 506 | # vim: tabstop=4 expandtab shiftwidth=4 |