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