Incremental GSC parsing (exact copy of the tutorials shared as a separate project) / tqdm / _utils.py
1576 viewsimport os1import subprocess2from platform import system as _curos3CUR_OS = _curos()4IS_WIN = CUR_OS in ['Windows', 'cli']5IS_NIX = (not IS_WIN) and any(6CUR_OS.startswith(i) for i in7['CYGWIN', 'MSYS', 'Linux', 'Darwin', 'SunOS', 'FreeBSD'])8910if True: # pragma: no cover11try:12_range = xrange13except NameError:14_range = range1516try:17_unich = unichr18except NameError:19_unich = chr2021try:22_unicode = unicode23except NameError:24_unicode = str2526try:27if IS_WIN:28import colorama29colorama.init()30else:31colorama = None32except ImportError:33colorama = None3435try:36from weakref import WeakSet37except ImportError:38WeakSet = set394041def _is_utf(encoding):42return encoding.lower().startswith('utf-') or ('U8' == encoding)434445def _supports_unicode(file):46return _is_utf(file.encoding) if (47getattr(file, 'encoding', None) or48# FakeStreams from things like bpython-curses can lie49getattr(file, 'interface', None)) else False # pragma: no cover505152def _environ_cols_wrapper(): # pragma: no cover53"""54Return a function which gets width and height of console55(linux,osx,windows,cygwin).56"""57_environ_cols = None58if IS_WIN:59_environ_cols = _environ_cols_windows60if _environ_cols is None:61_environ_cols = _environ_cols_tput62if IS_NIX:63_environ_cols = _environ_cols_linux64return _environ_cols656667def _environ_cols_windows(fp): # pragma: no cover68try:69from ctypes import windll, create_string_buffer70import struct71from sys import stdin, stdout7273io_handle = None74if fp == stdin:75io_handle = -1076elif fp == stdout:77io_handle = -1178else: # assume stderr79io_handle = -128081h = windll.kernel32.GetStdHandle(io_handle)82csbi = create_string_buffer(22)83res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)84if res:85(bufx, bufy, curx, cury, wattr, left, top, right, bottom,86maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)87# nlines = bottom - top + 188return right - left # +189except:90pass91return None929394def _environ_cols_tput(*args): # pragma: no cover95""" cygwin xterm (windows) """96try:97import subprocess98import shlex99cols = int(subprocess.check_call(shlex.split('tput cols')))100# rows = int(subprocess.check_call(shlex.split('tput lines')))101return cols102except:103pass104return None105106107def _environ_cols_linux(fp): # pragma: no cover108109try:110from termios import TIOCGWINSZ111from fcntl import ioctl112from array import array113except ImportError:114return None115else:116try:117return array('h', ioctl(fp, TIOCGWINSZ, '\0' * 8))[1]118except:119try:120from os.environ import get121except ImportError:122return None123else:124return int(get('COLUMNS', 1)) - 1125126127def _term_move_up(): # pragma: no cover128return '' if (os.name == 'nt') and (colorama is None) else '\x1b[A'129130131def _sh(*cmd, **kwargs):132return subprocess.Popen(cmd, stdout=subprocess.PIPE,133**kwargs).communicate()[0].decode('utf-8')134135136