Decode search results to Unicode and print as Unicode
authorW. Trevor King <wking@tremily.us>
Sun, 20 Jan 2013 14:33:47 +0000 (09:33 -0500)
committerW. Trevor King <wking@tremily.us>
Sun, 20 Jan 2013 18:05:18 +0000 (13:05 -0500)
LDAPv3 strings are encoded in UTF-8 [1], so use that encoding to
decode them in format_columns().  Adjust format_entry() and the main
block to handle the Unicode output from format_columns().

[1]: http://tools.ietf.org/html/rfc2251#section-4.1.2

mutt-ldap.py

index 808f6360fcbee82855dae093f4978225f4b7a615..3938d5bbdd79fc58d3177d382f177b9b3f03542d 100755 (executable)
@@ -121,11 +121,11 @@ def search(query, connection):
                 yield entry
 
 def format_columns(address, data):
-    yield address
-    yield data.get('displayName', data['cn'])[-1]
+    yield unicode(address, 'utf-8')
+    yield unicode(data.get('displayName', data['cn'])[-1], 'utf-8')
     optional_column = CONFIG.get('results', 'optional_column')
     if optional_column in data:
-        yield data[optional_column][-1]
+        yield unicode(data[optional_column][-1], 'utf-8')
 
 def format_entry(entry):
     cn,data = entry
@@ -133,7 +133,7 @@ def format_entry(entry):
         for m in data['mail']:
             # http://www.mutt.org/doc/manual/manual-4.html#ss4.5
             # Describes the format mutt expects: address\tname
-            yield "\t".join(format_columns(m, data))
+            yield u'\t'.join(format_columns(m, data))
 
 def cache_filename(query):
     # TODO: is the query filename safe?
@@ -197,5 +197,5 @@ if __name__ == '__main__':
             if connection:
                 connection.unbind()
 
-    print('{0} addresses found:'.format(len(addresses)))
-    print('\n'.join(addresses))
+    print(u'{0} addresses found:'.format(len(addresses)))
+    print(u'\n'.join(addresses))