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 the Formatter API for python lib #277

Merged
merged 4 commits into from
Jan 13, 2025

Conversation

latonis
Copy link
Contributor

@latonis latonis commented Dec 30, 2024

  • exposed the format API via the Python lib
  • updated the typing stub
  • documented the functions in Py
  • added __pycache__ to be ignored
  • test for python formatting

@plusvic
Copy link
Member

plusvic commented Dec 30, 2024

I think the Python API for the formatter should mimic the Rust API as much as possible. For instance, the Formatter::format method should receive two file objects, one for reading the unformatted code and the other for writing the formatted code. This API allows using the Formatter::format method with in-memory data, but also with file objects, which is what most people will probably want, as they are formatting files in most cases. Additionally, this way the Python API doesn't need to make copies of the input or output data, it simply passes the file objects down to the yara-x-fmt API.

The serialize_into and deserialize_from methods can be used as examples of how to convert a Python's file object into a Rust reader or writer, that can be passed to the Rust API.

yara-x/py/src/lib.rs

Lines 528 to 548 in 4d59017

fn serialize_into(&self, file: PyObject) -> PyResult<()> {
let f = PyFileLikeObject::with_requirements(
file, false, true, false, false,
)?;
self.inner
.rules
.serialize_into(f)
.map_err(|err| PyIOError::new_err(err.to_string()))
}
/// Deserializes rules from a file-like object.
#[staticmethod]
fn deserialize_from(file: PyObject) -> PyResult<Py<Rules>> {
let f = PyFileLikeObject::with_requirements(
file, true, false, false, false,
)?;
let rules = yrx::Rules::deserialize_from(f)
.map_err(|err| PyIOError::new_err(err.to_string()))?;
Python::with_gil(|py| Py::new(py, Rules::new(rules)))
}

@latonis
Copy link
Contributor Author

latonis commented Dec 31, 2024

I think the Python API for the formatter should mimic the Rust API as much as possible. For instance, the Formatter::format method should receive two file objects, one for reading the unformatted code and the other for writing the formatted code. This API allows using the Formatter::format method with in-memory data, but also with file objects, which is what most people will probably want, as they are formatting files in most cases. Additionally, this way the Python API doesn't need to make copies of the input or output data, it simply passes the file objects down to the yara-x-fmt API.

The serialize_into and deserialize_from methods can be used as examples of how to convert a Python's file object into a Rust reader or writer, that can be passed to the Rust API.

yara-x/py/src/lib.rs

Lines 528 to 548 in 4d59017

fn serialize_into(&self, file: PyObject) -> PyResult<()> {
let f = PyFileLikeObject::with_requirements(
file, false, true, false, false,
)?;
self.inner
.rules
.serialize_into(f)
.map_err(|err| PyIOError::new_err(err.to_string()))
}
/// Deserializes rules from a file-like object.
#[staticmethod]
fn deserialize_from(file: PyObject) -> PyResult<Py<Rules>> {
let f = PyFileLikeObject::with_requirements(
file, true, false, false, false,
)?;
let rules = yrx::Rules::deserialize_from(f)
.map_err(|err| PyIOError::new_err(err.to_string()))?;
Python::with_gil(|py| Py::new(py, Rules::new(rules)))
}

Ahh good point😸 , fixed in 53bae8f

@plusvic plusvic merged commit e760015 into VirusTotal:main Jan 13, 2025
15 checks passed
@latonis latonis deleted the py-format branch January 13, 2025 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants