initial support for conditional filenames (#1394)
This commit is contained in:
@@ -97,6 +97,28 @@ Description
|
|||||||
a valid filename extension.
|
a valid filename extension.
|
||||||
|
|
||||||
|
|
||||||
|
extractor.*.filename-conditions
|
||||||
|
-------------------------------
|
||||||
|
Type
|
||||||
|
``object``
|
||||||
|
Example
|
||||||
|
.. code:: json
|
||||||
|
|
||||||
|
{
|
||||||
|
"extension == 'mp4'" : "{id}_video.{extension}",
|
||||||
|
"extension in ('zip','rar')": "{id}_archive.{extension}",
|
||||||
|
"'nature' in title" : "{id}_{title}.{extension}"
|
||||||
|
}
|
||||||
|
Description
|
||||||
|
An object containing Python expressions mapping to the
|
||||||
|
filename format strings to use.
|
||||||
|
|
||||||
|
When none of the given conditions match, `extractor.*.filename`_ is used.
|
||||||
|
|
||||||
|
Expressions are evaluated in the order as specified in Python 3.6+
|
||||||
|
and in an undetermined order in Python 3.4 and 3.5.
|
||||||
|
|
||||||
|
|
||||||
extractor.*.directory
|
extractor.*.directory
|
||||||
---------------------
|
---------------------
|
||||||
Type
|
Type
|
||||||
|
|||||||
@@ -756,6 +756,7 @@ class PathFormat():
|
|||||||
|
|
||||||
def __init__(self, extractor):
|
def __init__(self, extractor):
|
||||||
filename_fmt = extractor.config("filename")
|
filename_fmt = extractor.config("filename")
|
||||||
|
filename_conditions = extractor.config("filename-conditions")
|
||||||
if filename_fmt is None:
|
if filename_fmt is None:
|
||||||
filename_fmt = extractor.filename_fmt
|
filename_fmt = extractor.filename_fmt
|
||||||
|
|
||||||
@@ -770,6 +771,14 @@ class PathFormat():
|
|||||||
|
|
||||||
kwdefault = extractor.config("keywords-default")
|
kwdefault = extractor.config("keywords-default")
|
||||||
try:
|
try:
|
||||||
|
if filename_conditions:
|
||||||
|
self.build_filename = self.build_filename_conditional
|
||||||
|
self.filename_conditions = [
|
||||||
|
(compile_expression(expr),
|
||||||
|
Formatter(fmt, kwdefault).format_map)
|
||||||
|
for expr, fmt in filename_conditions.items()
|
||||||
|
]
|
||||||
|
|
||||||
self.filename_formatter = Formatter(
|
self.filename_formatter = Formatter(
|
||||||
filename_fmt, kwdefault).format_map
|
filename_fmt, kwdefault).format_map
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
@@ -933,6 +942,19 @@ class PathFormat():
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
raise exception.FilenameFormatError(exc)
|
raise exception.FilenameFormatError(exc)
|
||||||
|
|
||||||
|
def build_filename_conditional(self):
|
||||||
|
kwdict = self.kwdict
|
||||||
|
|
||||||
|
try:
|
||||||
|
for condition, formatter in self.filename_conditions:
|
||||||
|
if condition(kwdict):
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
formatter = self.filename_formatter
|
||||||
|
return self.clean_path(self.clean_segment(formatter(kwdict)))
|
||||||
|
except Exception as exc:
|
||||||
|
raise exception.FilenameFormatError(exc)
|
||||||
|
|
||||||
def build_path(self):
|
def build_path(self):
|
||||||
"""Combine directory and filename to full paths"""
|
"""Combine directory and filename to full paths"""
|
||||||
if self._create_directory:
|
if self._create_directory:
|
||||||
|
|||||||
@@ -6,4 +6,4 @@
|
|||||||
# it under the terms of the GNU General Public License version 2 as
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
# published by the Free Software Foundation.
|
# published by the Free Software Foundation.
|
||||||
|
|
||||||
__version__ = "1.17.6-dev"
|
__version__ = "1.18.0-dev"
|
||||||
|
|||||||
Reference in New Issue
Block a user