Skip to content

Commit

Permalink
implement conditional directories (#1394)
Browse files Browse the repository at this point in the history
They work the same way as conditional filenames (84d2e64), e.g.

"directory": {
    "score >= 20": ["high score"],
    "score >= 5" : ["mid score"],
    ""           : ["{category}", "default"]
}
  • Loading branch information
mikf committed Jun 20, 2021
1 parent ee1064a commit fd00d47
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

## Unreleased

## 1.18.0 - 2021-06-19
### Additions
- [foolfuuka] support `archive.wakarimasen.moe` ([#1595](https://github.com/mikf/gallery-dl/issues/1595))
Expand Down
22 changes: 18 additions & 4 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extractor.*.filename
Type
``string`` or ``object``
Example
* .. code::
* .. code:: json

"{manga}_c{chapter}_{page:>03}.{extension}"

Expand Down Expand Up @@ -115,11 +115,25 @@ Description
extractor.*.directory
---------------------
Type
``list`` of ``strings``
``list`` of ``strings`` or ``object``
Example
``["{category}", "{manga}", "c{chapter} - {title}"]``
* .. code:: json

["{category}", "{manga}", "c{chapter} - {title}"]

* .. code:: json

{
"'nature' in content": ["Nature Pictures"],
"retweet_id != 0" : ["{category}", "{user[name]}", "Retweets"],
"" : ["{category}", "{user[name]}"]
}

Description
A list of `format strings`_ for the resulting target directory.
A list of `format strings`_ to build target directory paths with.

If this is an ``object``, it must contain Python expressions mapping to the
list of format strings to use.

Each individual string in such a list represents a single path
segment, which will be joined together and appended to the
Expand Down
77 changes: 55 additions & 22 deletions gallery_dl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,20 @@ def __init__(self, extractor):
raise exception.FilenameFormatError(exc)

directory_fmt = config("directory")
if directory_fmt is None:
directory_fmt = extractor.directory_fmt
try:
if directory_fmt is None:
directory_fmt = extractor.directory_fmt
elif isinstance(directory_fmt, dict):
self.directory_conditions = [
(compile_expression(expr), [
Formatter(fmt, kwdefault).format_map
for fmt in fmts
])
for expr, fmts in directory_fmt.items() if expr
]
self.build_directory = self.build_directory_conditional
directory_fmt = directory_fmt.get("", extractor.directory_fmt)

self.directory_formatters = [
Formatter(dirfmt, kwdefault).format_map
for dirfmt in directory_fmt
Expand Down Expand Up @@ -870,29 +881,14 @@ def _enum_file(self):
def set_directory(self, kwdict):
"""Build directory path and create it if necessary"""
self.kwdict = kwdict

# Build path segments by applying 'kwdict' to directory format strings
segments = []
append = segments.append
try:
for formatter in self.directory_formatters:
segment = formatter(kwdict).strip()
if WINDOWS:
# remove trailing dots and spaces (#647)
segment = segment.rstrip(". ")
if segment:
append(self.clean_segment(segment))
except Exception as exc:
raise exception.DirectoryFormatError(exc)

# Join path segments
sep = os.sep
directory = self.clean_path(self.basedirectory + sep.join(segments))

# Ensure 'directory' ends with a path separator
segments = self.build_directory(kwdict)
if segments:
directory += sep
self.directory = directory
self.directory = directory = self.clean_path(
self.basedirectory + sep.join(segments) + sep)
else:
self.directory = directory = self.clean_path(self.basedirectory)

if WINDOWS:
# Enable longer-than-260-character paths on Windows
Expand Down Expand Up @@ -956,6 +952,43 @@ def build_filename_conditional(self):
except Exception as exc:
raise exception.FilenameFormatError(exc)

def build_directory(self, kwdict):
"""Apply 'kwdict' to directory format strings"""
segments = []
append = segments.append

try:
for formatter in self.directory_formatters:
segment = formatter(kwdict).strip()
if WINDOWS:
# remove trailing dots and spaces (#647)
segment = segment.rstrip(". ")
if segment:
append(self.clean_segment(segment))
return segments
except Exception as exc:
raise exception.DirectoryFormatError(exc)

def build_directory_conditional(self, kwdict):
segments = []
append = segments.append

try:
for condition, formatters in self.directory_conditions:
if condition(kwdict):
break
else:
formatters = self.directory_formatters
for formatter in formatters:
segment = formatter(kwdict).strip()
if WINDOWS:
segment = segment.rstrip(". ")
if segment:
append(self.clean_segment(segment))
return segments
except Exception as exc:
raise exception.DirectoryFormatError(exc)

def build_path(self):
"""Combine directory and filename to full paths"""
if self._create_directory:
Expand Down
2 changes: 1 addition & 1 deletion gallery_dl/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

__version__ = "1.18.0"
__version__ = "1.18.1-dev"

0 comments on commit fd00d47

Please sign in to comment.