Skip to content

Commit aab07e7

Browse files
authored
Optionally create root dir in LocalStore (#177)
1 parent e71b2cc commit aab07e7

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

obstore/python/obstore/store/__init__.pyi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ class LocalStore:
2727
```
2828
"""
2929
def __init__(
30-
self, prefix: str | Path | None = None, *, automatic_cleanup: bool = False
30+
self,
31+
prefix: str | Path | None = None,
32+
*,
33+
automatic_cleanup: bool = False,
34+
mkdir: bool = False,
3135
) -> None:
3236
"""Create a new LocalStore.
3337
@@ -36,6 +40,7 @@ class LocalStore:
3640
3741
Keyword Args:
3842
automatic_cleanup: if `True`, enables automatic cleanup of empty directories when deleting files. Defaults to False.
43+
mkdir: if `True` and `prefix` is not `None`, the directory at `prefix` will attempt to be created. Note that this root directory will not be cleaned up, even if `automatic_cleanup` is `True`.
3944
"""
4045
def __repr__(self) -> str: ...
4146
@classmethod

pyo3-object_store/src/local.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fs::create_dir_all;
12
use std::sync::Arc;
23

34
use object_store::local::LocalFileSystem;
@@ -29,12 +30,16 @@ impl PyLocalStore {
2930
#[pymethods]
3031
impl PyLocalStore {
3132
#[new]
32-
#[pyo3(signature = (prefix = None, *, automatic_cleanup=false))]
33+
#[pyo3(signature = (prefix = None, *, automatic_cleanup=false, mkdir=false))]
3334
fn py_new(
3435
prefix: Option<std::path::PathBuf>,
3536
automatic_cleanup: bool,
37+
mkdir: bool,
3638
) -> PyObjectStoreResult<Self> {
3739
let fs = if let Some(prefix) = prefix {
40+
if mkdir {
41+
create_dir_all(&prefix)?;
42+
}
3843
LocalFileSystem::new_with_prefix(prefix)?
3944
} else {
4045
LocalFileSystem::new()

tests/store/test_local.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import tempfile
12
from pathlib import Path
23

34
import pytest
@@ -42,3 +43,14 @@ def test_local_from_url():
4243
url = f"file://{HERE.absolute()}//"
4344
with pytest.raises(GenericError):
4445
store = LocalStore.from_url(url)
46+
47+
48+
def test_create_prefix():
49+
tmpdir = Path(tempfile.gettempdir()) / "abc"
50+
assert not tmpdir.exists()
51+
LocalStore(tmpdir, mkdir=True)
52+
assert tmpdir.exists()
53+
54+
# Assert that mkdir=True works even when the dir already exists
55+
LocalStore(tmpdir, mkdir=True)
56+
assert tmpdir.exists()

0 commit comments

Comments
 (0)