X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2Fcompat.py;h=b2bf149ef63ffb1c31bfb02f976f5cce2dbadad3;hb=575dad3c9842f333c4af27563a26bddaf0015fa2;hp=ec685d50e2b27d5aef32cf97ab7cc33d3f8a7fac;hpb=8f9312c38779f4e0746c01ef9a8a93ab4767699b;p=youtube-dl diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py index ec685d50e..b2bf149ef 100644 --- a/youtube_dl/compat.py +++ b/youtube_dl/compat.py @@ -1,9 +1,11 @@ from __future__ import unicode_literals +import collections import getpass import optparse import os import re +import shutil import socket import subprocess import sys @@ -120,9 +122,9 @@ except NameError: compat_str = str try: - compat_basestr = basestring # Python 2 + compat_basestring = basestring # Python 2 except NameError: - compat_basestr = str + compat_basestring = str try: compat_chr = unichr # Python 2 @@ -364,12 +366,41 @@ def workaround_optparse_bug9161(): return real_add_option(self, *bargs, **bkwargs) optparse.OptionGroup.add_option = _compat_add_option +if hasattr(shutil, 'get_terminal_size'): # Python >= 3.3 + compat_get_terminal_size = shutil.get_terminal_size +else: + _terminal_size = collections.namedtuple('terminal_size', ['columns', 'lines']) + + def compat_get_terminal_size(): + columns = compat_getenv('COLUMNS', None) + if columns: + columns = int(columns) + else: + columns = None + lines = compat_getenv('LINES', None) + if lines: + lines = int(lines) + else: + lines = None + + try: + sp = subprocess.Popen( + ['stty', 'size'], + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = sp.communicate() + lines, columns = map(int, out.split()) + except: + pass + return _terminal_size(columns, lines) + __all__ = [ 'compat_HTTPError', + 'compat_basestring', 'compat_chr', 'compat_cookiejar', 'compat_expanduser', + 'compat_get_terminal_size', 'compat_getenv', 'compat_getpass', 'compat_html_entities',