Skip to content

Commit 117598c

Browse files
committed
Implement PathProxy as a way to defer download of file data
1 parent 22955f0 commit 117598c

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

replicate/use.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _process_iterator_item(item: Any, openapi_schema: dict) -> Any:
113113
# If items are file URLs, download them
114114
if items_schema.get("type") == "string" and items_schema.get("format") == "uri":
115115
if isinstance(item, str) and item.startswith(("http://", "https://")):
116-
return _download_file(item)
116+
return PathProxy(item)
117117

118118
return item
119119

@@ -208,6 +208,37 @@ def __str__(self) -> str:
208208
return str(self.iterator_factory())
209209

210210

211+
class PathProxy(Path):
212+
def __init__(self, target: str) -> None:
213+
path: Path | None = None
214+
215+
def ensure_path() -> Path:
216+
nonlocal path
217+
if path is None:
218+
path = _download_file(target)
219+
return path
220+
221+
object.__setattr__(self, "__target__", target)
222+
object.__setattr__(self, "__path__", ensure_path)
223+
224+
def __getattribute__(self, name) -> Any:
225+
if name in ("__path__", "__target__"):
226+
return object.__getattribute__(self, name)
227+
228+
return getattr(object.__getattribute__(self, "__path__")(), name)
229+
230+
def __setattr__(self, name, value) -> None:
231+
if name in ("__path__", "__target__"):
232+
raise ValueError()
233+
234+
object.__setattr__(object.__getattribute__(self, "__path__")(), name, value)
235+
236+
def __delattr__(self, name) -> None:
237+
if name in ("__path__", "__target__"):
238+
raise ValueError()
239+
delattr(object.__getattribute__(self, "__path__")(), name)
240+
241+
211242
@dataclass
212243
class Run:
213244
"""

0 commit comments

Comments
 (0)