From e82677839a0d70da17aee5ce22b1433b8de7e82a Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Sat, 3 Oct 2009 21:52:48 +0200 Subject: [PATCH] [entropy.const] add helper function (unicode handling) --- libraries/entropy/const.py | 42 +++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/libraries/entropy/const.py b/libraries/entropy/const.py index d7a81bae0..b6c946426 100644 --- a/libraries/entropy/const.py +++ b/libraries/entropy/const.py @@ -28,9 +28,6 @@ """ - - - import sys import os import stat @@ -39,7 +36,6 @@ import fcntl import signal import gzip import bz2 - from entropy.i18n import _ @@ -1074,7 +1070,7 @@ def const_create_working_dirs(): pass # Create paths - keys = [x for x in etpConst if isinstance(etpConst[x], basestring)] + keys = [x for x in etpConst if const_isstring(etpConst[x])] for key in keys: if not etpConst[key] or \ @@ -1340,6 +1336,42 @@ def const_add_entropy_group(): group_fw.write(app_line) group_fw.flush() +def const_isstring(obj): + """ + Return whether obj is a string (unicode or raw). + + @param obj: Python object + @type obj: Python object + @return: True, if object is string + @rtype: bool + """ + if sys.hexversion >= 0x3000000: + return isinstance(obj, (str, bytes,)) + else: + return isinstance(obj, basestring) + +def const_isunicode(obj): + """ + Return whether obj is a unicode. + + @param obj: Python object + @type obj: Python object + @return: True, if object is unicode + @rtype: bool + """ + if sys.hexversion >= 0x3000000: + return isinstance(obj, str) + else: + return isinstance(obj, unicode) + +def const_convert_to_unicode(obj): + if const_isunicode(obj): + return obj + if sys.hexversion >= 0x3000000: + return str(obj, 'raw_unicode_escape') + else: + return unicode(obj, 'raw_unicode_escape') + def const_islive(): """ Live environments (Operating System running off a CD/DVD)