# HG changeset patch # User paulb # Date 1094506514 0 # Node ID 455602cb8c98aea138b20623d680c3af3aae4bc0 # Parent e08429cda6d39f1e4fd5232523576a882be2cb3a [project @ 2004-09-06 21:35:14 by paulb] Added a helper function to filter out path field values from field values so that the body field values remain. Added a mechanism to let Unicode conversion fail almost gracefully. diff -r e08429cda6d3 -r 455602cb8c98 WebStack/Helpers/Request.py --- a/WebStack/Helpers/Request.py Mon Sep 06 21:31:04 2004 +0000 +++ b/WebStack/Helpers/Request.py Mon Sep 06 21:35:14 2004 +0000 @@ -130,7 +130,14 @@ elif hasattr(field_str, "read"): return field_str.read() elif encoding is not None: - return unicode(field_str, encoding) + try: + return unicode(field_str, encoding) + except UnicodeError: + # NOTE: Hacks to permit graceful failure. + try: + return unicode(field_str, "iso-8859-1") + except UnicodeError: + return u"" else: return field_str @@ -159,4 +166,39 @@ return fields +def filter_fields(all_fields, fields_from_path): + + """ + Taking items from the 'all_fields' dictionary, produce a new dictionary + which does not contain items from the 'fields_from_path' dictionary. + Return a new dictionary. + """ + + fields = {} + for field_name, field_values in all_fields.items(): + + # Find the path values for this field (for filtering below). + + if fields_from_path.has_key(field_name): + field_from_path_values = fields_from_path[field_name] + if type(field_from_path_values) != type([]): + field_from_path_values = [field_from_path_values] + else: + field_from_path_values = [] + + fields[field_name] = [] + for field_value in field_values: + + # Filter path values. + + if field_value not in field_from_path_values: + fields[field_name].append(field_value) + + # Remove filtered fields. + + if fields[field_name] == []: + del fields[field_name] + + return fields + # vim: tabstop=4 expandtab shiftwidth=4