Support counters in image folder names
This commit is contained in:
@@ -37,17 +37,18 @@ def _next_counter_for_relative_path(base_directory, relative_template):
|
||||
relative_template = str(relative_template).replace("\\", "/")
|
||||
if "%counter%" not in relative_template:
|
||||
return 1
|
||||
template = relative_template.replace("%counter%", "*")
|
||||
parent = Path(base_directory) / Path(template).parent
|
||||
stem = Path(template).name
|
||||
if not parent.exists():
|
||||
base = Path(base_directory)
|
||||
if not base.exists():
|
||||
return 1
|
||||
pattern = re.compile(re.escape(stem).replace(r"\*", r"(\d+)") + r"(?:\.[^.]+)?$")
|
||||
pattern = re.compile(
|
||||
re.escape(relative_template).replace(re.escape("%counter%"), r"(\d+)")
|
||||
)
|
||||
highest = 0
|
||||
for path in parent.iterdir():
|
||||
for path in base.rglob("*"):
|
||||
if not path.is_file():
|
||||
continue
|
||||
match = pattern.match(path.name)
|
||||
relative_path = path.relative_to(base).as_posix()
|
||||
match = pattern.fullmatch(relative_path)
|
||||
if match:
|
||||
highest = max(highest, int(match.group(1)))
|
||||
return highest + 1
|
||||
|
||||
@@ -94,6 +94,20 @@ class DumasImageNodeTests(unittest.TestCase):
|
||||
5,
|
||||
)
|
||||
|
||||
def test_counter_can_be_in_a_folder_name(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
os.makedirs(os.path.join(directory, "renders001"))
|
||||
os.makedirs(os.path.join(directory, "renders004"))
|
||||
open(os.path.join(directory, "renders001", "cover.png"), "wb").close()
|
||||
open(os.path.join(directory, "renders004", "cover.png"), "wb").close()
|
||||
|
||||
self.assertEqual(
|
||||
self.nodes._next_counter_for_relative_path(
|
||||
directory, "renders%counter%/cover.png"
|
||||
),
|
||||
5,
|
||||
)
|
||||
|
||||
def test_save_batch_assigns_distinct_counter_names(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
FakeImage.saved_paths.clear()
|
||||
|
||||
Reference in New Issue
Block a user