1# pycomp.py 2# Python 2 and Python 3 compatibility module 3# 4# Copyright (C) 2013-2016 Red Hat, Inc. 5# 6# This copyrighted material is made available to anyone wishing to use, 7# modify, copy, or redistribute it subject to the terms and conditions of 8# the GNU General Public License v.2, or (at your option) any later version. 9# This program is distributed in the hope that it will be useful, but WITHOUT 10# ANY WARRANTY expressed or implied, including the implied warranties of 11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 12# Public License for more details. You should have received a copy of the 13# GNU General Public License along with this program; if not, write to the 14# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 15# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the 16# source code or documentation are not subject to the GNU General Public 17# License and may only be used or replicated with the express permission of 18# Red Hat, Inc. 19# 20 21from gettext import NullTranslations 22from sys import version_info 23import base64 24import email.mime.text 25import gettext 26import itertools 27import locale 28import sys 29import types 30 31PY3 = version_info.major >= 3 32 33if PY3: 34 from io import StringIO 35 from configparser import ConfigParser 36 import queue 37 import urllib.parse 38 import shlex 39 40 # functions renamed in py3 41 Queue = queue.Queue 42 basestring = unicode = str 43 filterfalse = itertools.filterfalse 44 long = int 45 NullTranslations.ugettext = NullTranslations.gettext 46 NullTranslations.ungettext = NullTranslations.ngettext 47 xrange = range 48 raw_input = input 49 base64_decodebytes = base64.decodebytes 50 urlparse = urllib.parse 51 urllib_quote = urlparse.quote 52 shlex_quote = shlex.quote 53 sys_maxsize = sys.maxsize 54 55 56 def gettext_setup(t): 57 _ = t.gettext 58 P_ = t.ngettext 59 return (_, P_) 60 61 # string helpers 62 def is_py2str_py3bytes(o): 63 return isinstance(o, bytes) 64 def is_py3bytes(o): 65 return isinstance(o, bytes) 66 67 # functions that don't take unicode arguments in py2 68 ModuleType = lambda m: types.ModuleType(m) 69 format = locale.format_string 70 def setlocale(category, loc=None): 71 locale.setlocale(category, loc) 72 def write_to_file(f, content): 73 f.write(content) 74 def email_mime(body): 75 return email.mime.text.MIMEText(body) 76else: 77 # functions renamed in py3 78 from __builtin__ import unicode, basestring, long, xrange, raw_input 79 from StringIO import StringIO 80 from ConfigParser import ConfigParser 81 import Queue 82 import urllib 83 import urlparse 84 import pipes 85 86 Queue = Queue.Queue 87 filterfalse = itertools.ifilterfalse 88 base64_decodebytes = base64.decodestring 89 urllib_quote = urllib.quote 90 shlex_quote = pipes.quote 91 sys_maxsize = sys.maxint 92 93 def gettext_setup(t): 94 _ = t.ugettext 95 P_ = t.ungettext 96 return (_, P_) 97 98 # string helpers 99 def is_py2str_py3bytes(o): 100 return isinstance(o, str) 101 def is_py3bytes(o): 102 return False 103 104 # functions that don't take unicode arguments in py2 105 ModuleType = lambda m: types.ModuleType(m.encode('utf-8')) 106 def format(percent, *args, **kwargs): 107 return locale.format(percent.encode('utf-8'), *args, **kwargs) 108 def setlocale(category, loc=None): 109 locale.setlocale(category, loc.encode('utf-8')) 110 def write_to_file(f, content): 111 f.write(content.encode('utf-8')) 112 def email_mime(body): 113 return email.mime.text.MIMEText(body.encode('utf-8')) 114