Skip to content

Commit

Permalink
Make ObjectStore a base class instead of a type
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Jan 31, 2025
1 parent ffa3dd9 commit 1f47967
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
4 changes: 3 additions & 1 deletion pyo3-object_store/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use pyo3::prelude::*;

use crate::error::*;
use crate::{
PyAzureStore, PyGCSStore, PyHttpStore, PyLocalStore, PyMemoryStore, PyPrefixStore, PyS3Store,
PyAzureStore, PyBaseObjectStore, PyGCSStore, PyHttpStore, PyLocalStore, PyMemoryStore,
PyPrefixStore, PyS3Store,
};

/// Export the default Python API as a submodule named `store` within the given parent module
Expand Down Expand Up @@ -46,6 +47,7 @@ pub fn register_store_module(

let child_module = PyModule::new(parent_module.py(), "store")?;

child_module.add_class::<PyBaseObjectStore>()?;
child_module.add_class::<PyAzureStore>()?;
child_module.add_class::<PyGCSStore>()?;
child_module.add_class::<PyHttpStore>()?;
Expand Down
15 changes: 8 additions & 7 deletions pyo3-object_store/src/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use crate::client::PyClientOptions;
use crate::config::PyConfigValue;
use crate::error::{ObstoreError, PyObjectStoreError, PyObjectStoreResult};
use crate::retry::PyRetryConfig;
use crate::PyBaseObjectStore;

/// A Python-facing wrapper around an [`AmazonS3`].
#[pyclass(name = "S3Store", frozen)]
#[pyclass(name = "S3Store", frozen, extends=PyBaseObjectStore)]
pub struct PyS3Store(Arc<AmazonS3>);

impl AsRef<Arc<AmazonS3>> for PyS3Store {
Expand All @@ -41,7 +42,7 @@ impl PyS3Store {
client_options: Option<PyClientOptions>,
retry_config: Option<PyRetryConfig>,
kwargs: Option<PyAmazonS3Config>,
) -> PyObjectStoreResult<Self> {
) -> PyObjectStoreResult<(Self, PyBaseObjectStore)> {
let mut builder = AmazonS3Builder::from_env();
if let Some(bucket) = bucket {
builder = builder.with_bucket_name(bucket);
Expand All @@ -55,7 +56,7 @@ impl PyS3Store {
if let Some(retry_config) = retry_config {
builder = builder.with_retry(retry_config.into())
}
Ok(Self(Arc::new(builder.build()?)))
Ok((Self(Arc::new(builder.build()?)), PyBaseObjectStore {}))
}

// Create from an existing boto3.Session or botocore.session.Session object
Expand All @@ -71,7 +72,7 @@ impl PyS3Store {
client_options: Option<PyClientOptions>,
retry_config: Option<PyRetryConfig>,
kwargs: Option<PyAmazonS3Config>,
) -> PyObjectStoreResult<Self> {
) -> PyObjectStoreResult<(Self, PyBaseObjectStore)> {
// boto3.Session has a region_name attribute, but botocore.session.Session does not.
let region = if let Ok(region) = session.getattr(intern!(py, "region_name")) {
region.extract::<Option<String>>()?
Expand Down Expand Up @@ -118,7 +119,7 @@ impl PyS3Store {
builder = builder.with_retry(retry_config.into())
}

Ok(Self(Arc::new(builder.build()?)))
Ok((Self(Arc::new(builder.build()?)), PyBaseObjectStore {}))
}

#[classmethod]
Expand All @@ -130,7 +131,7 @@ impl PyS3Store {
client_options: Option<PyClientOptions>,
retry_config: Option<PyRetryConfig>,
kwargs: Option<PyAmazonS3Config>,
) -> PyObjectStoreResult<Self> {
) -> PyObjectStoreResult<(Self, PyBaseObjectStore)> {
let mut builder = AmazonS3Builder::from_env().with_url(url);
if let Some(config_kwargs) = combine_config_kwargs(config, kwargs)? {
builder = config_kwargs.apply_config(builder);
Expand All @@ -141,7 +142,7 @@ impl PyS3Store {
if let Some(retry_config) = retry_config {
builder = builder.with_retry(retry_config.into())
}
Ok(Self(Arc::new(builder.build()?)))
Ok((Self(Arc::new(builder.build()?)), PyBaseObjectStore {}))
}

fn __repr__(&self) -> String {
Expand Down
5 changes: 5 additions & 0 deletions pyo3-object_store/src/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use pyo3::prelude::*;

/// Base class from which all other stores subclass
#[pyclass(name = "BaseObjectStore", frozen, subclass)]
pub struct PyBaseObjectStore {}
2 changes: 2 additions & 0 deletions pyo3-object_store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
mod api;
mod aws;
mod azure;
mod base;
mod client;
mod config;
pub(crate) mod error;
Expand All @@ -18,6 +19,7 @@ mod store;
pub use api::{register_exceptions_module, register_store_module};
pub use aws::PyS3Store;
pub use azure::PyAzureStore;
pub use base::PyBaseObjectStore;
pub use client::{PyClientConfigKey, PyClientOptions};
pub use error::{PyObjectStoreError, PyObjectStoreResult};
pub use gcp::PyGCSStore;
Expand Down

0 comments on commit 1f47967

Please sign in to comment.