add --part-suffix option

This commit is contained in:
Joseph Lansdowne 2015-05-31 13:33:06 +01:00
parent 96b9690985
commit 93cd47ef55
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

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

View File

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

View File

@ -622,6 +622,10 @@ def parseOpts(overrideArguments=None):
'--no-part', '--no-part',
action='store_true', dest='nopart', default=False, action='store_true', dest='nopart', default=False,
help='Do not use .part files - write directly into output file') 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( filesystem.add_option(
'--no-mtime', '--no-mtime',
action='store_false', dest='updatetime', default=True, action='store_false', dest='updatetime', default=True,