Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement more detailed error types #51

Merged
merged 5 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions python/python_calamine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@
CalamineError,
CalamineSheet,
CalamineWorkbook,
PasswordError,
SheetMetadata,
SheetTypeEnum,
SheetVisibleEnum,
WorksheetNotFound,
XmlError,
ZipError,
load_workbook,
)

__all__ = (
"CalamineError",
"CalamineSheet",
"CalamineWorkbook",
"PasswordError",
"SheetMetadata",
"SheetTypeEnum",
"SheetVisibleEnum",
"WorksheetNotFound",
"XmlError",
"ZipError",
"load_workbook",
)
4 changes: 4 additions & 0 deletions python/python_calamine/_python_calamine.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class CalamineWorkbook:
def get_sheet_by_index(self, index: int) -> CalamineSheet: ...

class CalamineError(Exception): ...
class PasswordError(CalamineError): ...
class WorksheetNotFound(CalamineError): ...
class XmlError(CalamineError): ...
class ZipError(CalamineError): ...

def load_workbook(
path_or_filelike: str | os.PathLike | ReadBuffer,
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use pyo3::prelude::*;
mod types;
mod utils;
use crate::types::{
CalamineError, CalamineSheet, CalamineWorkbook, CellValue, SheetMetadata, SheetTypeEnum,
SheetVisibleEnum,
CalamineError, CalamineSheet, CalamineWorkbook, CellValue, PasswordError, SheetMetadata,
SheetTypeEnum, SheetVisibleEnum, WorksheetNotFound, XmlError, ZipError,
};

#[pyfunction]
Expand All @@ -21,5 +21,9 @@ fn _python_calamine(py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<SheetTypeEnum>()?;
m.add_class::<SheetVisibleEnum>()?;
m.add("CalamineError", py.get_type::<CalamineError>())?;
m.add("PasswordError", py.get_type::<PasswordError>())?;
m.add("WorksheetNotFound", py.get_type::<WorksheetNotFound>())?;
m.add("XmlError", py.get_type::<XmlError>())?;
m.add("ZipError", py.get_type::<ZipError>())?;
Ok(())
}
4 changes: 4 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ pub use sheet::{CalamineSheet, SheetMetadata, SheetTypeEnum, SheetVisibleEnum};
pub use workbook::CalamineWorkbook;

create_exception!(python_calamine, CalamineError, PyException);
create_exception!(python_calamine, PasswordError, CalamineError);
create_exception!(python_calamine, WorksheetNotFound, CalamineError);
create_exception!(python_calamine, XmlError, CalamineError);
create_exception!(python_calamine, ZipError, CalamineError);
22 changes: 3 additions & 19 deletions src/types/workbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use pyo3::types::{PyString, PyType};
use pyo3_file::PyFileLikeObject;

use crate::utils::err_to_py;
use crate::{CalamineError, CalamineSheet, SheetMetadata};
use crate::{CalamineSheet, SheetMetadata, WorksheetNotFound};

enum SheetsEnum {
File(Sheets<BufReader<File>>),
Expand Down Expand Up @@ -40,16 +40,6 @@ impl SheetsEnum {
SheetsEnum::FileLike(f) => f.worksheet_range(name),
}
}

fn worksheet_range_at(
&mut self,
index: usize,
) -> Option<Result<calamine::Range<calamine::Data>, Error>> {
match self {
SheetsEnum::File(f) => f.worksheet_range_at(index),
SheetsEnum::FileLike(f) => f.worksheet_range_at(index),
}
}
}

#[pyclass]
Expand Down Expand Up @@ -164,14 +154,8 @@ impl CalamineWorkbook {
let name = self
.sheet_names
.get(index)
.ok_or_else(|| CalamineError::new_err("Workbook is empty"))?
.ok_or_else(|| WorksheetNotFound::new_err(format!("Worksheet '{}' not found", index)))?
.to_string();
let range = self
.sheets
.worksheet_range_at(index)
.unwrap_or_else(|| Err(Error::Msg("Workbook is empty")))
.map_err(err_to_py)?;

Ok(CalamineSheet::new(name, range))
self.get_sheet_by_name(&name)
}
}
38 changes: 36 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
use calamine::Error;
use calamine::{Error, OdsError, XlsError, XlsbError, XlsxError};
use pyo3::exceptions::PyIOError;
use pyo3::PyErr;

use crate::types::CalamineError;
use crate::{CalamineError, PasswordError, WorksheetNotFound, XmlError, ZipError};

