Skip to content

Commit 65cbe0e

Browse files
tehunterThomas Hap--
authored
Add st_birthtime as standard field (#254)
* Add .vscode to .gitignore * Add tests for stat time values * Add tests for birthtime * Move birthtime implementation * upath._stat: fix linter error --------- Co-authored-by: Thomas H <[email protected]> Co-authored-by: Andreas Poehlmann <[email protected]>
1 parent b2eff7e commit 65cbe0e

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,6 @@ cython_debug/
139139

140140
# setuptools_scm
141141
upath/_version.py
142+
143+
# vscode workspace settings
144+
.vscode/

upath/_stat.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,29 +281,33 @@ def st_ctime(self) -> int | float:
281281
pass
282282
return self._seq[9]
283283

284+
@property
285+
def st_birthtime(self) -> int | float:
286+
"""time of creation"""
287+
for key in [
288+
"birthtime",
289+
"created",
290+
"creation_time",
291+
"timeCreated",
292+
"created_at",
293+
]:
294+
try:
295+
raw_value = self._info[key]
296+
except KeyError:
297+
continue
298+
try:
299+
return _convert_value_to_timestamp(raw_value)
300+
except (TypeError, ValueError):
301+
pass
302+
raise AttributeError("birthtime")
303+
284304
# --- extra fields ------------------------------------------------
285305

286306
def __getattr__(self, item):
287307
if item in self._fields_extra:
288308
return 0 # fallback default value
289309
raise AttributeError(item)
290310

291-
if "st_birthtime" in _fields_extra:
292-
293-
@property
294-
def st_birthtime(self) -> int | float:
295-
"""time of creation"""
296-
for key in ["created", "creation_time", "timeCreated", "created_at"]:
297-
try:
298-
raw_value = self._info[key]
299-
except KeyError:
300-
continue
301-
try:
302-
return _convert_value_to_timestamp(raw_value)
303-
except (TypeError, ValueError):
304-
pass
305-
return 0
306-
307311
# --- os.stat_result tuple interface ------------------------------
308312

309313
def __len__(self) -> int:

upath/tests/test_stat.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,45 @@ def test_stat_as_info(pth_file):
2525

2626

2727
def test_stat_atime(pth_file):
28-
assert isinstance(pth_file.stat().st_atime, (float, int))
28+
atime = pth_file.stat().st_atime
29+
assert isinstance(atime, (float, int))
30+
31+
32+
@pytest.mark.xfail(reason="fsspec does not return 'atime'")
33+
def test_stat_atime_value(pth_file):
34+
atime = pth_file.stat().st_atime
35+
assert atime > 0
2936

3037

3138
def test_stat_mtime(pth_file):
32-
assert isinstance(pth_file.stat().st_mtime, (float, int))
39+
mtime = pth_file.stat().st_mtime
40+
assert isinstance(mtime, (float, int))
41+
42+
43+
def test_stat_mtime_value(pth_file):
44+
mtime = pth_file.stat().st_mtime
45+
assert mtime > 0
3346

3447

3548
def test_stat_ctime(pth_file):
36-
assert isinstance(pth_file.stat().st_ctime, (float, int))
49+
ctime = pth_file.stat().st_ctime
50+
assert isinstance(ctime, (float, int))
51+
52+
53+
@pytest.mark.xfail(reason="fsspec returns 'created' but not 'ctime'")
54+
def test_stat_ctime_value(pth_file):
55+
ctime = pth_file.stat().st_ctime
56+
assert ctime > 0
57+
58+
59+
def test_stat_birthtime(pth_file):
60+
birthtime = pth_file.stat().st_birthtime
61+
assert isinstance(birthtime, (float, int))
62+
63+
64+
def test_stat_birthtime_value(pth_file):
65+
birthtime = pth_file.stat().st_birthtime
66+
assert birthtime > 0
3767

3868

3969
def test_stat_seq_interface(pth_file):

0 commit comments

Comments
 (0)