Skip to content

Commit

Permalink
Add st_birthtime as standard field (#254)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Aug 23, 2024
1 parent b2eff7e commit 65cbe0e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,6 @@ cython_debug/

# setuptools_scm
upath/_version.py

# vscode workspace settings
.vscode/
36 changes: 20 additions & 16 deletions upath/_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,29 +281,33 @@ def st_ctime(self) -> int | float:
pass
return self._seq[9]

@property
def st_birthtime(self) -> int | float:
"""time of creation"""
for key in [
"birthtime",
"created",
"creation_time",
"timeCreated",
"created_at",
]:
try:
raw_value = self._info[key]
except KeyError:
continue
try:
return _convert_value_to_timestamp(raw_value)
except (TypeError, ValueError):
pass
raise AttributeError("birthtime")

# --- extra fields ------------------------------------------------

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

if "st_birthtime" in _fields_extra:

@property
def st_birthtime(self) -> int | float:
"""time of creation"""
for key in ["created", "creation_time", "timeCreated", "created_at"]:
try:
raw_value = self._info[key]
except KeyError:
continue
try:
return _convert_value_to_timestamp(raw_value)
except (TypeError, ValueError):
pass
return 0

# --- os.stat_result tuple interface ------------------------------

def __len__(self) -> int:
Expand Down
36 changes: 33 additions & 3 deletions upath/tests/test_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,45 @@ def test_stat_as_info(pth_file):


def test_stat_atime(pth_file):
assert isinstance(pth_file.stat().st_atime, (float, int))
atime = pth_file.stat().st_atime
assert isinstance(atime, (float, int))


@pytest.mark.xfail(reason="fsspec does not return 'atime'")
def test_stat_atime_value(pth_file):
atime = pth_file.stat().st_atime
assert atime > 0


def test_stat_mtime(pth_file):
assert isinstance(pth_file.stat().st_mtime, (float, int))
mtime = pth_file.stat().st_mtime
assert isinstance(mtime, (float, int))


def test_stat_mtime_value(pth_file):
mtime = pth_file.stat().st_mtime
assert mtime > 0


def test_stat_ctime(pth_file):
assert isinstance(pth_file.stat().st_ctime, (float, int))
ctime = pth_file.stat().st_ctime
assert isinstance(ctime, (float, int))


@pytest.mark.xfail(reason="fsspec returns 'created' but not 'ctime'")
def test_stat_ctime_value(pth_file):
ctime = pth_file.stat().st_ctime
assert ctime > 0


def test_stat_birthtime(pth_file):
birthtime = pth_file.stat().st_birthtime
assert isinstance(birthtime, (float, int))


def test_stat_birthtime_value(pth_file):
birthtime = pth_file.stat().st_birthtime
assert birthtime > 0


def test_stat_seq_interface(pth_file):
Expand Down

0 comments on commit 65cbe0e

Please sign in to comment.