mirror of
https://github.com/gryf/ebook-converter.git
synced 2026-04-24 07:01:30 +02:00
Removed Windows bits.
This commit is contained in:
@@ -1,27 +1,11 @@
|
||||
import os, sys, re
|
||||
|
||||
try:
|
||||
import ctypes.wintypes
|
||||
iswindows = True
|
||||
except ValueError:
|
||||
iswindows = False
|
||||
import fcntl, termios, struct
|
||||
|
||||
|
||||
def fmt(code):
|
||||
return '\033[%dm' % code
|
||||
|
||||
|
||||
if iswindows:
|
||||
class CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
|
||||
_fields_ = [
|
||||
('dwSize', ctypes.wintypes._COORD),
|
||||
('dwCursorPosition', ctypes.wintypes._COORD),
|
||||
('wAttributes', ctypes.wintypes.WORD),
|
||||
('srWindow', ctypes.wintypes._SMALL_RECT),
|
||||
('dwMaximumWindowSize', ctypes.wintypes._COORD)
|
||||
]
|
||||
|
||||
|
||||
RATTRIBUTES = dict(
|
||||
zip(range(1, 9), (
|
||||
'bold',
|
||||
@@ -65,21 +49,6 @@ COLORS = {v:fmt(k) for k, v in RCOLORS.items()}
|
||||
|
||||
RESET = fmt(0)
|
||||
|
||||
if iswindows:
|
||||
# From wincon.h
|
||||
WCOLORS = {c:i for i, c in enumerate((
|
||||
'black', 'blue', 'green', 'cyan', 'red', 'magenta', 'yellow', 'white'))}
|
||||
|
||||
def to_flag(fg, bg, bold):
|
||||
val = 0
|
||||
if bold:
|
||||
val |= 0x08
|
||||
if fg in WCOLORS:
|
||||
val |= WCOLORS[fg]
|
||||
if bg in WCOLORS:
|
||||
val |= (WCOLORS[bg] << 4)
|
||||
return val
|
||||
|
||||
|
||||
def colored(text, fg=None, bg=None, bold=False):
|
||||
prefix = []
|
||||
@@ -100,76 +69,14 @@ def colored(text, fg=None, bg=None, bold=False):
|
||||
class Detect(object):
|
||||
|
||||
def __init__(self, stream):
|
||||
__import__('pdb').set_trace()
|
||||
self.stream = stream or sys.stdout
|
||||
self.isatty = getattr(self.stream, 'isatty', lambda : False)()
|
||||
force_ansi = False
|
||||
if not self.isatty and force_ansi:
|
||||
self.isatty = True
|
||||
self.isansi = force_ansi or not iswindows
|
||||
self.isansi = force_ansi or not False
|
||||
self.set_console = self.write_console = None
|
||||
self.is_console = False
|
||||
if not self.isansi:
|
||||
try:
|
||||
import msvcrt
|
||||
self.msvcrt = msvcrt
|
||||
self.file_handle = msvcrt.get_osfhandle(self.stream.fileno())
|
||||
from ctypes import windll, wintypes, byref, POINTER, WinDLL
|
||||
mode = wintypes.DWORD(0)
|
||||
f = windll.kernel32.GetConsoleMode
|
||||
f.argtypes, f.restype = [wintypes.HANDLE, POINTER(wintypes.DWORD)], wintypes.BOOL
|
||||
if f(self.file_handle, byref(mode)):
|
||||
# Stream is a console
|
||||
self.set_console = windll.kernel32.SetConsoleTextAttribute
|
||||
self.default_console_text_attributes = WCOLORS['white']
|
||||
kernel32 = WinDLL('kernel32', use_last_error=True)
|
||||
self.write_console = kernel32.WriteConsoleW
|
||||
self.write_console.argtypes = [wintypes.HANDLE, wintypes.c_wchar_p, wintypes.DWORD, POINTER(wintypes.DWORD), wintypes.LPVOID]
|
||||
self.write_console.restype = wintypes.BOOL
|
||||
kernel32.GetConsoleScreenBufferInfo.argtypes = [wintypes.HANDLE, ctypes.POINTER(CONSOLE_SCREEN_BUFFER_INFO)]
|
||||
kernel32.GetConsoleScreenBufferInfo.restype = wintypes.BOOL
|
||||
csbi = CONSOLE_SCREEN_BUFFER_INFO()
|
||||
if kernel32.GetConsoleScreenBufferInfo(self.file_handle, byref(csbi)):
|
||||
self.default_console_text_attributes = csbi.wAttributes
|
||||
self.is_console = True
|
||||
except:
|
||||
pass
|
||||
|
||||
def write_unicode_text(self, text, ignore_errors=False):
|
||||
' Windows only method that writes unicode strings correctly to the windows console using the Win32 API '
|
||||
if self.is_console:
|
||||
from ctypes import wintypes, byref, c_wchar_p
|
||||
written = wintypes.DWORD(0)
|
||||
text = text.replace('\0', '')
|
||||
chunk = len(text)
|
||||
while text:
|
||||
t, text = text[:chunk], text[chunk:]
|
||||
wt = c_wchar_p(t)
|
||||
text_len = len(t.encode('utf-16'))
|
||||
|
||||
if not self.write_console(self.file_handle, wt, text_len, byref(written), None):
|
||||
# Older versions of windows can fail to write large strings
|
||||
# to console with WriteConsoleW (seen it happen on Win XP)
|
||||
import winerror
|
||||
err = ctypes.get_last_error()
|
||||
if err == winerror.ERROR_NOT_ENOUGH_MEMORY and chunk >= 128:
|
||||
# Retry with a smaller chunk size (give up if chunk < 128)
|
||||
chunk = chunk // 2
|
||||
text = t + text
|
||||
continue
|
||||
if err == winerror.ERROR_GEN_FAILURE:
|
||||
# On newer windows, this happens when trying to write
|
||||
# non-ascii chars to the console and the console is set
|
||||
# to use raster fonts (the default). In this case
|
||||
# rather than failing, write an informative error
|
||||
# message and the asciized version of the text.
|
||||
print('Non-ASCII text detected. You must set your Console\'s font to'
|
||||
' Lucida Console or Consolas or some other TrueType font to see this text', file=self.stream, end=' -- ')
|
||||
from ebook_converter.utils.filenames import ascii_text
|
||||
print(ascii_text(t + text), file=self.stream, end='')
|
||||
continue
|
||||
if not ignore_errors:
|
||||
raise ctypes.WinError(err)
|
||||
|
||||
|
||||
class ColoredStream(Detect):
|
||||
@@ -178,10 +85,6 @@ class ColoredStream(Detect):
|
||||
stream = getattr(stream, 'buffer', stream)
|
||||
Detect.__init__(self, stream)
|
||||
self.fg, self.bg, self.bold = fg, bg, bold
|
||||
if self.set_console is not None:
|
||||
self.wval = to_flag(self.fg, self.bg, bold)
|
||||
if not self.bg:
|
||||
self.wval |= self.default_console_text_attributes & 0xF0
|
||||
|
||||
def cwrite(self, what):
|
||||
if not isinstance(what, bytes):
|
||||
@@ -286,13 +189,6 @@ class ANSIStream(Detect):
|
||||
def write_plain_text(self, text, start, end):
|
||||
if start < end:
|
||||
text = text[start:end]
|
||||
if self.is_console and isinstance(text, bytes):
|
||||
try:
|
||||
utext = text.decode(self.encoding)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
return self.write_unicode_text(utext)
|
||||
self.polyglot_write(text)
|
||||
|
||||
def convert_ansi(self, paramstring, command):
|
||||
@@ -326,60 +222,11 @@ class ANSIStream(Detect):
|
||||
fg, bg, bold = None, None, False
|
||||
|
||||
self.last_state = (fg, bg, bold)
|
||||
if fg or bg or bold:
|
||||
val = to_flag(fg, bg, bold)
|
||||
if not bg:
|
||||
val |= self.default_console_text_attributes & 0xF0
|
||||
self.set_console(self.file_handle, val)
|
||||
else:
|
||||
self.set_console(self.file_handle, self.default_console_text_attributes)
|
||||
|
||||
|
||||
def windows_terminfo():
|
||||
from ctypes import Structure, byref
|
||||
from ctypes.wintypes import SHORT, WORD
|
||||
|
||||
class COORD(Structure):
|
||||
|
||||
"""struct in wincon.h"""
|
||||
_fields_ = [
|
||||
('X', SHORT),
|
||||
('Y', SHORT),
|
||||
]
|
||||
|
||||
class SMALL_RECT(Structure):
|
||||
|
||||
"""struct in wincon.h."""
|
||||
_fields_ = [
|
||||
("Left", SHORT),
|
||||
("Top", SHORT),
|
||||
("Right", SHORT),
|
||||
("Bottom", SHORT),
|
||||
]
|
||||
|
||||
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
|
||||
|
||||
"""struct in wincon.h."""
|
||||
_fields_ = [
|
||||
("dwSize", COORD),
|
||||
("dwCursorPosition", COORD),
|
||||
("wAttributes", WORD),
|
||||
("srWindow", SMALL_RECT),
|
||||
("dwMaximumWindowSize", COORD),
|
||||
]
|
||||
csbi = CONSOLE_SCREEN_BUFFER_INFO()
|
||||
import msvcrt
|
||||
file_handle = msvcrt.get_osfhandle(sys.stdout.fileno())
|
||||
from ctypes import windll
|
||||
success = windll.kernel32.GetConsoleScreenBufferInfo(file_handle,
|
||||
byref(csbi))
|
||||
if not success:
|
||||
raise Exception('stdout is not a console?')
|
||||
return csbi
|
||||
self.set_console(self.file_handle, self.default_console_text_attributes)
|
||||
|
||||
|
||||
def get_term_geometry():
|
||||
import fcntl, termios, struct
|
||||
|
||||
def ioctl_GWINSZ(fd):
|
||||
try:
|
||||
@@ -405,30 +252,10 @@ def get_term_geometry():
|
||||
|
||||
|
||||
def geometry():
|
||||
if iswindows:
|
||||
try:
|
||||
|
||||
ti = windows_terminfo()
|
||||
return (ti.dwSize.X or 80, ti.dwSize.Y or 25)
|
||||
except:
|
||||
return 80, 25
|
||||
else:
|
||||
try:
|
||||
lines, cols = get_term_geometry()
|
||||
if lines is not None:
|
||||
return cols, lines
|
||||
except Exception:
|
||||
pass
|
||||
return 80, 25
|
||||
|
||||
|
||||
def test():
|
||||
s = ANSIStream()
|
||||
|
||||
text = [colored(t, fg=t)+'. '+colored(t, fg=t, bold=True)+'.' for t in
|
||||
('red', 'yellow', 'green', 'white', 'cyan', 'magenta', 'blue',)]
|
||||
s.write('\n'.join(text))
|
||||
u = u'\u041c\u0438\u0445\u0430\u0438\u043b fällen'
|
||||
print()
|
||||
s.write_unicode_text(u)
|
||||
print()
|
||||
try:
|
||||
lines, cols = get_term_geometry()
|
||||
if lines is not None:
|
||||
return cols, lines
|
||||
except Exception:
|
||||
pass
|
||||
return 80, 25
|
||||
|
||||
Reference in New Issue
Block a user