20
20
from _pytask .typing import NoDefault
21
21
from attrs import define
22
22
from attrs import field
23
+ from upath import UPathStatResult
23
24
24
25
25
26
if TYPE_CHECKING :
@@ -179,14 +180,7 @@ def state(self) -> str | None:
179
180
180
181
"""
181
182
if self .path .exists ():
182
- stat = self .path .stat ()
183
- if isinstance (stat , stat_result ):
184
- modification_time = self .path .stat ().st_mtime
185
- return hash_path (self .path , modification_time )
186
- if isinstance (stat , dict ):
187
- return stat .get ("ETag" , "0" )
188
- msg = "Unknown stat object."
189
- raise NotImplementedError (msg )
183
+ return _get_state (self .path )
190
184
return None
191
185
192
186
def load (self , is_product : bool = False ) -> Path : # noqa: ARG002
@@ -322,14 +316,7 @@ def from_path(cls, path: Path) -> PickleNode:
322
316
323
317
def state (self ) -> str | None :
324
318
if self .path .exists ():
325
- stat = self .path .stat ()
326
- if isinstance (stat , stat_result ):
327
- modification_time = self .path .stat ().st_mtime
328
- return hash_path (self .path , modification_time )
329
- if isinstance (stat , dict ):
330
- return stat .get ("ETag" , "0" )
331
- msg = "Unknown stat object."
332
- raise NotImplementedError (msg )
319
+ return _get_state (self .path )
333
320
return None
334
321
335
322
def load (self , is_product : bool = False ) -> Any :
@@ -341,3 +328,19 @@ def load(self, is_product: bool = False) -> Any:
341
328
def save (self , value : Any ) -> None :
342
329
with self .path .open ("wb" ) as f :
343
330
pickle .dump (value , f )
331
+
332
+
333
+ def _get_state (path : Path ) -> str :
334
+ """Get state of a path.
335
+
336
+ A simple function to handle local and remote files.
337
+
338
+ """
339
+ stat = path .stat ()
340
+ if isinstance (stat , stat_result ):
341
+ modification_time = path .stat ().st_mtime
342
+ return hash_path (path , modification_time )
343
+ if isinstance (stat , UPathStatResult ):
344
+ return stat .as_info ().get ("ETag" , "0" )
345
+ msg = "Unknown stat object."
346
+ raise NotImplementedError (msg )
0 commit comments