Skip to content

Commit

Permalink
more filepath opportunities
Browse files Browse the repository at this point in the history
TODO: make documentation
  • Loading branch information
zeptofine committed Nov 9, 2023
1 parent 782f0a3 commit 1ac3a5e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 56 deletions.
6 changes: 5 additions & 1 deletion imdataset_creator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ def main(
p.log("Finished. No images remain")
return 0
if simulate:
p.log("10 random scenarios: ", sample(scenarios, 10), f"Simulated. {len(scenarios)} images remain")
p.log(
"10 random scenarios: ",
sample(scenarios, min(len(scenarios), 10)),
f"Simulated. {len(scenarios)} images remain",
)
return 0

try:
Expand Down
2 changes: 0 additions & 2 deletions imdataset_creator/datarules/data_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import os
from datetime import datetime
from pathlib import Path
from types import MappingProxyType
from typing import Self

Expand All @@ -17,7 +16,6 @@
Producer,
ProducerSchema,
Rule,
combine_expr_conds,
)

STAT_TRACKED = ("st_size", "st_atime", "st_mtime", "st_ctime")
Expand Down
71 changes: 49 additions & 22 deletions imdataset_creator/file.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import re
from collections.abc import Mapping, Sequence
from collections.abc import Callable, Mapping, Sequence
from dataclasses import dataclass
from pathlib import Path
from string import Formatter
from typing import Any
from typing import Any, ClassVar

from typing_extensions import SupportsIndex

Expand All @@ -14,12 +16,12 @@ def __init__(self, disallowed: str):


str_slice = re.compile(r"\[(?P<start>[-\d]*):(?P<stop>[-\d]*):?(?P<step>[-\d]*)\]")
str_cond = re.compile(r"^(?P<prompt>[^\?:]+)\?(?P<true>(?:[^:])*):?(?P<false>.*)$") # present?yes:no


class SafeFormatter(Formatter):
def get_field(self, field_name: str, _: Sequence[Any], kwargs: Mapping[str, Any]) -> tuple[Any, Any]:
# the goal is to make sure `property`s and indexing is still available, while dunders and things are not

if "__" in field_name:
raise InvalidFormatError("__")

Expand All @@ -37,30 +39,55 @@ def get_field(self, field_name: str, _: Sequence[Any], kwargs: Mapping[str, Any]
return super().get_field(field_name, _, kwargs)


escaped_split = re.compile(r"[^\\],")
condition_fmt = re.compile(r"^(?P<prompt>[^\?:]+)\?(?P<true>(?:[^:])*):?(?P<false>.*)$") # present?yes:no
replacement_fmt = re.compile(r"'(?P<from>[^']+)'='(?P<to>[^']*)'")


def condition_format(pth: str, match: re.Match) -> str:
"""
Inline if condition. (if?then:else)
"""
p = match.group("prompt")

if (p := match.group("prompt")) in pth:
return match.group("true") or p
return match.group("false")


class MalleablePath(str):
mpath_format_conditions: ClassVar[dict[re.Pattern, Callable[[str, re.Match], str]]] = {
re.compile(r"^(?P<prompt>[^\?:]+)\?(?P<true>(?:[^:!])*):?(?P<false>.*)$"): (condition_format),
re.compile(r"^'(?P<from>[^']+)'='(?P<to>[^']*)'$"): ( # replaces <from> to <to>
lambda pth, m: pth.replace(m.group("from"), m.group("to"))
),
}

def __format__(self, format_spec: str):
formats = format_spec.split(",")
newfmt: MalleablePath = self
if not format_spec:
return self

formats = [s.replace(r"\,", ",") for s in escaped_split.split(format_spec)]
newpth: str = str(self)
for fmt in formats:
# if "=" in fmt:
# key, val = fmt.split("=")
# if key == "maxlen":
# newfmt = MalleablePath(newfmt[: int(val)])
# else:
# raise ValueError(f"Unknown format specifier: {key}")
matches = list(str_cond.finditer(fmt))
if matches:
match = matches[0]
if (p := match.group("prompt")) in newfmt:
newfmt = MalleablePath(t if (t := match.group("true")) else p)
else:
newfmt = MalleablePath(match.group("false"))
if not fmt:
continue

patterns_used = False
for pattern, func in self.mpath_format_conditions.items():
if any(match := list(pattern.finditer(fmt))):
patterns_used = True
newpth = func(newpth, match[0])

if fmt == "underscores":
newfmt = MalleablePath("_".join(newfmt.split(" ")))
elif fmt == "underscore_path":
newfmt = MalleablePath("_".join(Path(newfmt).parts))
return str(newfmt)
newpth = "_".join(newpth.split(" "))
elif fmt == "underscore_parts":
newpth = "_".join(Path(newpth).parts)

elif not patterns_used:
raise ValueError(f"Unknown format specifier: {fmt!r}")

return str(newpth)

def __getitem__(self, __key: SupportsIndex | slice) -> str:
return MalleablePath(super().__getitem__(__key))
Expand Down
6 changes: 1 addition & 5 deletions imdataset_creator/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import os
from datetime import datetime
from pathlib import Path
from pprint import pprint

from polars import DataFrame, concat
from PySide6.QtCore import Qt, QThread, Signal, Slot
Expand All @@ -15,15 +14,12 @@
QGridLayout,
QMainWindow,
QMenu,
QMenuBar,
QProgressBar,
QPushButton,
QSplitter,
QWidget,
)
from rich import print as rprint

from .. import DatasetBuilder, File
from .. import DatasetBuilder
from ..configs import MainConfig
from ..datarules import chunk_split
from .err_dialog import catch_errors
Expand Down
26 changes: 0 additions & 26 deletions imdataset_creator/gui/settings_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,29 +623,3 @@ def mouseMoveEvent(self, event: QMouseEvent) -> None:
def mousePressEvent(self, event: QMouseEvent) -> None:
self.previous_position = event.position()
return super().mousePressEvent(event)


# class ItemSettings(dict[str | tuple[str, ...], BaseSettingsInput]):
# def from_cfg(self, cfg: dict):
# for key, setting in self.items():
# if isinstance(key, tuple):
# vals = [cfg[k] for k in key] # sort based on key order
# setting.from_cfg(vals)
# else:
# if key in cfg:
# setting.from_cfg(cfg[key])

# def get_cfg(self) -> dict:
# dct = {}
# for key, setting in self.items():
# val = setting.get_cfg()

# if isinstance(key, tuple):
# for k, v in zip(key, val):
# dct[k] = v
# else:
# dct[key] = val
# return dct

# def get_widgets(self) -> list[list[QWidget]]:
# return [setting.create_widgets() for setting in self.values()]

0 comments on commit 1ac3a5e

Please sign in to comment.