Python: allows again to use string for types (but with a deprecation warning)

pull/690/head
Tpt 10 months ago committed by Thomas Tanon
parent f445166942
commit 4f404ab650
  1. 33
      python/src/io.rs
  2. 28
      python/src/sparql.rs
  3. 8
      python/src/store.rs

@ -3,7 +3,7 @@
use crate::model::{hash, PyQuad, PyTriple}; use crate::model::{hash, PyQuad, PyTriple};
use oxigraph::io::{FromReadQuadReader, ParseError, RdfFormat, RdfParser, RdfSerializer}; use oxigraph::io::{FromReadQuadReader, ParseError, RdfFormat, RdfParser, RdfSerializer};
use oxigraph::model::QuadRef; use oxigraph::model::QuadRef;
use pyo3::exceptions::{PySyntaxError, PyValueError}; use pyo3::exceptions::{PyDeprecationWarning, PySyntaxError, PyValueError};
use pyo3::intern; use pyo3::intern;
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::types::PyBytes; use pyo3::types::PyBytes;
@ -53,7 +53,7 @@ use std::sync::OnceLock;
#[pyo3(signature = (input = None, format = None, *, path = None, base_iri = None, without_named_graphs = false, rename_blank_nodes = false))] #[pyo3(signature = (input = None, format = None, *, path = None, base_iri = None, without_named_graphs = false, rename_blank_nodes = false))]
pub fn parse( pub fn parse(
input: Option<PyReadableInput>, input: Option<PyReadableInput>,
format: Option<PyRdfFormat>, format: Option<PyRdfFormatInput>,
path: Option<PathBuf>, path: Option<PathBuf>,
base_iri: Option<&str>, base_iri: Option<&str>,
without_named_graphs: bool, without_named_graphs: bool,
@ -120,7 +120,7 @@ pub fn parse(
pub fn serialize<'a>( pub fn serialize<'a>(
input: &PyAny, input: &PyAny,
output: Option<PyWritableOutput>, output: Option<PyWritableOutput>,
format: Option<PyRdfFormat>, format: Option<PyRdfFormatInput>,
py: Python<'a>, py: Python<'a>,
) -> PyResult<Option<&'a PyBytes>> { ) -> PyResult<Option<&'a PyBytes>> {
PyWritable::do_write( PyWritable::do_write(
@ -519,9 +519,22 @@ impl Write for PyIo {
} }
} }
pub fn lookup_rdf_format(format: Option<PyRdfFormat>, path: Option<&Path>) -> PyResult<RdfFormat> { pub fn lookup_rdf_format(
format: Option<PyRdfFormatInput>,
path: Option<&Path>,
) -> PyResult<RdfFormat> {
if let Some(format) = format { if let Some(format) = format {
return Ok(format.inner); return match format {
PyRdfFormatInput::Object(format) => Ok(format.inner),
PyRdfFormatInput::MediaType(media_type) => {
deprecation_warning("Using string to specify a RDF format is deprecated, please use a RdfFormat object instead.")?;
RdfFormat::from_media_type(&media_type).ok_or_else(|| {
PyValueError::new_err(format!(
"The media type {media_type} is not supported by pyoxigraph"
))
})
}
};
} }
let Some(path) = path else { let Some(path) = path else {
return Err(PyValueError::new_err( return Err(PyValueError::new_err(
@ -538,6 +551,12 @@ pub fn lookup_rdf_format(format: Option<PyRdfFormat>, path: Option<&Path>) -> Py
.ok_or_else(|| PyValueError::new_err(format!("Not supported RDF format extension: {ext}"))) .ok_or_else(|| PyValueError::new_err(format!("Not supported RDF format extension: {ext}")))
} }
#[derive(FromPyObject)]
pub enum PyRdfFormatInput {
Object(PyRdfFormat),
MediaType(String),
}
pub fn map_parse_error(error: ParseError, file_path: Option<PathBuf>) -> PyErr { pub fn map_parse_error(error: ParseError, file_path: Option<PathBuf>) -> PyErr {
match error { match error {
ParseError::Syntax(error) => { ParseError::Syntax(error) => {
@ -608,3 +627,7 @@ pub fn python_version() -> (u8, u8) {
}) })
}) })
} }
pub fn deprecation_warning(message: &str) -> PyResult<()> {
Python::with_gil(|py| PyErr::warn(py, py.get_type::<PyDeprecationWarning>(), message, 0))
}

@ -240,7 +240,7 @@ impl PyQuerySolutions {
fn serialize<'a>( fn serialize<'a>(
&mut self, &mut self,
output: Option<PyWritableOutput>, output: Option<PyWritableOutput>,
format: Option<PyQueryResultsFormat>, format: Option<PyQueryResultsFormatInput>,
py: Python<'a>, py: Python<'a>,
) -> PyResult<Option<&'a PyBytes>> { ) -> PyResult<Option<&'a PyBytes>> {
PyWritable::do_write( PyWritable::do_write(
@ -340,7 +340,7 @@ impl PyQueryBoolean {
fn serialize<'a>( fn serialize<'a>(
&mut self, &mut self,
output: Option<PyWritableOutput>, output: Option<PyWritableOutput>,
format: Option<PyQueryResultsFormat>, format: Option<PyQueryResultsFormatInput>,
py: Python<'a>, py: Python<'a>,
) -> PyResult<Option<&'a PyBytes>> { ) -> PyResult<Option<&'a PyBytes>> {
PyWritable::do_write( PyWritable::do_write(
@ -418,7 +418,7 @@ impl PyQueryTriples {
fn serialize<'a>( fn serialize<'a>(
&mut self, &mut self,
output: Option<PyWritableOutput>, output: Option<PyWritableOutput>,
format: Option<PyRdfFormat>, format: Option<PyRdfFormatInput>,
py: Python<'a>, py: Python<'a>,
) -> PyResult<Option<&'a PyBytes>> { ) -> PyResult<Option<&'a PyBytes>> {
PyWritable::do_write( PyWritable::do_write(
@ -479,7 +479,7 @@ impl PyQueryTriples {
#[pyo3(signature = (input = None, format = None, *, path = None))] #[pyo3(signature = (input = None, format = None, *, path = None))]
pub fn parse_query_results( pub fn parse_query_results(
input: Option<PyReadableInput>, input: Option<PyReadableInput>,
format: Option<PyQueryResultsFormat>, format: Option<PyQueryResultsFormatInput>,
path: Option<PathBuf>, path: Option<PathBuf>,
py: Python<'_>, py: Python<'_>,
) -> PyResult<PyObject> { ) -> PyResult<PyObject> {
@ -650,11 +650,21 @@ impl PyQueryResultsFormat {
} }
pub fn lookup_query_results_format( pub fn lookup_query_results_format(
format: Option<PyQueryResultsFormat>, format: Option<PyQueryResultsFormatInput>,
path: Option<&Path>, path: Option<&Path>,
) -> PyResult<QueryResultsFormat> { ) -> PyResult<QueryResultsFormat> {
if let Some(format) = format { if let Some(format) = format {
return Ok(format.inner); return match format {
PyQueryResultsFormatInput::Object(format) => Ok(format.inner),
PyQueryResultsFormatInput::MediaType(media_type) => {
deprecation_warning("Using a string to specify a query results format is deprecated, please use a QueryResultsFormat object instead.")?;
QueryResultsFormat::from_media_type(&media_type).ok_or_else(|| {
PyValueError::new_err(format!(
"The media type {media_type} is not supported by pyoxigraph"
))
})
}
};
} }
let Some(path) = path else { let Some(path) = path else {
return Err(PyValueError::new_err( return Err(PyValueError::new_err(
@ -671,6 +681,12 @@ pub fn lookup_query_results_format(
.ok_or_else(|| PyValueError::new_err(format!("Not supported RDF format extension: {ext}"))) .ok_or_else(|| PyValueError::new_err(format!("Not supported RDF format extension: {ext}")))
} }
#[derive(FromPyObject)]
pub enum PyQueryResultsFormatInput {
Object(PyQueryResultsFormat),
MediaType(String),
}
pub fn map_evaluation_error(error: EvaluationError) -> PyErr { pub fn map_evaluation_error(error: EvaluationError) -> PyErr {
match error { match error {
EvaluationError::Parsing(error) => PySyntaxError::new_err(error.to_string()), EvaluationError::Parsing(error) => PySyntaxError::new_err(error.to_string()),

@ -1,7 +1,7 @@
#![allow(clippy::needless_option_as_deref)] #![allow(clippy::needless_option_as_deref)]
use crate::io::{ use crate::io::{
allow_threads_unsafe, lookup_rdf_format, map_parse_error, PyRdfFormat, PyReadable, allow_threads_unsafe, lookup_rdf_format, map_parse_error, PyRdfFormatInput, PyReadable,
PyReadableInput, PyWritable, PyWritableOutput, PyReadableInput, PyWritable, PyWritableOutput,
}; };
use crate::model::*; use crate::model::*;
@ -385,7 +385,7 @@ impl PyStore {
fn load( fn load(
&self, &self,
input: Option<PyReadableInput>, input: Option<PyReadableInput>,
format: Option<PyRdfFormat>, format: Option<PyRdfFormatInput>,
path: Option<PathBuf>, path: Option<PathBuf>,
base_iri: Option<&str>, base_iri: Option<&str>,
to_graph: Option<&PyAny>, to_graph: Option<&PyAny>,
@ -452,7 +452,7 @@ impl PyStore {
fn bulk_load( fn bulk_load(
&self, &self,
input: Option<PyReadableInput>, input: Option<PyReadableInput>,
format: Option<PyRdfFormat>, format: Option<PyRdfFormatInput>,
path: Option<PathBuf>, path: Option<PathBuf>,
base_iri: Option<&str>, base_iri: Option<&str>,
to_graph: Option<&PyAny>, to_graph: Option<&PyAny>,
@ -520,7 +520,7 @@ impl PyStore {
fn dump<'a>( fn dump<'a>(
&self, &self,
output: Option<PyWritableOutput>, output: Option<PyWritableOutput>,
format: Option<PyRdfFormat>, format: Option<PyRdfFormatInput>,
from_graph: Option<&PyAny>, from_graph: Option<&PyAny>,
py: Python<'a>, py: Python<'a>,
) -> PyResult<Option<&'a PyBytes>> { ) -> PyResult<Option<&'a PyBytes>> {

Loading…
Cancel
Save