This commit is contained in:
Joseph Lansdowne 2020-10-06 07:48:30 +02:00 committed by GitHub
commit 82f5dddcb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 3 deletions

View File

@ -0,0 +1,59 @@
import unittest
from test.helper import FakeYDL
from youtube_dl import FileDownloader
class BaseTestFileDownloader(unittest.TestCase):
params = {}
def setUp(self):
self.downloader = FileDownloader(FakeYDL(), self.params)
class TestPartFileDefaults(BaseTestFileDownloader):
params = {}
def test_temp_name_missing_file(self):
# file is missing: should add the default suffix
fn = self.downloader.temp_name('some file.ext')
self.assertEqual(fn, 'some file.ext.part')
def test_undo_temp_name_no_suffix(self):
# file doesn't end with the suffix: should be untouched
fn = self.downloader.undo_temp_name('some file.ext')
self.assertEqual(fn, 'some file.ext')
def test_undo_temp_name_with_suffix(self):
# file ends with the suffix: should be removed
fn = self.downloader.undo_temp_name('some file.ext.part')
self.assertEqual(fn, 'some file.ext')
class TestPartFileCustomSuffix(BaseTestFileDownloader):
params = {'partsuffix': '.othersuffix'}
def test_temp_name_missing_file(self):
# file is missing: should add the custom suffix
fn = self.downloader.temp_name('some file.ext')
self.assertEqual(fn, 'some file.ext.othersuffix')
def test_undo_temp_name_no_suffix(self):
# file doesn't end with the suffix: should be untouched
fn = self.downloader.undo_temp_name('some file.ext')
self.assertEqual(fn, 'some file.ext')
def test_undo_temp_name_default_suffix(self):
# file ends with the default suffix: should be untouched
fn = self.downloader.undo_temp_name('some file.ext.part')
self.assertEqual(fn, 'some file.ext.part')
def test_undo_temp_name_custom_suffix(self):
# file ends with the custom suffix: should be removed
fn = self.downloader.undo_temp_name('some file.ext.othersuffix')
self.assertEqual(fn, 'some file.ext')
if __name__ == '__main__':
unittest.main()

View File

@ -365,6 +365,7 @@ def _real_main(argv=None):
'logtostderr': opts.outtmpl == '-',
'consoletitle': opts.consoletitle,
'nopart': opts.nopart,
'partsuffix': opts.partsuffix,
'updatetime': opts.updatetime,
'writedescription': opts.writedescription,
'writeannotations': opts.writeannotations,

View File

@ -16,6 +16,9 @@ from ..utils import (
timeconvert,
)
# default value for 'partsuffix' option
DEFAULT_PART_SUFFIX = '.part'
class FileDownloader(object):
"""File Downloader class.
@ -40,6 +43,7 @@ class FileDownloader(object):
logtostderr: Log messages to stderr instead of stdout.
consoletitle: Display progress in console window's titlebar.
nopart: Do not use temporary .part files.
partsuffix: Suffix for .part files.
updatetime: Use the Last-modified header to set output file timestamps.
test: Download only first bytes to test the downloader.
min_filesize: Skip files smaller than this size
@ -185,11 +189,12 @@ class FileDownloader(object):
if self.params.get('nopart', False) or filename == '-' or \
(os.path.exists(encodeFilename(filename)) and not os.path.isfile(encodeFilename(filename))):
return filename
return filename + '.part'
return filename + self.params.get('partsuffix', DEFAULT_PART_SUFFIX)
def undo_temp_name(self, filename):
if filename.endswith('.part'):
return filename[:-len('.part')]
suffix = self.params.get('partsuffix', DEFAULT_PART_SUFFIX)
if filename.endswith(suffix):
return filename[:-len(suffix)]
return filename
def ytdl_filename(self, filename):

View File

@ -729,6 +729,10 @@ def parseOpts(overrideArguments=None):
'--no-part',
action='store_true', dest='nopart', default=False,
help='Do not use .part files - write directly into output file')
filesystem.add_option(
'--part-suffix',
action='store', dest='partsuffix', default='.part', metavar='SUFFIX',
help='Suffix to use for .part files (default is "%default")')
filesystem.add_option(
'--no-mtime',
action='store_false', dest='updatetime', default=True,