-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpy_cel_env_internal.h
More file actions
123 lines (100 loc) · 4.19 KB
/
Copy pathpy_cel_env_internal.h
File metadata and controls
123 lines (100 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_INTERNAL_H_
#define THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_INTERNAL_H_
#include <Python.h> // IWYU pragma: keep - Needed for PyObject
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "compiler/compiler.h"
#include "runtime/runtime.h"
#include "runtime/runtime_builder.h"
#include "runtime/runtime_options.h"
#include "cel_expr_python/cel_extension.h"
#include "cel_expr_python/py_cel_type.h"
#include "cel_expr_python/py_descriptor_database.h"
#include "cel_expr_python/py_message_factory.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/dynamic_message.h"
#include "google/protobuf/message.h"
namespace cel_python {
class PyCelEnvInternal;
class CelExtensionHandle {
public:
explicit CelExtensionHandle(PyObject* extension);
~CelExtensionHandle();
absl::StatusOr<CelExtension*> GetExtension(
const std::shared_ptr<PyCelEnvInternal>& env);
private:
// The Python object that was passed to the constructor and is retained for
// the duration of the CelExtensionHandle lifetime.
PyObject* py_extension_;
// Non-retaining pointer to the CelExtension encapsulated by `py_extension_`.
// It must be deleted before `py_extension_`.
CelExtension* cel_extension_;
};
// PyCelEnvInternal is a container for internal CEL components not exposed to
// the python side.
class PyCelEnvInternal {
public:
PyCelEnvInternal(PyObject* descriptor_pool,
std::unordered_map<std::string, PyCelType> variableTypes,
const std::vector<PyObject*>& extensions,
std::string container);
~PyCelEnvInternal() = default;
static absl::StatusOr<const cel::Compiler*> GetCompiler(
const std::shared_ptr<PyCelEnvInternal>& env);
enum RuntimeMode {
// Standard CEL runtime with warnings treated as errors.
kStandard,
// Standard CEL runtime with warnings ignored. Useful for CEL expressions
// that bypass type checking.
kStandardIgnoreWarnings,
};
static absl::StatusOr<const cel::Runtime*> GetRuntime(
const std::shared_ptr<PyCelEnvInternal>& env, RuntimeMode runtime_mode);
const google::protobuf::DescriptorPool* GetDescriptorPool() const {
return &descriptor_pool_;
}
google::protobuf::MessageFactory* GetMessageFactory() const {
return const_cast<google::protobuf::DynamicMessageFactory*>(&message_factory_);
}
std::shared_ptr<PyMessageFactory> GetPyMessageFactory() const {
return py_message_factory_;
}
const PyCelType& GetVariableType(const std::string& name) const;
private:
PyDescriptorDatabase py_descriptor_database_;
google::protobuf::DescriptorPool descriptor_pool_;
google::protobuf::DynamicMessageFactory message_factory_;
std::shared_ptr<PyMessageFactory> py_message_factory_;
// Synchronized by the GIL.
std::unordered_map<std::string, PyCelType> variable_types_;
std::vector<std::unique_ptr<CelExtensionHandle>> extensions_;
std::string container_;
std::unique_ptr<cel::Compiler> compiler_;
absl::flat_hash_map<RuntimeMode, std::unique_ptr<const cel::Runtime>>
runtimes_;
absl::Status ConfigureStandardExtension(
cel::CompilerBuilder& compiler_builder, const std::string& extension);
absl::Status ConfigureStandardExtension(cel::RuntimeBuilder& runtime_builder,
const std::string& extension,
const cel::RuntimeOptions& opts);
};
} // namespace cel_python
#endif // THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_INTERNAL_H_