# Copyright (C) 2005-2023 Splunk Inc. All Rights Reserved. import sys import itsi_path # noqa string_type = None ''' Legacy string type: * in python2 it is a basestring, * in python3 environment is set to string and bytes types (to be backwards compatible with python2) ''' ext_string_type = None ''' Extended string type: * in python2 it is a regular "normal" string, bytes, and a unicode * in python3 environment it's "normal" string (that is by default a unicode string) and bytes ''' def decode(string): ''' a helper function to decode string: * in python2 leaves string unchanged since in python2 bytes and str are same * in python3 decodes bytes into a str returns str ''' return string def to_bytes(string): ''' a helper function to convert string to bytes: * in python2 noop, since string is also a byte string * in python3 converts string to bytes using UTF-8 encoding returns bytes ''' return string ############ i18n setup ################ # noqa: E266 def ugettext(message): """ Translate a string message This method is also installed as "_" in builtins """ return message def ungettext(msgid1, msgid2, n): """ Translate a string message with a number in it """ return msgid1 if n == 1 else msgid2 # Check if i18n functions are available (or use stubs) try: from splunk.appserver.mrsparkle.lib import i18n i18n.ugettext('test') # try it out to check if it's functional ugettext = i18n.ugettext # noqa: F811 ungettext = i18n.ungettext # noqa: F811 except Exception: '''No working i18n available - will use stubs for ugettext and ungettext''' _ = ugettext ############ end of i18n setup ################ # noqa: E266 if sys.version_info >= (3, 0): #################################################### # Python 3: specific types, functions and settings # #################################################### string_type = (str, bytes) ext_string_type = (str, bytes) ''' Unicode type: * in python2 it is a regular unicode type * in python3 it is a str, since strings are unicode by default ''' unicode = str def py3decode(string): if type(string) is bytes: return string.decode() return string decode = py3decode # noqa: F811 def py3_to_bytes(string): if type(string) is str: return bytes(string, encoding='UTF-8') return string to_bytes = py3_to_bytes # noqa: F811 import builtins # global i18n definitions builtins.__dict__['_'] = ugettext builtins.__dict__['ungettext'] = ungettext else: #################################################### # Python 2: specific types, functions and settings # #################################################### import __builtin__ string_type = __builtin__.basestring ext_string_type = (str, unicode) unicode = unicode # global i18n definitions __builtin__.__dict__['_'] = ugettext __builtin__.__dict__['ungettext'] = ungettext