Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/yt-dlp/yt-dlp into ytdlp
Browse files Browse the repository at this point in the history
* 'master' of https://github.com/yt-dlp/yt-dlp:
  Protect stdout from unexpected progress and console-title
  Remove incorrect warning for `--dateafter`
  [MetadataParser] Validate outtmpl early
  Fix case of `http_headers`
  • Loading branch information
Lesmiscore committed Mar 11, 2022
2 parents acf7afa + cf4f42c commit 8559f46
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 42 deletions.
73 changes: 40 additions & 33 deletions yt_dlp/YoutubeDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,15 +600,20 @@ def __init__(self, params=None, auto_init=True):
self._download_retcode = 0
self._num_downloads = 0
self._num_videos = 0
self._screen_file = [sys.stdout, sys.stderr][params.get('logtostderr', False)]
self._err_file = sys.stderr
self.params = params
self.cache = Cache(self)

windows_enable_vt_mode()
self._out_files = {
'error': sys.stderr,
'print': sys.stderr if self.params.get('logtostderr') else sys.stdout,
'console': None if compat_os_name == 'nt' else next(
filter(supports_terminal_sequences, (sys.stderr, sys.stdout)), None)
}
self._out_files['screen'] = sys.stderr if self.params.get('quiet') else self._out_files['print']
self._allow_colors = {
'screen': not self.params.get('no_color') and supports_terminal_sequences(self._screen_file),
'err': not self.params.get('no_color') and supports_terminal_sequences(self._err_file),
type_: not self.params.get('no_color') and supports_terminal_sequences(self._out_files[type_])
for type_ in ('screen', 'error')
}

if sys.version_info < (3, 6):
Expand Down Expand Up @@ -673,7 +678,7 @@ def check_deprecated(param, option, suggestion):
sp_kwargs = dict(
stdin=subprocess.PIPE,
stdout=slave,
stderr=self._err_file)
stderr=self._out_files['error'])
try:
self._output_process = Popen(['bidiv'] + width_args, **sp_kwargs)
except OSError:
Expand Down Expand Up @@ -841,22 +846,37 @@ def _write_string(self, message, out=None, only_once=False):
self._printed_messages.add(message)
write_string(message, out=out, encoding=self.params.get('encoding'))

def to_stdout(self, message, skip_eol=False, quiet=False):
def to_stdout(self, message, skip_eol=False, quiet=None):
"""Print message to stdout"""
if quiet is not None:
self.deprecation_warning('"ydl.to_stdout" no longer accepts the argument quiet. Use "ydl.to_screen" instead')
self._write_string(
'%s%s' % (self._bidi_workaround(message), ('' if skip_eol else '\n')),
self._out_files['print'])

def to_screen(self, message, skip_eol=False, quiet=None):
"""Print message to screen if not in quiet mode"""
if self.params.get('logger'):
self.params['logger'].debug(message)
elif not quiet or self.params.get('verbose'):
self._write_string(
'%s%s' % (self._bidi_workaround(message), ('' if skip_eol else '\n')),
self._err_file if quiet else self._screen_file)
return
if (self.params.get('quiet') if quiet is None else quiet) and not self.params.get('verbose'):
return
self._write_string(
'%s%s' % (self._bidi_workaround(message), ('' if skip_eol else '\n')),
self._out_files['screen'])

def to_stderr(self, message, only_once=False):
"""Print message to stderr"""
assert isinstance(message, compat_str)
if self.params.get('logger'):
self.params['logger'].error(message)
else:
self._write_string('%s\n' % self._bidi_workaround(message), self._err_file, only_once=only_once)
self._write_string('%s\n' % self._bidi_workaround(message), self._out_files['error'], only_once=only_once)

def _send_console_code(self, code):
if compat_os_name == 'nt' or not self._out_files['console']:
return
self._write_string(code, self._out_files['console'])

def to_console_title(self, message):
if not self.params.get('consoletitle', False):
Expand All @@ -867,26 +887,18 @@ def to_console_title(self, message):
# c_wchar_p() might not be necessary if `message` is
# already of type unicode()
ctypes.windll.kernel32.SetConsoleTitleW(ctypes.c_wchar_p(message))
elif 'TERM' in os.environ:
self._write_string('\033]0;%s\007' % message, self._screen_file)
else:
self._send_console_code(f'\033]0;{message}\007')

def save_console_title(self):
if not self.params.get('consoletitle', False):
if not self.params.get('consoletitle') or self.params.get('simulate'):
return
if self.params.get('simulate'):
return
if compat_os_name != 'nt' and 'TERM' in os.environ:
# Save the title on stack
self._write_string('\033[22;0t', self._screen_file)
self._send_console_code('\033[22;0t') # Save the title on stack

def restore_console_title(self):
if not self.params.get('consoletitle', False):
if not self.params.get('consoletitle') or self.params.get('simulate'):
return
if self.params.get('simulate'):
return
if compat_os_name != 'nt' and 'TERM' in os.environ:
# Restore the title from stack
self._write_string('\033[23;0t', self._screen_file)
self._send_console_code('\033[23;0t') # Restore the title from stack

def __enter__(self):
self.save_console_title()
Expand Down Expand Up @@ -935,11 +947,6 @@ def trouble(self, message=None, tb=None, is_error=True):
raise DownloadError(message, exc_info)
self._download_retcode = 1

def to_screen(self, message, skip_eol=False):
"""Print message to stdout if not in quiet mode"""
self.to_stdout(
message, skip_eol, quiet=self.params.get('quiet', False))

