Allow specifying a directory as the output template

This commit is contained in:
Mitchell Mebane 2015-04-21 17:31:30 -05:00
parent b4c0806963
commit 22c00da06b
3 changed files with 21 additions and 0 deletions

View File

@ -16,10 +16,12 @@ import json
import xml.etree.ElementTree
from youtube_dl.utils import (
add_defaults_if_directory,
age_restricted,
args_to_str,
clean_html,
DateRange,
DEFAULT_OUTTMPL,
detect_exe_version,
encodeFilename,
escape_rfc3986,
@ -581,6 +583,16 @@ ffmpeg version 2.4.4 Copyright (c) 2000-2014 the FFmpeg ...'''), '2.4.4')
'like_count > 100 & dislike_count <? 50 & description',
{'like_count': 190, 'dislike_count': 10}))
def test_add_defaults_if_directory(self):
cwd = os.getcwd()
existant_dir = os.path.join(cwd, '') # ensure trailing slash
self.assertTrue(os.path.isdir(existant_dir))
self.assertEqual(add_defaults_if_directory(cwd), os.path.join(existant_dir, DEFAULT_OUTTMPL))
nonexistant_dir = os.path.join(cwd, 'add_defaults_if_directory_test')
self.assertFalse(os.path.isdir(nonexistant_dir))
self.assertEqual(add_defaults_if_directory(nonexistant_dir), nonexistant_dir)
if __name__ == '__main__':
unittest.main()

View File

@ -38,6 +38,7 @@ from .compat import (
compat_urllib_request,
)
from .utils import (
add_defaults_if_directory,
escape_url,
ContentTooShortError,
date_from_str,
@ -567,6 +568,7 @@ class YoutubeDL(object):
outtmpl = sanitize_path(self.params.get('outtmpl', DEFAULT_OUTTMPL))
tmpl = compat_expanduser(outtmpl)
tmpl = add_defaults_if_directory(tmpl)
filename = tmpl % template_dict
# Temporary fix for #4787
# 'Treat' all problem characters by passing filename through preferredencoding

View File

@ -325,6 +325,13 @@ def sanitize_path(s):
sanitized_path.insert(0, drive_or_unc + os.path.sep)
return os.path.join(*sanitized_path)
def add_defaults_if_directory(path):
"""Appends the default output template, if the given path is an existing directory. Non-directory inputs are returned unchanged."""
if os.path.isdir(path):
dir = os.path.join(path, '') # ensure trailing slash
return dir + DEFAULT_OUTTMPL
else:
return path
def sanitize_url_path_consecutive_slashes(url):
"""Collapses consecutive slashes in URLs' path"""