From 93cd47ef55c6a1bd951cb46f05931c34ced97341 Mon Sep 17 00:00:00 2001 From: Joseph Lansdowne Date: Sun, 31 May 2015 13:33:06 +0100 Subject: [PATCH] add --part-suffix option --- test/test_filedownloader.py | 59 +++++++++++++++++++++++++++++++++ youtube_dl/__init__.py | 1 + youtube_dl/downloader/common.py | 11 ++++-- youtube_dl/options.py | 4 +++ 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 test/test_filedownloader.py diff --git a/test/test_filedownloader.py b/test/test_filedownloader.py new file mode 100644 index 000000000..7970023b9 --- /dev/null +++ b/test/test_filedownloader.py @@ -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() diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index ace17857c..ac8cc5dea 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -308,6 +308,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, diff --git a/youtube_dl/downloader/common.py b/youtube_dl/downloader/common.py index 97e755d4b..ae0e6eb0e 100644 --- a/youtube_dl/downloader/common.py +++ b/youtube_dl/downloader/common.py @@ -13,6 +13,9 @@ from ..utils import ( timeconvert, ) +# default value for 'partsuffix' option +DEFAULT_PART_SUFFIX = '.part' + class FileDownloader(object): """File Downloader class. @@ -37,6 +40,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 @@ -173,11 +177,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 try_rename(self, old_filename, new_filename): diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 5a2315bd9..e8b9c7a22 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -622,6 +622,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,