class Styles(Enum):
HEADERS = 'yellow'
EMPHASIS = 'light blue'
Expand All @@ -963,11 +970,11 @@ def _format_text(self, handle, allow_colors, text, f, fallback=None, *, test_enc

def _format_screen(self, *args, **kwargs):
return self._format_text(
self._screen_file, self._allow_colors['screen'], *args, **kwargs)
self._out_files['screen'], self._allow_colors['screen'], *args, **kwargs)

def _format_err(self, *args, **kwargs):
return self._format_text(
self._err_file, self._allow_colors['err'], *args, **kwargs)
self._out_files['error'], self._allow_colors['error'], *args, **kwargs)

def report_warning(self, message, only_once=False):
'''
Expand Down Expand Up @@ -3861,7 +3868,7 @@ def get_encoding(stream):
encoding_str = 'Encodings: locale %s, fs %s, out %s, err %s, pref %s' % (
locale.getpreferredencoding(),
sys.getfilesystemencoding(),
get_encoding(self._screen_file), get_encoding(self._err_file),
get_encoding(self._out_files['screen']), get_encoding(self._out_files['error']),
self.get_encoding())

logger = self.params.get('logger')
Expand Down
7 changes: 4 additions & 3 deletions yt_dlp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ def metadataparser_actions(f):
raise ValueError('unsupported geo-bypass country or ip-block')

opts.match_filter = match_filter_func(opts.match_filter)
opts.date = DateRange.day(opts.date) if opts.date else DateRange(opts.dateafter, opts.datebefore)

if opts.download_archive is not None:
opts.download_archive = expand_path(opts.download_archive)
Expand Down Expand Up @@ -415,8 +414,8 @@ def report_conflict(arg1, opt1, arg2='--allow-unplayable-formats', opt2='allow_u
setattr(opts, opt1, default)

# Conflicting options
report_conflict('--date-after', 'dateafter', '--date', 'date', default=None)
report_conflict('--date-before', 'datebefore', '--date', 'date', default=None)
report_conflict('--dateafter', 'dateafter', '--date', 'date', default=None)
report_conflict('--datebefore', 'datebefore', '--date', 'date', default=None)
report_conflict('--exec-before-download', 'exec_before_dl_cmd', '"--exec before_dl:"', 'exec_cmd', opts.exec_cmd.get('before_dl'))
report_conflict('--id', 'useid', '--output', 'outtmpl', val2=opts.outtmpl.get('default'))
report_conflict('--remux-video', 'remuxvideo', '--recode-video', 'recodevideo')
Expand Down Expand Up @@ -455,6 +454,8 @@ def report_deprecation(val, old, new=None):
# report_deprecation(opts.writeannotations, '--write-annotations') # It's just that no website has it

# Dependent options
opts.date = DateRange.day(opts.date) if opts.date else DateRange(opts.dateafter, opts.datebefore)

if opts.exec_before_dl_cmd:
opts.exec_cmd['before_dl'] = opts.exec_before_dl_cmd

Expand Down
2 changes: 1 addition & 1 deletion yt_dlp/downloader/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(self, ydl, params):
self._enable_progress()

def to_screen(self, *args, **kargs):
self.ydl.to_stdout(*args, quiet=self.params.get('quiet'), **kargs)
self.ydl.to_screen(*args, quiet=self.params.get('quiet'), **kargs)

def to_stderr(self, message):
self.ydl.to_stderr(message)
Expand Down
4 changes: 2 additions & 2 deletions yt_dlp/postprocessor/_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ def _prepare_multiline_status(self, lines=1):
elif self._ydl.params.get('logger'):
self._multiline = MultilineLogger(self._ydl.params['logger'], lines)
elif self._params.get('progress_with_newline'):
self._multiline = BreaklineStatusPrinter(self._ydl._screen_file, lines)
self._multiline = BreaklineStatusPrinter(self._ydl._out_files['screen'], lines)
else:
self._multiline = MultilinePrinter(self._ydl._screen_file, lines, not self._params.get('quiet'))
self._multiline = MultilinePrinter(self._ydl._out_files['screen'], lines, not self._params.get('quiet'))
self._multiline.allow_colors = self._multiline._HAVE_FULLCAP and not self._params.get('no_color')

def _finish_multiline_status(self):
Expand Down
8 changes: 6 additions & 2 deletions yt_dlp/postprocessor/metadataparser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import re

from enum import Enum

from .common import PostProcessor
Expand All @@ -26,12 +25,17 @@ def validate_action(cls, action, *data):
'''
if not isinstance(action, cls.Actions):
raise ValueError(f'{action!r} is not a valid action')
getattr(cls, action.value)(cls, *data)
getattr(cls, action.value)(cls, *data) # So this can raise error to validate

@staticmethod
def field_to_template(tmpl):
if re.match(r'[a-zA-Z_]+$', tmpl):
return f'%({tmpl})s'

from ..YoutubeDL import YoutubeDL
err = YoutubeDL.validate_outtmpl(tmpl)
if err:
raise err
return tmpl

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion yt_dlp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5540,4 +5540,4 @@ def get_first_group(match, *groups, default=None):

def merge_headers(*dicts):
"""Merge dicts of http headers case insensitively, prioritizing the latter ones"""
return {k.capitalize(): v for k, v in itertools.chain.from_iterable(map(dict.items, dicts))}
return {k.title(): v for k, v in itertools.chain.from_iterable(map(dict.items, dicts))}

0 comments on commit 8559f46

Please sign in to comment.