14
14
15
15
from attrs import define
16
16
from attrs import field
17
+ from typing_extensions import deprecated
17
18
from upath import UPath
18
19
from upath ._stat import UPathStatResult
19
20
28
29
from _pytask .typing import no_default
29
30
30
31
if TYPE_CHECKING :
32
+ from io import BufferedReader
33
+ from io import BufferedWriter
34
+
31
35
from _pytask .mark import Mark
32
36
from _pytask .models import NodeInfo
33
37
from _pytask .tree_util import PyTree
40
44
"PythonNode" ,
41
45
"Task" ,
42
46
"TaskWithoutPath" ,
47
+ "get_state_of_path" ,
43
48
]
44
49
45
50
@@ -145,7 +150,7 @@ def signature(self) -> str:
145
150
146
151
def state (self ) -> str | None :
147
152
"""Return the state of the node."""
148
- return _get_state (self .path )
153
+ return get_state_of_path (self .path )
149
154
150
155
def execute (self , ** kwargs : Any ) -> Any :
151
156
"""Execute the task."""
@@ -188,7 +193,7 @@ def state(self) -> str | None:
188
193
The state is given by the modification timestamp.
189
194
190
195
"""
191
- return _get_state (self .path )
196
+ return get_state_of_path (self .path )
192
197
193
198
def load (self , is_product : bool = False ) -> Path : # noqa: ARG002
194
199
"""Load the value."""
@@ -316,6 +321,8 @@ class PickleNode(PPathNode):
316
321
path : Path
317
322
name : str = ""
318
323
attributes : dict [Any , Any ] = field (factory = dict )
324
+ serializer : Callable [[Any , BufferedWriter ], None ] = field (default = pickle .dump )
325
+ deserializer : Callable [[BufferedReader ], Any ] = field (default = pickle .load )
319
326
320
327
@property
321
328
def signature (self ) -> str :
@@ -332,17 +339,17 @@ def from_path(cls, path: Path) -> PickleNode:
332
339
return cls (name = path .as_posix (), path = path )
333
340
334
341
def state (self ) -> str | None :
335
- return _get_state (self .path )
342
+ return get_state_of_path (self .path )
336
343
337
344
def load (self , is_product : bool = False ) -> Any :
338
345
if is_product :
339
346
return self
340
347
with self .path .open ("rb" ) as f :
341
- return pickle . load (f ) # noqa: S301
348
+ return self . deserializer (f )
342
349
343
350
def save (self , value : Any ) -> None :
344
351
with self .path .open ("wb" ) as f :
345
- pickle . dump (value , f )
352
+ self . serializer (value , f )
346
353
347
354
348
355
@define (kw_only = True )
@@ -387,7 +394,7 @@ def collect(self) -> list[Path]:
387
394
return list (self .root_dir .glob (self .pattern )) # type: ignore[union-attr]
388
395
389
396
390
- def _get_state (path : Path ) -> str | None :
397
+ def get_state_of_path (path : Path ) -> str | None :
391
398
"""Get state of a path.
392
399
393
400
A simple function to handle local and remote files.
@@ -411,3 +418,13 @@ def _get_state(path: Path) -> str | None:
411
418
return stat .as_info ().get ("ETag" , "0" )
412
419
msg = "Unknown stat object."
413
420
raise NotImplementedError (msg )
421
+
422
+
423
+ @deprecated ("Use 'pytask.get_state_of_path' instead." )
424
+ def _get_state (path : Path ) -> str | None :
425
+ """Get state of a path.
426
+
427
+ A simple function to handle local and remote files.
428
+
429
+ """
430
+ return get_state_of_path (path )
0 commit comments