[path] implement conditional 'base-directory'
This commit is contained in:
@@ -188,12 +188,29 @@ Description
|
||||
extractor.*.base-directory
|
||||
--------------------------
|
||||
Type
|
||||
|Path|_
|
||||
* |Path|_
|
||||
* ``object`` (Condition_ → |Path|_)
|
||||
Default
|
||||
``"./gallery-dl/"``
|
||||
Example
|
||||
.. code:: json
|
||||
|
||||
"~/Downloads/gallery-dl"
|
||||
|
||||
.. code:: json
|
||||
|
||||
{
|
||||
"score >= 100": "$DL",
|
||||
"duration" : "$DL/video",
|
||||
"" : "/tmp/files/"
|
||||
}
|
||||
Description
|
||||
Directory path used as base for all download destinations.
|
||||
|
||||
If this is an ``object``,
|
||||
it must contain Conditions_ mapping to the |Path|_ to use.
|
||||
Specifying a default |Path|_ with ``""`` is required.
|
||||
|
||||
|
||||
extractor.*.parent-directory
|
||||
----------------------------
|
||||
|
||||
@@ -118,22 +118,32 @@ class PathFormat():
|
||||
if WINDOWS:
|
||||
self.extended = config("path-extended", True)
|
||||
|
||||
self.basedirectory_conditions = None
|
||||
basedir = extractor._parentdir
|
||||
if not basedir:
|
||||
basedir = config("base-directory")
|
||||
sep = os.sep
|
||||
if basedir is None:
|
||||
basedir = f".{sep}gallery-dl{sep}"
|
||||
basedir = self.clean_path(f".{os.sep}gallery-dl{os.sep}")
|
||||
elif basedir:
|
||||
basedir = util.expand_path(basedir)
|
||||
altsep = os.altsep
|
||||
if altsep and altsep in basedir:
|
||||
basedir = basedir.replace(altsep, sep)
|
||||
if basedir[-1] != sep:
|
||||
basedir += sep
|
||||
basedir = self.clean_path(basedir)
|
||||
if isinstance(basedir, dict):
|
||||
self.basedirectory_conditions = conds = []
|
||||
for expr, bdir in basedir.items():
|
||||
if not expr:
|
||||
basedir = bdir
|
||||
continue
|
||||
conds.append((util.compile_filter(expr),
|
||||
self._prepare_basedirectory(bdir)))
|
||||
basedir = self._prepare_basedirectory(basedir)
|
||||
self.basedirectory = basedir
|
||||
|
||||
def _prepare_basedirectory(self, basedir):
|
||||
basedir = util.expand_path(basedir)
|
||||
if os.altsep and os.altsep in basedir:
|
||||
basedir = basedir.replace(os.altsep, os.sep)
|
||||
if basedir[-1] != os.sep:
|
||||
basedir += os.sep
|
||||
return self.clean_path(basedir)
|
||||
|
||||
def __str__(self):
|
||||
return self.realpath
|
||||
|
||||
@@ -175,11 +185,20 @@ class PathFormat():
|
||||
"""Build directory path and create it if necessary"""
|
||||
self.kwdict = kwdict
|
||||
|
||||
if segments := self.build_directory(kwdict):
|
||||
self.directory = directory = self.basedirectory + self.clean_path(
|
||||
os.sep.join(segments) + os.sep)
|
||||
if self.basedirectory_conditions is None:
|
||||
basedir = self.basedirectory
|
||||
else:
|
||||
self.directory = directory = self.basedirectory
|
||||
for condition, basedir in self.basedirectory_conditions:
|
||||
if condition(kwdict):
|
||||
break
|
||||
else:
|
||||
basedir = self.basedirectory
|
||||
|
||||
if segments := self.build_directory(kwdict):
|
||||
self.directory = directory = \
|
||||
f"{basedir}{self.clean_path(os.sep.join(segments))}{os.sep}"
|
||||
else:
|
||||
self.directory = directory = basedir
|
||||
|
||||
if WINDOWS and self.extended:
|
||||
directory = self._extended_path(directory)
|
||||
|
||||
Reference in New Issue
Block a user