pub fn err_to_py(e: Error) -> PyErr {
match e {
Error::Io(err) => PyIOError::new_err(err.to_string()),
Error::Ods(ref err) => match err {
OdsError::Io(error) => PyIOError::new_err(error.to_string()),
OdsError::Zip(error) => ZipError::new_err(error.to_string()),
OdsError::Xml(error) => XmlError::new_err(error.to_string()),
OdsError::XmlAttr(error) => XmlError::new_err(error.to_string()),
OdsError::Password => PasswordError::new_err(err.to_string()),
OdsError::WorksheetNotFound(error) => WorksheetNotFound::new_err(error.to_string()),
_ => CalamineError::new_err(err.to_string()),
},
Error::Xls(ref err) => match err {
XlsError::Io(error) => PyIOError::new_err(error.to_string()),
XlsError::Password => PasswordError::new_err(err.to_string()),
XlsError::WorksheetNotFound(error) => WorksheetNotFound::new_err(error.to_string()),
_ => CalamineError::new_err(err.to_string()),
},
Error::Xlsx(ref err) => match err {
XlsxError::Io(error) => PyIOError::new_err(error.to_string()),
XlsxError::Zip(error) => ZipError::new_err(error.to_string()),
XlsxError::Xml(error) => XmlError::new_err(error.to_string()),
XlsxError::XmlAttr(error) => XmlError::new_err(error.to_string()),
XlsxError::XmlEof(error) => XmlError::new_err(error.to_string()),
XlsxError::Password => PasswordError::new_err(err.to_string()),
XlsxError::WorksheetNotFound(error) => WorksheetNotFound::new_err(error.to_string()),
_ => CalamineError::new_err(err.to_string()),
},
Error::Xlsb(ref err) => match err {
XlsbError::Io(error) => PyIOError::new_err(error.to_string()),
XlsbError::Zip(error) => ZipError::new_err(error.to_string()),
XlsbError::Xml(error) => XmlError::new_err(error.to_string()),
XlsbError::XmlAttr(error) => XmlError::new_err(error.to_string()),
XlsbError::Password => PasswordError::new_err(err.to_string()),
XlsbError::WorksheetNotFound(error) => WorksheetNotFound::new_err(error.to_string()),
_ => CalamineError::new_err(err.to_string()),
},
_ => CalamineError::new_err(e.to_string()),
}
}
Empty file added tests/data/empty_file.ods
Empty file.
Empty file added tests/data/empty_file.xlsb
Empty file.
Empty file added tests/data/empty_file.xlsx
Empty file.
Binary file added tests/data/password.ods
Binary file not shown.
Binary file added tests/data/password.xls
Binary file not shown.
Binary file added tests/data/password.xlsb
Binary file not shown.
Binary file added tests/data/password.xlsx
Binary file not shown.
58 changes: 57 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path

import pytest
from python_calamine import CalamineWorkbook
from python_calamine import CalamineWorkbook, PasswordError, WorksheetNotFound, ZipError

PATH = Path(__file__).parent / "data"

Expand Down Expand Up @@ -218,6 +218,62 @@ def test_nrows():
]


@pytest.mark.parametrize(
"path",
[
PATH / "base.xlsx",
PATH / "base.xls",
PATH / "base.xlsb",
PATH / "base.ods",
],
)
def test_worksheet_errors(path):
reader = CalamineWorkbook.from_object(path)
with pytest.raises(WorksheetNotFound):
reader.get_sheet_by_name("Sheet4")


@pytest.mark.parametrize(
"path",
[
PATH / "password.xlsx",
PATH / "password.xls",
PATH / "password.xlsb",
PATH / "password.ods",
],
)
def test_password_errors(path):
with pytest.raises(PasswordError):
CalamineWorkbook.from_object(path)


@pytest.mark.parametrize(
"path",
[
PATH / "empty_file.xlsx",
PATH / "empty_file.xlsb",
PATH / "empty_file.ods",
],
)
def test_zip_errors(path):
with pytest.raises(ZipError):
CalamineWorkbook.from_path(path)


@pytest.mark.parametrize(
"path",
[
PATH / "non_existent_file.xlsx",
PATH / "non_existent_file.xls",
PATH / "non_existent_file.xlsb",
PATH / "non_existent_file.ods",
],
)
def test_io_errors(path):
with pytest.raises(IOError):
CalamineWorkbook.from_path(path)


@pytest.mark.parametrize(
"path",
[
Expand Down