Skip to content

Commit

Permalink
Return plugins via Python API
Browse files Browse the repository at this point in the history
  • Loading branch information
tesujimath committed Feb 5, 2024
1 parent 1cc0c4b commit 99c2552
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
4 changes: 4 additions & 0 deletions beancount-parser-lima-python/python-examples/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def main():
print(result.options)

print("Assets account is called '%s'" % result.options.account_name_by_type["Assets"])

for plugin in result.plugins:
print("plugin \"%s\"%s" % (plugin.module_name, (" with config \"%s\"" % plugin.config) if plugin.config is not None else " without config"))
# print(plugin)

sources.write(result.warnings)
else:
Expand Down
13 changes: 13 additions & 0 deletions beancount-parser-lima-python/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,19 @@ impl Converter {
long_string_maxlines,
})
}

pub(crate) fn plugin(&mut self, py: Python<'_>, x: &lima::Plugin<'_>) -> PyResult<Plugin> {
let module_name = self.string.create_or_reuse(py, x.module_name());
let config = x
.config()
.as_ref()
.map(|config| self.string.create_or_reuse(py, config.item()));

Ok(Plugin {
module_name,
config,
})
}
}

struct StringFactory {
Expand Down
12 changes: 10 additions & 2 deletions beancount-parser-lima-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn parse(py: Python<'_>, parser: &lima::BeancountParser) -> PyResult<Py<PyAny>>
Ok(lima::ParseSuccess {
directives,
options,
plugins: _,
plugins,
warnings,
}) => {
use lima::DirectiveVariant as V;
Expand All @@ -93,6 +93,10 @@ fn parse(py: Python<'_>, parser: &lima::BeancountParser) -> PyResult<Py<PyAny>>
.collect::<PyResult<Vec<Py<PyAny>>>>()?;

let options = c.options(py, &options)?;
let plugins = plugins
.iter()
.map(|x| c.plugin(py, x))
.collect::<PyResult<Vec<Plugin>>>()?;

let warnings = warnings.into_iter().map(Warning::new).collect::<Vec<_>>();

Expand All @@ -102,6 +106,7 @@ fn parse(py: Python<'_>, parser: &lima::BeancountParser) -> PyResult<Py<PyAny>>
ParseSuccess {
directives,
options,
plugins,
warnings,
},
ParseResult {},
Expand Down Expand Up @@ -132,7 +137,8 @@ pub struct ParseSuccess {
pub(crate) directives: Vec<Py<PyAny>>,
#[pyo3(get)]
pub(crate) options: Options,
// TODO plugins
#[pyo3(get)]
pub(crate) plugins: Vec<Plugin>,
#[pyo3(get)]
pub(crate) warnings: Vec<Warning>,
}
Expand Down Expand Up @@ -171,4 +177,6 @@ impl Warning {

mod conversions;
use conversions::Converter;

use crate::types::Plugin;
mod types;
10 changes: 10 additions & 0 deletions beancount-parser-lima-python/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,14 @@ pub(crate) struct Options {
pub(crate) long_string_maxlines: usize,
}

/// A Beancount plugin pragma.
#[derive(Clone, Debug)]
#[pyclass(frozen)]
pub(crate) struct Plugin {
#[pyo3(get)]
pub(crate) module_name: Py<PyString>,
#[pyo3(get)]
pub(crate) config: Option<Py<PyString>>,
}

mod format;
20 changes: 20 additions & 0 deletions beancount-parser-lima-python/src/types/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,26 @@ where
}
}

#[pymethods]
impl Plugin {
fn __str__(&self, _py: Python<'_>) -> String {
self.to_string()
}
}

impl Display for Plugin {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "plugin \"{}\"", self.module_name)?;
if let Some(config) = self.config.as_ref() {
writeln!(f, " \"{}\"", config)?;
} else {
writeln!(f)?;
}

Ok(())
}
}

/// Format the given container, with optional prefix, applying `mapper` to each element,
/// and with the given `separator`.
fn format<C, T, M, D>(
Expand Down

0 comments on commit 99c2552

Please sign in to comment.