Add codec translation mapping function finder for unicode←→str conversion.

testing/mmn/mktime_takes_localtime_not_gmtime
Isis Lovecruft 2013-05-22 07:59:21 +00:00
parent 57c83acc31
commit 5a352972b3
No known key found for this signature in database
GPG Key ID: A3ADB67A2CDB8B35
1 changed files with 36 additions and 0 deletions

View File

@ -23,6 +23,8 @@ Extra utilities for python-gnupg.
from datetime import datetime
import codecs
import encodings
import os
import time
import threading
@ -63,6 +65,40 @@ _conf = os.path.join(os.path.join(_user, '.config'), 'python-gnupg')
log = _logger.create_logger(0)
def find_encodings(enc=None, system=False):
"""Find functions for encoding translations for a specific codec.
:param str enc: The codec to find translation functions for. It will be
normalized by converting to lowercase, excluding
everything which is not ascii, and hyphens will be
converted to underscores.
:param bool system: If True, find encodings based on the system's stdin
encoding, otherwise assume utf-8.
:raises: :exc:LookupError if the normalized codec, ``enc``, cannot be
found in Python's encoding translation map.
"""
if not enc:
enc = 'utf-8'
if system:
if getattr(sys.stdin, 'encoding', None) is None:
enc = sys.stdin.encoding
log.debug("Obtained encoding from stdin: %s" % enc)
else:
enc = 'ascii'
## have to have lowercase to work, see
## http://docs.python.org/dev/library/codecs.html#standard-encodings
enc = enc.lower()
codec_alias = encodings.normalize_encoding(enc)
codecs.register(encodings.search_function)
coder = codecs.lookup(codec_alias)
return coder
def _copy_data(instream, outstream):
"""Copy data from one stream to another.