cengal.user_interface.console.encoding_changer.versions.v_0.encoding_changer
1#!/usr/bin/env python 2# coding=utf-8 3 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space> 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18import sys 19import os 20from contextlib import contextmanager, redirect_stdout, redirect_stderr 21from cengal.system import IS_RUNNING_IN_PYCHARM 22if 'nt' == os.name: 23 import win32console 24 import codecs 25 26 27""" 28Module Docstring 29Docstrings: http://www.python.org/dev/peps/pep-0257/ 30""" 31 32__author__ = "ButenkoMS <gtalk@butenkoms.space>" 33__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>" 34__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ] 35__license__ = "Apache License, Version 2.0" 36__version__ = "4.4.1" 37__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>" 38__email__ = "gtalk@butenkoms.space" 39# __status__ = "Prototype" 40__status__ = "Development" 41# __status__ = "Production" 42 43 44# if 'nt' == os.name: 45# @contextmanager 46# def change_console_output_cp(new_output_cp=None, new_input_cp=None, encoding_error=None): 47# """ 48# :param new_output_cp: ResultExistence(True, (65001, 'utf-8)) 49# :param new_input_cp: ResultExistence(True, (65001, 'utf-8)) 50# :return: 51# """ 52# encoding_error = encoding_error or 'replace' 53# cur_output_cp = None 54# cur_input_cp = None 55# cur_output_cp_name = str() 56# cur_input_cp_name = str() 57# original_stdout = sys.stdout 58# original_stderr = sys.stderr 59# original_stdin = sys.stdin 60# original_stdout_fileno = sys.stdout.fileno() 61# original_stderr_fileno = sys.stderr.fileno() 62# original_stdin_fileno = sys.stdin.fileno() 63# if 'nt' == os.name: 64# if not IS_RUNNING_IN_PYCHARM: 65# cur_output_cp = win32console.GetConsoleOutputCP() 66# cur_output_cp_name = sys.stdout.encoding 67# if (new_output_cp is not None) and new_output_cp.existence: 68# # sys.stdout = codecs.getwriter(new_output_cp.result[1])(sys.stdout.buffer, encoding_error) 69# # sys.stderr = codecs.getwriter(new_output_cp.result[1])(sys.stderr.buffer, encoding_error) 70# # sys.stdout = codecs.getwriter(new_output_cp.result[1])(sys.stdout.detach(), encoding_error) 71# # sys.stderr = codecs.getwriter(new_output_cp.result[1])(sys.stderr.detach(), encoding_error) 72# sys.stdout = open(original_stdout_fileno, mode='w', encoding=new_output_cp.result[1], buffering=1) 73# print('stdout:', sys.stdout.encoding) 74# sys.stderr = open(original_stderr_fileno, mode='w', encoding=new_output_cp.result[1], buffering=1) 75# print('stderr:', sys.stderr.encoding) 76# win32console.SetConsoleOutputCP(new_output_cp.result[0]) 77# print('conout:', win32console.GetConsoleOutputCP()) 78# cur_input_cp = win32console.GetConsoleCP() 79# cur_input_cp_name = sys.stdin.encoding 80# if (new_input_cp is not None) and new_input_cp.existence: 81# # sys.stdin = codecs.getwriter(new_input_cp.result[1])(sys.stdin.buffer, encoding_error) 82# sys.stdin = open(original_stdin_fileno, mode='r', encoding=new_input_cp.result[1], buffering=1) 83# print('stdin:', sys.stdin.encoding) 84# win32console.SetConsoleCP(new_input_cp.result[0]) 85# print('conin:', win32console.GetConsoleOutputCP()) 86# try: 87# yield ((cur_input_cp, cur_output_cp), (cur_input_cp_name, cur_output_cp_name)) 88# except: 89# raise 90# finally: 91# if 'nt' == os.name: 92# if not IS_RUNNING_IN_PYCHARM: 93# if (new_output_cp is not None) and new_output_cp.existence: 94# win32console.SetConsoleOutputCP(cur_output_cp) 95# print('conout:', win32console.GetConsoleOutputCP()) 96# # sys.stdout.close() 97# # sys.stdout = original_stdout 98# # sys.stderr = original_stderr 99# # sys.stdout = open(original_stdout_fileno, mode='w', encoding=cur_output_cp_name, buffering=1) 100# print('stdout:', sys.stdout.encoding) 101# # sys.stderr.close() 102# # sys.stderr = open(original_stderr_fileno, mode='w', encoding=cur_output_cp_name, buffering=1) 103# print('stderr:', sys.stderr.encoding) 104# if (new_input_cp is not None) and new_input_cp.existence: 105# win32console.SetConsoleCP(cur_input_cp) 106# print('conin:', win32console.GetConsoleOutputCP()) 107# # sys.stdin.close() 108# # sys.stdin = original_stdin 109# # sys.stdin = open(original_stdin_fileno, mode='r', encoding=cur_input_cp_name, buffering=1) 110# print('stdin:', sys.stdin.encoding) 111# else: 112# @contextmanager 113# def change_console_output_cp(new_output_cp=None, new_input_cp=None, encoding_error=None): 114# yield None 115 116 117def fileno(file_or_fd): 118 fd = getattr(file_or_fd, 'fileno', lambda: file_or_fd)() 119 if not isinstance(fd, int): 120 raise ValueError("Expected a file (`.fileno()`) or a file descriptor") 121 return fd 122 123 124if 'nt' == os.name: 125 @contextmanager 126 def change_console_output_cp(new_output_cp=None, new_input_cp=None, encoding_error=None): 127 """ 128 :param new_output_cp: ResultExistence(True, (65001, 'utf-8)) 129 :param new_input_cp: ResultExistence(True, (65001, 'utf-8)) 130 :return: 131 """ 132 encoding_error = encoding_error or 'replace' 133 cur_output_cp = None 134 cur_input_cp = None 135 cur_output_cp_name = str() 136 cur_input_cp_name = str() 137 original_stdout = sys.stdout 138 original_stderr = sys.stderr 139 original_stdin = sys.stdin 140 original_stdout_fileno = sys.stdout.fileno() 141 original_stderr_fileno = sys.stderr.fileno() 142 original_stdin_fileno = sys.stdin.fileno() 143 new_stdout = None 144 new_stderr = None 145 new_stdin = None 146 if 'nt' == os.name: 147 if IS_RUNNING_IN_PYCHARM: 148 yield None 149 else: 150 with os.fdopen(os.dup(original_stdout_fileno), 'wb') as copied_stdout, \ 151 os.fdopen(os.dup(original_stderr_fileno), 'wb') as copied_stderr, \ 152 os.fdopen(os.dup(original_stdin_fileno), 'rb') as copied_stdin, \ 153 os.fdopen(os.dup(original_stdout_fileno), 'wb') as copied_stdout_buff, \ 154 os.fdopen(os.dup(original_stderr_fileno), 'wb') as copied_stderr_buff, \ 155 os.fdopen(os.dup(original_stdin_fileno), 'rb') as copied_stdin_buff: 156 # copied_stdout = os.fdopen(os.dup(original_stdout_fileno), 'wb') 157 # copied_stderr = os.fdopen(os.dup(original_stderr_fileno), 'wb') 158 # copied_stdin = os.fdopen(os.dup(original_stdin_fileno), 'wb') 159 # copied_stdout_buff = os.fdopen(os.dup(original_stdout_fileno), 'wb') 160 # copied_stderr_buff = os.fdopen(os.dup(original_stderr_fileno), 'wb') 161 # copied_stdin_buff = os.fdopen(os.dup(original_stdin_fileno), 'wb') 162 original_stdout.flush() 163 original_stderr.flush() 164 original_stdin.flush() 165 166 cur_output_cp = win32console.GetConsoleOutputCP() 167 cur_output_cp_name = sys.stdout.encoding 168 if (new_output_cp is not None) and new_output_cp.existence: 169 new_stdout = open(copied_stdout.fileno(), mode='w', encoding=new_output_cp.result[1], 170 buffering=1) 171 sys.stdout = new_stdout 172 # print('stdout:', sys.stdout.encoding) 173 new_stderr = open(copied_stderr.fileno(), mode='w', encoding=new_output_cp.result[1], 174 buffering=1) 175 sys.stderr = new_stderr 176 # print('stderr:', sys.stderr.encoding) 177 win32console.SetConsoleOutputCP(new_output_cp.result[0]) 178 # print('conout:', win32console.GetConsoleOutputCP()) 179 cur_input_cp = win32console.GetConsoleCP() 180 cur_input_cp_name = sys.stdin.encoding 181 if (new_input_cp is not None) and new_input_cp.existence: 182 new_stdin = open(copied_stdin.fileno(), mode='r', encoding=new_input_cp.result[1], 183 buffering=1) 184 sys.stdin = new_stdin 185 # print('stdin:', sys.stdin.encoding) 186 win32console.SetConsoleCP(new_input_cp.result[0]) 187 # print('conin:', win32console.GetConsoleOutputCP()) 188 try: 189 yield ((cur_input_cp, cur_output_cp), (cur_input_cp_name, cur_output_cp_name)) 190 except: 191 raise 192 finally: 193 sys.stdout.flush() 194 sys.stderr.flush() 195 sys.stdin.flush() 196 197 if (new_output_cp is not None) and new_output_cp.existence: 198 win32console.SetConsoleOutputCP(cur_output_cp) 199 # print('conout:', win32console.GetConsoleOutputCP()) 200 os.dup2(copied_stdout_buff.fileno(), original_stdout_fileno) 201 sys.stdout = original_stdout 202 # print('stdout:', sys.stdout.encoding) 203 os.dup2(copied_stderr_buff.fileno(), original_stderr_fileno) 204 sys.stderr = original_stderr 205 # print('stderr:', sys.stderr.encoding) 206 if (new_input_cp is not None) and new_input_cp.existence: 207 win32console.SetConsoleCP(cur_input_cp) 208 # print('conin:', win32console.GetConsoleOutputCP()) 209 os.dup2(copied_stdin_buff.fileno(), original_stdin_fileno) 210 sys.stdin = original_stdin 211 # print('stdin:', sys.stdin.encoding) 212 else: 213 yield None 214else: 215 @contextmanager 216 def change_console_output_cp(new_output_cp=None, new_input_cp=None, encoding_error=None): 217 yield None
def
fileno(file_or_fd):
@contextmanager
def
change_console_output_cp(new_output_cp=None, new_input_cp=None, encoding_error=None):