2019-08-12 21:46:30 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2019 Mike Fährmann
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
import zipfile
|
|
|
|
import tempfile
|
2019-08-14 21:57:42 +02:00
|
|
|
from datetime import datetime, timezone as tz
|
2019-08-12 21:46:30 +02:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
from unittest.mock import Mock, mock_open, patch
|
|
|
|
|
|
|
|
from gallery_dl import postprocessor, extractor, util, config
|
|
|
|
from gallery_dl.postprocessor.common import PostProcessor
|
|
|
|
|
|
|
|
|
|
|
|
class MockPostprocessorModule(Mock):
|
|
|
|
__postprocessor__ = "mock"
|
|
|
|
|
|
|
|
|
|
|
|
class TestPostprocessorModule(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
postprocessor._cache.clear()
|
|
|
|
|
|
|
|
def test_find(self):
|
|
|
|
for name in (postprocessor.modules):
|
|
|
|
cls = postprocessor.find(name)
|
|
|
|
self.assertEqual(cls.__name__, name.capitalize() + "PP")
|
|
|
|
self.assertIs(cls.__base__, PostProcessor)
|
|
|
|
|
|
|
|
self.assertEqual(postprocessor.find("foo"), None)
|
|
|
|
self.assertEqual(postprocessor.find(1234) , None)
|
|
|
|
self.assertEqual(postprocessor.find(None) , None)
|
|
|
|
|
|
|
|
@patch("importlib.import_module")
|
|
|
|
def test_cache(self, import_module):
|
|
|
|
import_module.return_value = MockPostprocessorModule()
|
|
|
|
|
|
|
|
for name in (postprocessor.modules):
|
|
|
|
postprocessor.find(name)
|
|
|
|
self.assertEqual(import_module.call_count, len(postprocessor.modules))
|
|
|
|
|
|
|
|
# no new calls to import_module
|
|
|
|
for name in (postprocessor.modules):
|
|
|
|
postprocessor.find(name)
|
|
|
|
self.assertEqual(import_module.call_count, len(postprocessor.modules))
|
|
|
|
|
|
|
|
|
|
|
|
class BasePostprocessorTest(unittest.TestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.extractor = extractor.find("test:")
|
|
|
|
cls.dir = tempfile.TemporaryDirectory()
|
2019-11-23 23:50:16 +01:00
|
|
|
config.set((), "base-directory", cls.dir.name)
|
2019-08-12 21:46:30 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
cls.dir.cleanup()
|
|
|
|
config.clear()
|
|
|
|
|
|
|
|
def _create(self, options=None, data=None):
|
|
|
|
kwdict = {"category": "test", "filename": "file", "extension": "ext"}
|
|
|
|
if options is None:
|
|
|
|
options = {}
|
|
|
|
if data is not None:
|
|
|
|
kwdict.update(data)
|
|
|
|
|
|
|
|
self.pathfmt = util.PathFormat(self.extractor)
|
|
|
|
self.pathfmt.set_directory(kwdict)
|
|
|
|
self.pathfmt.set_filename(kwdict)
|
|
|
|
|
|
|
|
pp = postprocessor.find(self.__class__.__name__[:-4].lower())
|
|
|
|
return pp(self.pathfmt, options)
|
|
|
|
|
|
|
|
|
|
|
|
class ClassifyTest(BasePostprocessorTest):
|
|
|
|
|
|
|
|
def test_classify_default(self):
|
|
|
|
pp = self._create()
|
|
|
|
|
|
|
|
self.assertEqual(pp.mapping, {
|
|
|
|
ext: directory
|
|
|
|
for directory, exts in pp.DEFAULT_MAPPING.items()
|
|
|
|
for ext in exts
|
|
|
|
})
|
|
|
|
self.pathfmt.set_extension("jpg")
|
|
|
|
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
path = os.path.join(self.dir.name, "test", "Pictures")
|
2019-08-21 23:54:08 +02:00
|
|
|
self.assertEqual(self.pathfmt.path, path + "/file.jpg")
|
2019-08-12 21:46:30 +02:00
|
|
|
self.assertEqual(self.pathfmt.realpath, path + "/file.jpg")
|
|
|
|
|
|
|
|
with patch("os.makedirs") as mkdirs:
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
mkdirs.assert_called_once_with(path, exist_ok=True)
|
|
|
|
|
|
|
|
def test_classify_noop(self):
|
|
|
|
pp = self._create()
|
|
|
|
rp = self.pathfmt.realpath
|
|
|
|
|
|
|
|
pp.prepare(self.pathfmt)
|
2019-08-21 23:54:08 +02:00
|
|
|
self.assertEqual(self.pathfmt.path, rp)
|
2019-08-12 21:46:30 +02:00
|
|
|
self.assertEqual(self.pathfmt.realpath, rp)
|
|
|
|
|
|
|
|
with patch("os.makedirs") as mkdirs:
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
self.assertEqual(mkdirs.call_count, 0)
|
|
|
|
|
|
|
|
def test_classify_custom(self):
|
|
|
|
pp = self._create({"mapping": {
|
|
|
|
"foo/bar": ["foo", "bar"],
|
|
|
|
}})
|
|
|
|
|
|
|
|
self.assertEqual(pp.mapping, {
|
|
|
|
"foo": "foo/bar",
|
|
|
|
"bar": "foo/bar",
|
|
|
|
})
|
|
|
|
self.pathfmt.set_extension("foo")
|
|
|
|
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
path = os.path.join(self.dir.name, "test", "foo", "bar")
|
2019-08-21 23:54:08 +02:00
|
|
|
self.assertEqual(self.pathfmt.path, path + "/file.foo")
|
2019-08-12 21:46:30 +02:00
|
|
|
self.assertEqual(self.pathfmt.realpath, path + "/file.foo")
|
|
|
|
|
|
|
|
with patch("os.makedirs") as mkdirs:
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
mkdirs.assert_called_once_with(path, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
class MetadataTest(BasePostprocessorTest):
|
|
|
|
|
|
|
|
def test_metadata_default(self):
|
|
|
|
pp = self._create()
|
|
|
|
|
|
|
|
# default arguments
|
|
|
|
self.assertEqual(pp.write , pp._write_json)
|
|
|
|
self.assertEqual(pp.ascii , False)
|
|
|
|
self.assertEqual(pp.indent , 4)
|
|
|
|
self.assertEqual(pp.extension, "json")
|
|
|
|
|
|
|
|
def test_metadata_json(self):
|
|
|
|
pp = self._create({
|
|
|
|
"mode" : "json",
|
|
|
|
"ascii" : True,
|
|
|
|
"indent" : 2,
|
|
|
|
"extension": "JSON",
|
2019-11-21 16:57:39 +01:00
|
|
|
}, {
|
|
|
|
"public" : "hello",
|
|
|
|
"_private" : "world",
|
2019-08-12 21:46:30 +02:00
|
|
|
})
|
|
|
|
|
2019-11-29 23:12:22 +01:00
|
|
|
self.assertEqual(pp.path , pp._path_append)
|
2019-08-12 21:46:30 +02:00
|
|
|
self.assertEqual(pp.write , pp._write_json)
|
|
|
|
self.assertEqual(pp.ascii , True)
|
|
|
|
self.assertEqual(pp.indent , 2)
|
|
|
|
self.assertEqual(pp.extension, "JSON")
|
|
|
|
|
|
|
|
with patch("builtins.open", mock_open()) as m:
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
|
|
|
|
path = self.pathfmt.realpath + ".JSON"
|
|
|
|
m.assert_called_once_with(path, "w", encoding="utf-8")
|
|
|
|
self.assertEqual(self._output(m), """{
|
|
|
|
"category": "test",
|
|
|
|
"extension": "ext",
|
2019-11-21 16:57:39 +01:00
|
|
|
"filename": "file",
|
|
|
|
"public": "hello"
|
2019-08-12 21:46:30 +02:00
|
|
|
}
|
|
|
|
""")
|
|
|
|
|
|
|
|
def test_metadata_tags(self):
|
2019-08-26 23:10:12 +02:00
|
|
|
pp = self._create(
|
|
|
|
{"mode": "tags"},
|
|
|
|
{"tags": ["foo", "bar", "baz"]},
|
|
|
|
)
|
2019-08-12 21:46:30 +02:00
|
|
|
self.assertEqual(pp.write, pp._write_tags)
|
|
|
|
self.assertEqual(pp.extension, "txt")
|
|
|
|
|
|
|
|
with patch("builtins.open", mock_open()) as m:
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
|
|
|
|
path = self.pathfmt.realpath + ".txt"
|
|
|
|
m.assert_called_once_with(path, "w", encoding="utf-8")
|
|
|
|
self.assertEqual(self._output(m), "foo\nbar\nbaz\n")
|
|
|
|
|
|
|
|
def test_metadata_tags_split_1(self):
|
2019-08-26 23:10:12 +02:00
|
|
|
pp = self._create(
|
|
|
|
{"mode": "tags"},
|
|
|
|
{"tags": "foo, bar, baz"},
|
|
|
|
)
|
2019-08-12 21:46:30 +02:00
|
|
|
with patch("builtins.open", mock_open()) as m:
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
self.assertEqual(self._output(m), "foo\nbar\nbaz\n")
|
|
|
|
|
|
|
|
def test_metadata_tags_split_2(self):
|
|
|
|
pp = self._create(
|
|
|
|
{"mode": "tags"},
|
|
|
|
{"tags": "foobar1 foobar2 foobarbaz"},
|
|
|
|
)
|
|
|
|
with patch("builtins.open", mock_open()) as m:
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
self.assertEqual(self._output(m), "foobar1\nfoobar2\nfoobarbaz\n")
|
|
|
|
|
|
|
|
def test_metadata_tags_tagstring(self):
|
2019-08-26 23:10:12 +02:00
|
|
|
pp = self._create(
|
|
|
|
{"mode": "tags"},
|
|
|
|
{"tag_string": "foo, bar, baz"},
|
|
|
|
)
|
2019-08-12 21:46:30 +02:00
|
|
|
with patch("builtins.open", mock_open()) as m:
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
self.assertEqual(self._output(m), "foo\nbar\nbaz\n")
|
|
|
|
|
|
|
|
def test_metadata_custom(self):
|
|
|
|
pp = self._create(
|
|
|
|
{"mode": "custom", "format": "{foo}\n{missing}\n"},
|
|
|
|
{"foo": "bar"},
|
|
|
|
)
|
|
|
|
self.assertEqual(pp.write, pp._write_custom)
|
|
|
|
self.assertEqual(pp.extension, "txt")
|
2019-11-29 23:12:22 +01:00
|
|
|
self.assertTrue(pp.contentfmt)
|
2019-08-12 21:46:30 +02:00
|
|
|
|
|
|
|
with patch("builtins.open", mock_open()) as m:
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
self.assertEqual(self._output(m), "bar\nNone\n")
|
|
|
|
|
2019-11-29 23:12:22 +01:00
|
|
|
def test_metadata_extfmt(self):
|
|
|
|
pp = self._create({
|
|
|
|
"extension" : "ignored",
|
|
|
|
"extension-format": "json",
|
|
|
|
})
|
|
|
|
|
|
|
|
self.assertEqual(pp.path, pp._path_format)
|
|
|
|
|
|
|
|
with patch("builtins.open", mock_open()) as m:
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
|
|
|
|
path = self.pathfmt.realdirectory + "file.json"
|
|
|
|
m.assert_called_once_with(path, "w", encoding="utf-8")
|
|
|
|
|
|
|
|
def test_metadata_extfmt_2(self):
|
|
|
|
pp = self._create({
|
|
|
|
"extension-format": "{extension!u}-data:{category:Res/ES/}",
|
|
|
|
})
|
|
|
|
|
|
|
|
self.pathfmt.prefix = "2."
|
|
|
|
with patch("builtins.open", mock_open()) as m:
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
|
|
|
|
path = self.pathfmt.realdirectory + "file.2.EXT-data:tESt"
|
|
|
|
m.assert_called_once_with(path, "w", encoding="utf-8")
|
|
|
|
|
2019-08-12 21:46:30 +02:00
|
|
|
@staticmethod
|
|
|
|
def _output(mock):
|
|
|
|
return "".join(
|
|
|
|
call[1][0]
|
|
|
|
for call in mock.mock_calls
|
|
|
|
if call[0] == "().write"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class MtimeTest(BasePostprocessorTest):
|
|
|
|
|
|
|
|
def test_mtime_default(self):
|
|
|
|
pp = self._create()
|
|
|
|
self.assertEqual(pp.key, "date")
|
|
|
|
|
|
|
|
def test_mtime_datetime(self):
|
2019-08-14 21:57:42 +02:00
|
|
|
pp = self._create(None, {"date": datetime(1980, 1, 1, tzinfo=tz.utc)})
|
2019-08-12 21:46:30 +02:00
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
2019-08-14 21:57:42 +02:00
|
|
|
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800)
|
2019-08-12 21:46:30 +02:00
|
|
|
|
|
|
|
def test_mtime_timestamp(self):
|
2019-08-14 21:57:42 +02:00
|
|
|
pp = self._create(None, {"date": 315532800})
|
2019-08-12 21:46:30 +02:00
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
2019-08-14 21:57:42 +02:00
|
|
|
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800)
|
2019-08-12 21:46:30 +02:00
|
|
|
|
|
|
|
def test_mtime_custom(self):
|
2019-08-14 21:57:42 +02:00
|
|
|
pp = self._create({"key": "foo"}, {"foo": 315532800})
|
2019-08-12 21:46:30 +02:00
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
2019-08-14 21:57:42 +02:00
|
|
|
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800)
|
2019-08-12 21:46:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ZipTest(BasePostprocessorTest):
|
|
|
|
|
|
|
|
def test_zip_default(self):
|
|
|
|
pp = self._create()
|
|
|
|
self.assertEqual(pp.path, self.pathfmt.realdirectory)
|
|
|
|
self.assertEqual(pp.run, pp._write)
|
|
|
|
self.assertEqual(pp.delete, True)
|
|
|
|
self.assertFalse(hasattr(pp, "args"))
|
|
|
|
self.assertEqual(pp.zfile.compression, zipfile.ZIP_STORED)
|
2019-08-26 23:10:12 +02:00
|
|
|
self.assertTrue(pp.zfile.filename.endswith("/test.zip"))
|
2019-08-12 21:46:30 +02:00
|
|
|
|
|
|
|
def test_zip_options(self):
|
|
|
|
pp = self._create({
|
|
|
|
"keep-files": True,
|
|
|
|
"compression": "zip",
|
|
|
|
"extension": "cbz",
|
|
|
|
})
|
|
|
|
self.assertEqual(pp.delete, False)
|
|
|
|
self.assertEqual(pp.zfile.compression, zipfile.ZIP_DEFLATED)
|
2019-08-26 23:10:12 +02:00
|
|
|
self.assertTrue(pp.zfile.filename.endswith("/test.cbz"))
|
2019-08-12 21:46:30 +02:00
|
|
|
|
|
|
|
def test_zip_safe(self):
|
|
|
|
pp = self._create({"mode": "safe"})
|
|
|
|
self.assertEqual(pp.delete, True)
|
|
|
|
self.assertEqual(pp.path, self.pathfmt.realdirectory)
|
|
|
|
self.assertEqual(pp.run, pp._write_safe)
|
|
|
|
self.assertEqual(pp.args, (
|
2019-08-26 23:10:12 +02:00
|
|
|
pp.path[:-1] + ".zip", "a", zipfile.ZIP_STORED, True,
|
2019-08-12 21:46:30 +02:00
|
|
|
))
|
2019-08-26 23:10:12 +02:00
|
|
|
self.assertTrue(pp.args[0].endswith("/test.zip"))
|
|
|
|
|
|
|
|
def test_zip_write(self):
|
|
|
|
pp = self._create()
|
|
|
|
nti = pp.zfile.NameToInfo
|
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile("w", dir=self.dir.name) as file:
|
|
|
|
file.write("foobar\n")
|
|
|
|
|
|
|
|
# write dummy file with 3 different names
|
|
|
|
for i in range(3):
|
|
|
|
name = "file{}.ext".format(i)
|
|
|
|
self.pathfmt.temppath = file.name
|
|
|
|
self.pathfmt.filename = name
|
|
|
|
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
|
|
|
|
self.assertEqual(len(nti), i+1)
|
|
|
|
self.assertIn(name, nti)
|
|
|
|
|
|
|
|
# check file contents
|
|
|
|
self.assertEqual(len(nti), 3)
|
|
|
|
self.assertIn("file0.ext", nti)
|
|
|
|
self.assertIn("file1.ext", nti)
|
|
|
|
self.assertIn("file2.ext", nti)
|
|
|
|
|
|
|
|
# write the last file a second time (will be skipped)
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
self.assertEqual(len(pp.zfile.NameToInfo), 3)
|
|
|
|
|
|
|
|
# close file
|
2019-11-03 21:45:45 +01:00
|
|
|
pp.run_final(self.pathfmt, 0)
|
2019-08-26 23:10:12 +02:00
|
|
|
|
|
|
|
# reopen to check persistence
|
|
|
|
with zipfile.ZipFile(pp.zfile.filename) as file:
|
|
|
|
nti = file.NameToInfo
|
|
|
|
self.assertEqual(len(pp.zfile.NameToInfo), 3)
|
|
|
|
self.assertIn("file0.ext", pp.zfile.NameToInfo)
|
|
|
|
self.assertIn("file1.ext", pp.zfile.NameToInfo)
|
|
|
|
self.assertIn("file2.ext", pp.zfile.NameToInfo)
|
|
|
|
|
|
|
|
os.unlink(pp.zfile.filename)
|
|
|
|
|
|
|
|
def test_zip_write_mock(self):
|
|
|
|
|
|
|
|
def side_effect(_, name):
|
|
|
|
pp.zfile.NameToInfo.add(name)
|
|
|
|
|
|
|
|
pp = self._create()
|
|
|
|
pp.zfile = Mock()
|
|
|
|
pp.zfile.NameToInfo = set()
|
|
|
|
pp.zfile.write.side_effect = side_effect
|
|
|
|
|
|
|
|
# write 3 files
|
|
|
|
for i in range(3):
|
|
|
|
self.pathfmt.temppath = self.pathfmt.realdirectory + "file.ext"
|
|
|
|
self.pathfmt.filename = "file{}.ext".format(i)
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
|
|
|
|
# write the last file a second time (will be skipped)
|
|
|
|
pp.prepare(self.pathfmt)
|
|
|
|
pp.run(self.pathfmt)
|
|
|
|
|
2019-11-03 21:45:45 +01:00
|
|
|
pp.run_final(self.pathfmt, 0)
|
2019-08-26 23:10:12 +02:00
|
|
|
|
|
|
|
self.assertEqual(pp.zfile.write.call_count, 3)
|
|
|
|
for call in pp.zfile.write.call_args_list:
|
|
|
|
args, kwargs = call
|
|
|
|
self.assertEqual(len(args), 2)
|
|
|
|
self.assertEqual(len(kwargs), 0)
|
|
|
|
self.assertEqual(args[0], self.pathfmt.temppath)
|
|
|
|
self.assertRegex(args[1], r"file\d\.ext")
|
|
|
|
self.assertEqual(pp.zfile.close.call_count, 1)
|
2019-08-12 21:46:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|