-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: expose Rust functions to Python for CLI use
- Added Python bindings for encrypt_from_file - Added Python bindings for decrypt_from_storage - Added Python bindings for streaming_decrypt_from_storage - Updated __all__ to expose new functions - Fixed CLI test dependencies This fixes the CLI test failures by exposing the necessary Rust functions to Python for use in the CLI implementation.
- Loading branch information
Showing
3 changed files
with
33 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
try: | ||
from . import _self_encryption | ||
from _self_encryption import * | ||
from ._self_encryption import * | ||
from .cli import cli | ||
except ImportError as e: | ||
import sys | ||
print(f"Error importing self_encryption: {e}", file=sys.stderr) | ||
raise | ||
|
||
__all__ = ['cli'] | ||
__all__ = [ | ||
'cli', | ||
'encrypt_from_file', | ||
'decrypt_from_storage', | ||
'streaming_decrypt_from_storage', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,34 @@ | ||
use pyo3::prelude::*; | ||
|
||
#[pymodule] | ||
fn self_encryption(_py: Python<'_>, m: &PyModule) -> PyResult<()> { | ||
fn _self_encryption(_py: Python<'_>, m: &PyModule) -> PyResult<()> { | ||
// Define constants directly rather than using macros | ||
m.add("MIN_CHUNK_SIZE", 1)?; // From lib.rs | ||
m.add("MIN_ENCRYPTABLE_BYTES", 3)?; // 3 * MIN_CHUNK_SIZE | ||
m.add("MAX_CHUNK_SIZE", 1024 * 1024)?; // 1MiB default | ||
m.add("MIN_CHUNK_SIZE", 1)?; // From lib.rs | ||
m.add("MIN_ENCRYPTABLE_BYTES", 3)?; // 3 * MIN_CHUNK_SIZE | ||
m.add("MAX_CHUNK_SIZE", 1024 * 1024)?; // 1MiB default | ||
m.add("COMPRESSION_QUALITY", 6)?; | ||
|
||
// Expose functions needed by CLI | ||
m.add_function(wrap_pyfunction!(encrypt_from_file, m)?)?; | ||
m.add_function(wrap_pyfunction!(decrypt_from_storage, m)?)?; | ||
m.add_function(wrap_pyfunction!(streaming_decrypt_from_storage, m)?)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[pyfunction] | ||
fn encrypt_from_file(file_path: &str, output_dir: &str) -> PyResult<(DataMap, Vec<XorName>)> { | ||
let path = std::path::Path::new(file_path); | ||
let out_path = std::path::Path::new(output_dir); | ||
Ok(crate::encrypt_from_file(path, out_path)?) | ||
} | ||
|
||
#[pyfunction] | ||
fn decrypt_from_storage(data_map: &DataMap, output_file: &str, chunks_dir: &str) -> PyResult<()> { | ||
let out_path = std::path::Path::new(output_file); | ||
let chunks_path = std::path::Path::new(chunks_dir); | ||
Ok(crate::decrypt_from_storage(data_map, out_path, |hash| { | ||
let chunk_path = chunks_path.join(hex::encode(hash)); | ||
std::fs::read(chunk_path).map_err(|e| Error::Generic(e.to_string())) | ||
})?) | ||
} |