Skip to content

Commit

Permalink
Ensure LocalStore.prefix is a pathlib.Path (#219)
Browse files Browse the repository at this point in the history
* Ensure `LocalStore.prefix` is a `pathlib.Path`

* bump to 0.4.0-beta.3
  • Loading branch information
kylebarron authored Feb 5, 2025
1 parent e621e9b commit 12551fd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion obstore/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "obstore"
version = "0.4.0-beta.2"
version = "0.4.0-beta.3"
authors = { workspace = true }
edition = { workspace = true }
description = "The simplest, highest-throughput interface to Amazon S3, Google Cloud Storage, Azure Blob Storage, and S3-compliant APIs like Cloudflare R2."
Expand Down
3 changes: 3 additions & 0 deletions obstore/python/obstore/store/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ class LocalStore:

def __getnewargs_ex__(self): ...
def __repr__(self) -> str: ...
@property
def prefix(self) -> Path | None:
"""Get the prefix applied to all operations in this store, if any."""

class MemoryStore:
"""A fully in-memory implementation of ObjectStore.
Expand Down
16 changes: 14 additions & 2 deletions pyo3-object_store/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,19 @@ impl PyLocalStore {
}

#[getter]
fn prefix(&self) -> Option<&std::path::PathBuf> {
self.config.prefix.as_ref()
fn prefix(&self, py: Python) -> PyResult<PyObject> {
// Note: returning a std::path::Path or std::path::PathBuf converts back to a Python _str_
// not a Python _pathlib.Path_.
// So we manually convert to a pathlib.Path
if let Some(prefix) = &self.config.prefix {
let pathlib_mod = py.import(intern!(py, "pathlib"))?;
let path_object = pathlib_mod.call_method1(
intern!(py, "Path"),
PyTuple::new(py, vec![prefix.into_pyobject(py)?])?,
)?;
path_object.into_py_any(py)
} else {
Ok(py.None())
}
}
}
9 changes: 9 additions & 0 deletions tests/store/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@ def test_create_prefix():
# Assert that mkdir=True works even when the dir already exists
LocalStore(tmpdir, mkdir=True)
assert tmpdir.exists()


def test_prefix_property():
tmpdir = Path(tempfile.gettempdir())
store = LocalStore(tmpdir)
assert store.prefix == tmpdir
assert isinstance(store.prefix, Path)
# Can pass it back to the store init
LocalStore(store.prefix)

0 comments on commit 12551fd

Please sign in to comment.