2525#include < vector>
2626
2727#include " absl/log/absl_check.h"
28+ #include " common/container.h"
2829#include " env/config.h"
2930#include " cel_expr_python/py_cel_activation.h"
3031#include " cel_expr_python/py_cel_arena.h"
@@ -42,13 +43,48 @@ namespace cel_python {
4243namespace py = ::pybind11;
4344
4445void PyCelEnv::DefinePythonBindings (pybind11::module & m) {
46+ py::class_<cel::ExpressionContainer>(m, " ExpressionContainer" )
47+ .def (py::init (
48+ [](const std::string& name,
49+ const std::optional<std::vector<std::string>>& abbreviations,
50+ const std::optional<
51+ std::unordered_map<std::string, std::string>>& aliases) {
52+ auto container =
53+ ThrowIfError (cel::MakeExpressionContainer (name));
54+
55+ if (abbreviations) {
56+ for (const auto & abrev : *abbreviations) {
57+ auto status = container.AddAbbreviation (abrev);
58+ if (!status.ok ()) {
59+ throw StatusToException (status);
60+ }
61+ }
62+ }
63+
64+ if (aliases) {
65+ for (const auto & [alias, full_name] : *aliases) {
66+ auto status = container.AddAlias (alias, full_name);
67+ if (!status.ok ()) {
68+ throw StatusToException (status);
69+ }
70+ }
71+ }
72+
73+ return container;
74+ }),
75+ py::arg (" name" ) = " " , py::arg (" abbreviations" ) = py::none (),
76+ py::arg (" aliases" ) = py::none ())
77+ .def (" container" , [](const cel::ExpressionContainer& self) {
78+ return std::string (self.container ());
79+ });
80+
4581 py::class_<PyCelEnv, std::shared_ptr<PyCelEnv>> cel_class (m, " Env" );
4682 m.def (
4783 " NewEnv" ,
4884 [](py::object descriptor_pool, std::optional<PyCelEnvConfig>& config,
4985 std::optional<std::unordered_map<std::string, PyCelType>>& variables,
5086 std::optional<std::vector<py::object>>& extensions,
51- const std::optional<std::string>& container,
87+ py::object container,
5288 std::optional<std::vector<std::shared_ptr<PyCelFunctionDecl>>>&
5389 functions,
5490 std::optional<std::unordered_map<std::string, py::object>>&
@@ -77,10 +113,23 @@ void PyCelEnv::DefinePythonBindings(pybind11::module& m) {
77113 }
78114 }
79115
116+ cel::ExpressionContainer expr_container;
117+ if (!container.is_none ()) {
118+ if (py::isinstance<py::str>(container)) {
119+ expr_container = ThrowIfError (
120+ cel::MakeExpressionContainer (container.cast <std::string>()));
121+ } else if (py::isinstance<cel::ExpressionContainer>(container)) {
122+ expr_container = container.cast <cel::ExpressionContainer>();
123+ } else {
124+ throw py::type_error (
125+ " container must be a string or ExpressionContainer" );
126+ }
127+ }
128+
80129 return PyCelEnv (config.value_or (PyCelEnvConfig ()), pool_ptr,
81130 std::move (variables).value_or (
82131 std::unordered_map<std::string, PyCelType>{}),
83- ext_ptrs, container. value_or ( " " ),
132+ ext_ptrs, std::move (expr_container ),
84133 functions.value_or (
85134 std::vector<std::shared_ptr<PyCelFunctionDecl>>{}),
86135 function_impls.value_or (
@@ -125,7 +174,8 @@ void PyCelEnv::DefinePythonBindings(pybind11::module& m) {
125174PyCelEnv::PyCelEnv (
126175 const PyCelEnvConfig& config, PyObject* descriptor_pool,
127176 const std::unordered_map<std::string, PyCelType>& variable_types,
128- const std::vector<PyObject*>& extensions, const std::string& container,
177+ const std::vector<PyObject*>& extensions,
178+ cel::ExpressionContainer container,
129179 const std::vector<std::shared_ptr<PyCelFunctionDecl>>& functions,
130180 const std::unordered_map<std::string, py::object>& function_impls) {
131181 env_ = ThrowIfError (PyCelEnvInternal::NewCelEnvInternal (
0 commit comments