Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Path: tqdm / _utils.py
Views: 658
1
import os
2
import subprocess
3
from platform import system as _curos
4
CUR_OS = _curos()
5
IS_WIN = CUR_OS in ['Windows', 'cli']
6
IS_NIX = (not IS_WIN) and any(
7
CUR_OS.startswith(i) for i in
8
['CYGWIN', 'MSYS', 'Linux', 'Darwin', 'SunOS', 'FreeBSD'])
9
10
11
if True: # pragma: no cover
12
try:
13
_range = xrange
14
except NameError:
15
_range = range
16
17
try:
18
_unich = unichr
19
except NameError:
20
_unich = chr
21
22
try:
23
_unicode = unicode
24
except NameError:
25
_unicode = str
26
27
try:
28
if IS_WIN:
29
import colorama
30
colorama.init()
31
else:
32
colorama = None
33
except ImportError:
34
colorama = None
35
36
try:
37
from weakref import WeakSet
38
except ImportError:
39
WeakSet = set
40
41
42
def _is_utf(encoding):
43
return encoding.lower().startswith('utf-') or ('U8' == encoding)
44
45
46
def _supports_unicode(file):
47
return _is_utf(file.encoding) if (
48
getattr(file, 'encoding', None) or
49
# FakeStreams from things like bpython-curses can lie
50
getattr(file, 'interface', None)) else False # pragma: no cover
51
52
53
def _environ_cols_wrapper(): # pragma: no cover
54
"""
55
Return a function which gets width and height of console
56
(linux,osx,windows,cygwin).
57
"""
58
_environ_cols = None
59
if IS_WIN:
60
_environ_cols = _environ_cols_windows
61
if _environ_cols is None:
62
_environ_cols = _environ_cols_tput
63
if IS_NIX:
64
_environ_cols = _environ_cols_linux
65
return _environ_cols
66
67
68
def _environ_cols_windows(fp): # pragma: no cover
69
try:
70
from ctypes import windll, create_string_buffer
71
import struct
72
from sys import stdin, stdout
73
74
io_handle = None
75
if fp == stdin:
76
io_handle = -10
77
elif fp == stdout:
78
io_handle = -11
79
else: # assume stderr
80
io_handle = -12
81
82
h = windll.kernel32.GetStdHandle(io_handle)
83
csbi = create_string_buffer(22)
84
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
85
if res:
86
(bufx, bufy, curx, cury, wattr, left, top, right, bottom,
87
maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
88
# nlines = bottom - top + 1
89
return right - left # +1
90
except:
91
pass
92
return None
93
94
95
def _environ_cols_tput(*args): # pragma: no cover
96
""" cygwin xterm (windows) """
97
try:
98
import subprocess
99
import shlex
100
cols = int(subprocess.check_call(shlex.split('tput cols')))
101
# rows = int(subprocess.check_call(shlex.split('tput lines')))
102
return cols
103
except:
104
pass
105
return None
106
107
108
def _environ_cols_linux(fp): # pragma: no cover
109
110
try:
111
from termios import TIOCGWINSZ
112
from fcntl import ioctl
113
from array import array
114
except ImportError:
115
return None
116
else:
117
try:
118
return array('h', ioctl(fp, TIOCGWINSZ, '\0' * 8))[1]
119
except:
120
try:
121
from os.environ import get
122
except ImportError:
123
return None
124
else:
125
return int(get('COLUMNS', 1)) - 1
126
127
128
def _term_move_up(): # pragma: no cover
129
return '' if (os.name == 'nt') and (colorama is None) else '\x1b[A'
130
131
132
def _sh(*cmd, **kwargs):
133
return subprocess.Popen(cmd, stdout=subprocess.PIPE,
134
**kwargs).communicate()[0].decode('utf-8')
135
136