Skip to content

Commit

Permalink
refactor: more docs and fixes for Python
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Feb 5, 2025
1 parent 0da9a1d commit edacde0
Showing 1 changed file with 19 additions and 35 deletions.
54 changes: 19 additions & 35 deletions autonomi/src/python.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::client::{
chunk::DataMapChunk,
files::{archive_private::PrivateArchiveAccess, archive_public::ArchiveAddr},
payment::PaymentOption,
vault::{UserData, VaultSecretKey},
Client,
Expand Down Expand Up @@ -298,15 +297,17 @@ pub struct PyPointerAddress {

#[pymethods]
impl PyPointerAddress {
/// Creates a new pointer address from a hex string.
#[new]
pub fn new(hex_str: String) -> PyResult<Self> {
let bytes = hex::decode(&hex_str)
.map_err(|e| PyValueError::new_err(format!("`hex_str` is invalid: {e:?}")))?;
let xorname = XorName::from_content(&bytes);
/// Initialise pointer address from hex string.
#[staticmethod]
pub fn from_hex(hex: String) -> PyResult<Self> {
let bytes = hex::decode(hex)
.map_err(|e| PyValueError::new_err(format!("`hex` not a valid hex string: {e}")))?;
let bytes: [u8; 32] = bytes
.try_into()
.map_err(|_| PyValueError::new_err("`hex` invalid: must be 32 bytes"))?;

Ok(Self {
inner: PointerAddress::new(xorname),
inner: PointerAddress::new(XorName(bytes)),
})
}

Expand Down Expand Up @@ -370,11 +371,17 @@ pub struct PyPointerTarget {

#[pymethods]
impl PyPointerTarget {
/// Creates a new pointer target from a xorname byte array.
#[new]
fn new(xorname: &[u8]) -> PyResult<Self> {
/// Initialize a pointer target from a chunk address hex string.
#[staticmethod]
fn from_hex(hex: &str) -> PyResult<Self> {
let bytes = hex::decode(hex)
.map_err(|e| PyValueError::new_err(format!("`hex` not a valid hex string: {e}")))?;
let bytes: [u8; 32] = bytes
.try_into()
.map_err(|_| PyValueError::new_err("`hex` invalid: must be 32 bytes"))?;

Ok(Self {
inner: PointerTarget::ChunkAddress(ChunkAddress::new(XorName::from_content(xorname))),
inner: PointerTarget::ChunkAddress(ChunkAddress::new(XorName(bytes))),
})
}

Expand All @@ -392,14 +399,6 @@ impl PyPointerTarget {
}
}

/// Creates a pointer target from a xorname byte array.
#[staticmethod]
fn from_xorname(xorname: &[u8]) -> PyResult<Self> {
Ok(Self {
inner: PointerTarget::ChunkAddress(ChunkAddress::new(XorName::from_content(xorname))),
})
}

/// Creates a pointer target from a chunk address.
#[staticmethod]
fn from_chunk_address(addr: &PyChunkAddress) -> Self {
Expand Down Expand Up @@ -673,21 +672,6 @@ impl PyUserData {
}
}

fn add_file_archive(&mut self, archive: &str) -> Option<String> {
let name = XorName::from_content(archive.as_bytes());
let archive_addr = ArchiveAddr::from_content(&name);
self.inner.add_file_archive(archive_addr)
}

fn add_private_file_archive(&mut self, archive: &str) -> Option<String> {
let name = XorName::from_content(archive.as_bytes());
let private_access = match PrivateArchiveAccess::from_hex(&name.to_string()) {
Ok(access) => access,
Err(_e) => return None,
};
self.inner.add_private_file_archive(private_access)
}

/// Returns a list of public file archives as (address, name) pairs.
fn file_archives(&self) -> Vec<(String, String)> {
self.inner
Expand Down

0 comments on commit edacde0

Please sign in to comment.