Python locale.getpreferredencoding() Examples
The following are 30
code examples of locale.getpreferredencoding().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
locale
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: __init__.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def read_text_file(filename): """Return the contents of *filename*. Try to decode the file contents with utf-8, the preferred system encoding (e.g., cp1252 on some Windows machines), and latin1, in that order. Decoding a byte string with latin1 will never raise an error. In the worst case, the returned string will contain some garbage characters. """ with open(filename, 'rb') as fp: data = fp.read() encodings = ['utf-8', locale.getpreferredencoding(False), 'latin1'] for enc in encodings: try: data = data.decode(enc) except UnicodeDecodeError: continue break assert type(data) != bytes # Latin1 should have worked. return data
Example #2
Source File: __init__.py From vnpy_crypto with MIT License | 6 votes |
def read_text_file(filename): """Return the contents of *filename*. Try to decode the file contents with utf-8, the preferred system encoding (e.g., cp1252 on some Windows machines), and latin1, in that order. Decoding a byte string with latin1 will never raise an error. In the worst case, the returned string will contain some garbage characters. """ with open(filename, 'rb') as fp: data = fp.read() encodings = ['utf-8', locale.getpreferredencoding(False), 'latin1'] for enc in encodings: try: data = data.decode(enc) except UnicodeDecodeError: continue break assert type(data) != bytes # Latin1 should have worked. return data
Example #3
Source File: support.py From sndlatr with Apache License 2.0 | 6 votes |
def lnpgettext(self, context, singular, plural, num): """Equivalent to ``npgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. """ ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular) try: tmsg = self._catalog[(ctxt_msg_id, self.plural(num))] if self._output_charset: return tmsg.encode(self._output_charset) return tmsg.encode(locale.getpreferredencoding()) except KeyError: if self._fallback: return self._fallback.lnpgettext(context, singular, plural, num) if num == 1: return singular else: return plural
Example #4
Source File: __init__.py From pyspelling with MIT License | 6 votes |
def get_process_output(process, encoding=None): """Get the output from the process.""" output = process.communicate() returncode = process.returncode if not encoding: try: encoding = sys.stdout.encoding except Exception: encoding = locale.getpreferredencoding() if returncode != 0: raise RuntimeError("Runtime Error: %s" % (output[0].rstrip().decode(encoding, errors='replace'))) return output[0].decode(encoding, errors='replace')
Example #5
Source File: exec_command.py From recruit with Apache License 2.0 | 6 votes |
def filepath_from_subprocess_output(output): """ Convert `bytes` in the encoding used by a subprocess into a filesystem-appropriate `str`. Inherited from `exec_command`, and possibly incorrect. """ mylocale = locale.getpreferredencoding(False) if mylocale is None: mylocale = 'ascii' output = output.decode(mylocale, errors='replace') output = output.replace('\r\n', '\n') # Another historical oddity if output[-1:] == '\n': output = output[:-1] # stdio uses bytes in python 2, so to avoid issues, we simply # remove all non-ascii characters if sys.version_info < (3, 0): output = output.encode('ascii', errors='replace') return output
Example #6
Source File: configuration.py From FuYiSpider with Apache License 2.0 | 6 votes |
def _construct_parser(self, fname): # type: (str) -> RawConfigParser parser = configparser.RawConfigParser() # If there is no such file, don't bother reading it but create the # parser anyway, to hold the data. # Doing this is useful when modifying and saving files, where we don't # need to construct a parser. if os.path.exists(fname): try: parser.read(fname) except UnicodeDecodeError: raise ConfigurationError(( "ERROR: " "Configuration file contains invalid %s characters.\n" "Please fix your configuration, located at %s\n" ) % (locale.getpreferredencoding(False), fname)) return parser
Example #7
Source File: __init__.py From Python24 with MIT License | 6 votes |
def main(args=None): if args is None: args = sys.argv[1:] # Configure our deprecation warnings to be sent through loggers deprecation.install_warning_logger() autocomplete() try: cmd_name, cmd_args = parseopts(args) except PipError as exc: sys.stderr.write("ERROR: %s" % exc) sys.stderr.write(os.linesep) sys.exit(1) # Needed for locale.getpreferredencoding(False) to work # in pip._internal.utils.encoding.auto_decode try: locale.setlocale(locale.LC_ALL, '') except locale.Error as e: # setlocale can apparently crash if locale are uninitialized logger.debug("Ignoring error %s when setting locale", e) command = commands_dict[cmd_name](isolated=check_isolated(cmd_args)) return command.main(cmd_args)
Example #8
Source File: misc.py From Python24 with MIT License | 6 votes |
def read_text_file(filename): """Return the contents of *filename*. Try to decode the file contents with utf-8, the preferred system encoding (e.g., cp1252 on some Windows machines), and latin1, in that order. Decoding a byte string with latin1 will never raise an error. In the worst case, the returned string will contain some garbage characters. """ with open(filename, 'rb') as fp: data = fp.read() encodings = ['utf-8', locale.getpreferredencoding(False), 'latin1'] for enc in encodings: try: data = data.decode(enc) except UnicodeDecodeError: continue break assert type(data) != bytes # Latin1 should have worked. return data
Example #9
Source File: __init__.py From jbox with MIT License | 6 votes |
def main(args=None): if args is None: args = sys.argv[1:] # Configure our deprecation warnings to be sent through loggers deprecation.install_warning_logger() autocomplete() try: cmd_name, cmd_args = parseopts(args) except PipError as exc: sys.stderr.write("ERROR: %s" % exc) sys.stderr.write(os.linesep) sys.exit(1) # Needed for locale.getpreferredencoding(False) to work # in pip.utils.encoding.auto_decode locale.setlocale(locale.LC_ALL, '') command = commands_dict[cmd_name](isolated=check_isolated(cmd_args)) return command.main(cmd_args) # ########################################################### # # Writing freeze files
Example #10
Source File: __init__.py From jbox with MIT License | 6 votes |
def read_text_file(filename): """Return the contents of *filename*. Try to decode the file contents with utf-8, the preferred system encoding (e.g., cp1252 on some Windows machines), and latin1, in that order. Decoding a byte string with latin1 will never raise an error. In the worst case, the returned string will contain some garbage characters. """ with open(filename, 'rb') as fp: data = fp.read() encodings = ['utf-8', locale.getpreferredencoding(False), 'latin1'] for enc in encodings: try: data = data.decode(enc) except UnicodeDecodeError: continue break assert type(data) != bytes # Latin1 should have worked. return data
Example #11
Source File: configuration.py From Python24 with MIT License | 6 votes |
def _construct_parser(self, fname): # type: (str) -> RawConfigParser parser = configparser.RawConfigParser() # If there is no such file, don't bother reading it but create the # parser anyway, to hold the data. # Doing this is useful when modifying and saving files, where we don't # need to construct a parser. if os.path.exists(fname): try: parser.read(fname) except UnicodeDecodeError: raise ConfigurationError(( "ERROR: " "Configuration file contains invalid %s characters.\n" "Please fix your configuration, located at %s\n" ) % (locale.getpreferredencoding(False), fname)) return parser
Example #12
Source File: virsh.py From avocado-vt with GNU General Public License v2.0 | 6 votes |
def secret_set_value(uuid, password, options=None, encode=False, **dargs): """ Set a secret value :param uuid: secret UUID :param password: secret value :param encode: if False, that means you've already provided a base64-encoded password. if True, will base64-encode password before use it. :return: CmdResult object. """ cmd = "secret-set-value --secret %s" % uuid if password: if encode: encoding = locale.getpreferredencoding() cmd += (" --base64 %s" % base64.b64encode(password.encode(encoding)).decode(encoding)) else: cmd += " --base64 %s" % password if options: cmd += " --%s" % options return command(cmd, **dargs)
Example #13
Source File: dired_misc.py From SublimeFileBrowser with MIT License | 6 votes |
def run(self, edit, fname=None): path = self.path if not fname: self.index = self.get_all() files = self.get_selected(parent=False) fname = join(path, files[0] if files else '') else: files = True p, f = os.path.split(fname.rstrip(os.sep)) if not exists(fname): return sublime.status_message(u'Directory doesn’t exist “%s”' % path) if NT and path == 'ThisPC\\': if not ST3: fname = fname.encode(locale.getpreferredencoding(False)) return subprocess.Popen('explorer /select,"%s"' % fname) if files: self.view.window().run_command("open_dir", {"dir": p, "file": f}) else: self.view.window().run_command("open_dir", {"dir": path})
Example #14
Source File: svn_utils.py From pledgeservice with Apache License 2.0 | 6 votes |
def decode_as_string(text, encoding=None): """ Decode the console or file output explicitly using getpreferredencoding. The text paraemeter should be a encoded string, if not no decode occurs If no encoding is given, getpreferredencoding is used. If encoding is specified, that is used instead. This would be needed for SVN --xml output. Unicode is explicitly put in composed NFC form. --xml should be UTF-8 (SVN Issue 2938) the discussion on the Subversion DEV List from 2007 seems to indicate the same. """ #text should be a byte string if encoding is None: encoding = _console_encoding if not isinstance(text, unicode): text = text.decode(encoding) text = unicodedata.normalize('NFC', text) return text
Example #15
Source File: __init__.py From python-netsurv with MIT License | 6 votes |
def read_text_file(filename): """Return the contents of *filename*. Try to decode the file contents with utf-8, the preferred system encoding (e.g., cp1252 on some Windows machines), and latin1, in that order. Decoding a byte string with latin1 will never raise an error. In the worst case, the returned string will contain some garbage characters. """ with open(filename, 'rb') as fp: data = fp.read() encodings = ['utf-8', locale.getpreferredencoding(False), 'latin1'] for enc in encodings: try: data = data.decode(enc) except UnicodeDecodeError: continue break assert type(data) != bytes # Latin1 should have worked. return data
Example #16
Source File: test_adapter.py From django-click with MIT License | 6 votes |
def test_not_ascii(): # NOCOV """ Make sure that the systems preferred encoding is not `ascii`. Otherwise `click` is raising a RuntimeError for Python3. For a detailed description of this very problem please consult the following gist: https://gist.github.com/hackebrot/937245251887197ef542 This test also checks that `tox.ini` explicitly copies the according system environment variables to the test environments. """ try: preferred_encoding = locale.getpreferredencoding() fs_enc = codecs.lookup(preferred_encoding).name except Exception: fs_enc = "ascii" assert fs_enc != "ascii"
Example #17
Source File: __init__.py From python-netsurv with MIT License | 6 votes |
def read_text_file(filename): """Return the contents of *filename*. Try to decode the file contents with utf-8, the preferred system encoding (e.g., cp1252 on some Windows machines), and latin1, in that order. Decoding a byte string with latin1 will never raise an error. In the worst case, the returned string will contain some garbage characters. """ with open(filename, 'rb') as fp: data = fp.read() encodings = ['utf-8', locale.getpreferredencoding(False), 'latin1'] for enc in encodings: try: data = data.decode(enc) except UnicodeDecodeError: continue break assert type(data) != bytes # Latin1 should have worked. return data
Example #18
Source File: __init__.py From kobo-predict with BSD 2-Clause "Simplified" License | 5 votes |
def main(args=None): if args is None: args = sys.argv[1:] # Configure our deprecation warnings to be sent through loggers deprecation.install_warning_logger() autocomplete() try: cmd_name, cmd_args = parseopts(args) except PipError as exc: sys.stderr.write("ERROR: %s" % exc) sys.stderr.write(os.linesep) sys.exit(1) # Needed for locale.getpreferredencoding(False) to work # in pip.utils.encoding.auto_decode try: locale.setlocale(locale.LC_ALL, '') except locale.Error as e: # setlocale can apparently crash if locale are uninitialized logger.debug("Ignoring error %s when setting locale", e) command = commands_dict[cmd_name](isolated=check_isolated(cmd_args)) return command.main(cmd_args) # ########################################################### # # Writing freeze files
Example #19
Source File: gettext.py From ironpython2 with Apache License 2.0 | 5 votes |
def lgettext(self, message): missing = object() tmsg = self._catalog.get(message, missing) if tmsg is missing: if self._fallback: return self._fallback.lgettext(message) return message if self._output_charset: return tmsg.encode(self._output_charset) return tmsg.encode(locale.getpreferredencoding())
Example #20
Source File: invoker.py From bob with GNU General Public License v3.0 | 5 votes |
def getStdio(self): return self.__stdioBuffer.getvalue().decode( locale.getpreferredencoding(), 'surrogateescape')
Example #21
Source File: invoker.py From bob with GNU General Public License v3.0 | 5 votes |
def __print(self, *args, file=DEVNULL, **kwargs): buf = io.StringIO() print(*args, **kwargs, file=buf) binbuf = buf.getvalue().encode(locale.getpreferredencoding()) file.write(binbuf) self.__logFile.write(binbuf) return buf.getvalue()
Example #22
Source File: invoker.py From bob with GNU General Public License v3.0 | 5 votes |
def __closeLog(self, lastExit): try: self.__logFile.write("### END({}): {}\n" .format(lastExit, datetime.datetime.now().ctime()) .encode(locale.getpreferredencoding())) self.__logFile.close() self.__logFile = DEVNULL except OSError as e: self.error("Cannot close log file:", str(e))
Example #23
Source File: support.py From sndlatr with Apache License 2.0 | 5 votes |
def lpgettext(self, context, message): """Equivalent to ``pgettext()``, but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with ``bind_textdomain_codeset()``. """ ctxt_msg_id = self.CONTEXT_ENCODING % (context, message) missing = object() tmsg = self._catalog.get(ctxt_msg_id, missing) if tmsg is missing: if self._fallback: return self._fallback.lpgettext(context, message) return message if self._output_charset: return tmsg.encode(self._output_charset) return tmsg.encode(locale.getpreferredencoding())
Example #24
Source File: invoker.py From bob with GNU General Public License v3.0 | 5 votes |
def __openLog(self, header=None): # Create log file if self.__logFileName: self.__logFile = open(self.__logFileName, "ab", buffering=0) self.__logFile.write("### START: {}{}\n" .format(datetime.datetime.now().ctime(), (" (" + header + ")") if header else "") .encode(locale.getpreferredencoding())) else: self.__logFile = DEVNULL
Example #25
Source File: svn_utils.py From pledgeservice with Apache License 2.0 | 5 votes |
def determine_console_encoding(): try: #try for the preferred encoding encoding = locale.getpreferredencoding() #see if the locale.getdefaultlocale returns null #some versions of python\platforms return US-ASCII #when it cannot determine an encoding if not encoding or encoding == "US-ASCII": encoding = locale.getdefaultlocale()[1] if encoding: codecs.lookup(encoding) # make sure a lookup error is not made except (locale.Error, LookupError): encoding = None is_osx = sys.platform == "darwin" if not encoding: return ["US-ASCII", "utf-8"][is_osx] elif encoding.startswith("mac-") and is_osx: #certain versions of python would return mac-roman as default #OSX as a left over of earlier mac versions. return "utf-8" else: return encoding
Example #26
Source File: gettext.py From meddle with MIT License | 5 votes |
def lngettext(self, msgid1, msgid2, n): try: tmsg = self._catalog[(msgid1, self.plural(n))] if self._output_charset: return tmsg.encode(self._output_charset) return tmsg.encode(locale.getpreferredencoding()) except KeyError: if self._fallback: return self._fallback.lngettext(msgid1, msgid2, n) if n == 1: return msgid1 else: return msgid2
Example #27
Source File: i18n.py From Yuki-Chan-The-Auto-Pentest with MIT License | 5 votes |
def _getTerminalCharset(): """ Function used by getTerminalCharset() to get terminal charset. @see getTerminalCharset() """ # (1) Try locale.getpreferredencoding() try: charset = locale.getpreferredencoding() if charset: return charset except (locale.Error, AttributeError): pass # (2) Try locale.nl_langinfo(CODESET) try: charset = locale.nl_langinfo(locale.CODESET) if charset: return charset except (locale.Error, AttributeError): pass # (3) Try sys.stdout.encoding if hasattr(sys.stdout, "encoding") and sys.stdout.encoding: return sys.stdout.encoding # (4) Otherwise, returns "ASCII" return "ASCII"
Example #28
Source File: gettext.py From meddle with MIT License | 5 votes |
def lgettext(self, message): missing = object() tmsg = self._catalog.get(message, missing) if tmsg is missing: if self._fallback: return self._fallback.lgettext(message) return message if self._output_charset: return tmsg.encode(self._output_charset) return tmsg.encode(locale.getpreferredencoding())
Example #29
Source File: i18n.py From Yuki-Chan-The-Auto-Pentest with MIT License | 5 votes |
def getTerminalCharset(): """ Guess terminal charset using differents tests: 1. Try locale.getpreferredencoding() 2. Try locale.nl_langinfo(CODESET) 3. Try sys.stdout.encoding 4. Otherwise, returns "ASCII" WARNING: Call initLocale() before calling this function. """ try: return getTerminalCharset.value except AttributeError: getTerminalCharset.value = _getTerminalCharset() return getTerminalCharset.value
Example #30
Source File: pythoninfo.py From ironpython2 with Apache License 2.0 | 5 votes |
def collect_locale(info_add): import locale info_add('locale.encoding', locale.getpreferredencoding(False))