23 #include "pybind11/pybind11.h"
24 #include "pybind11/stl.h"
30 #include "lsst/pex/exceptions/Exception.h"
31 #include "lsst/pex/exceptions/python/Exception.h"
34 namespace py = pybind11;
35 using namespace pybind11::literals;
42 py::module mod(
"policy");
44 exceptions::python::declareException<BadNameError, exceptions::RuntimeError>(mod,
"BadNameError",
46 exceptions::python::declareException<DictionaryError, exceptions::DomainError>(mod,
"DictionaryError",
48 exceptions::python::declareException<NameNotFound, exceptions::NotFoundError>(mod,
"NameNotFound",
50 exceptions::python::declareException<TypeError, exceptions::DomainError>(mod,
"TypeError",
"DomainError");
51 auto clsValidationError =
52 exceptions::python::declareException<ValidationError>(mod,
"ValidationError",
"LogicError");
54 py::enum_<ValidationError::ErrorType>(clsValidationError,
"ErrorType")
55 .value(
"OK", ValidationError::ErrorType::OK)
56 .value(
"WRONG_TYPE", ValidationError::ErrorType::WRONG_TYPE)
57 .value(
"MISSING_REQUIRED", ValidationError::ErrorType::MISSING_REQUIRED)
58 .value(
"NOT_AN_ARRAY", ValidationError::ErrorType::NOT_AN_ARRAY)
59 .value(
"ARRAY_TOO_SHORT", ValidationError::ErrorType::ARRAY_TOO_SHORT)
60 .value(
"TOO_FEW_VALUES", ValidationError::ErrorType::TOO_FEW_VALUES)
61 .value(
"TOO_MANY_VALUES", ValidationError::ErrorType::TOO_MANY_VALUES)
62 .value(
"WRONG_OCCURRENCE_COUNT", ValidationError::ErrorType::WRONG_OCCURRENCE_COUNT)
63 .value(
"VALUE_DISALLOWED", ValidationError::ErrorType::VALUE_DISALLOWED)
64 .value(
"VALUE_OUT_OF_RANGE", ValidationError::ErrorType::VALUE_OUT_OF_RANGE)
65 .value(
"BAD_VALUE", ValidationError::ErrorType::BAD_VALUE)
66 .value(
"UNKNOWN_NAME", ValidationError::ErrorType::UNKNOWN_NAME)
67 .value(
"BAD_DEFINITION", ValidationError::ErrorType::BAD_DEFINITION)
68 .value(
"NOT_LOADED", ValidationError::ErrorType::NOT_LOADED)
69 .value(
"UNKNOWN_ERROR", ValidationError::ErrorType::UNKNOWN_ERROR)
72 clsValidationError.def(py::init<const std::string&>());
73 clsValidationError.def(py::init<char const*, int, char const*>());
75 clsValidationError.def_readonly_static(
"EMPTY", &ValidationError::EMPTY);
76 clsValidationError.def(
"getErrorMessageFor", &ValidationError::getErrorMessageFor);
77 clsValidationError.def(
"getParamCount", &ValidationError::getParamCount);
78 clsValidationError.def(
"paramNames", &ValidationError::paramNames);
79 clsValidationError.def(
"getParamNames", &ValidationError::getParamNames);
80 clsValidationError.def(
"getErrors",
81 (
int (ValidationError::*)(
const std::string&)
const) & ValidationError::getErrors);
82 clsValidationError.def(
"getErrors", (
int (ValidationError::*)()
const) & ValidationError::getErrors);
83 clsValidationError.def(
"describe", &ValidationError::describe);
84 clsValidationError.def(
"what", &ValidationError::what);
86 py::class_<Policy, std::shared_ptr<Policy>> clsPolicy(mod,
"Policy");
88 py::enum_<Policy::ValueType>(clsPolicy,
"ValueType")
89 .value(
"UNDETERMINED", Policy::ValueType::UNDETERMINED)
90 .value(
"UNDEF", Policy::ValueType::UNDEF)
91 .value(
"BOOL", Policy::ValueType::BOOL)
92 .value(
"INT", Policy::ValueType::INT)
93 .value(
"DOUBLE", Policy::ValueType::DOUBLE)
94 .value(
"STRING", Policy::ValueType::STRING)
95 .value(
"POLICY", Policy::ValueType::POLICY)
96 .value(
"FILE", Policy::ValueType::FILE)
99 clsPolicy.def(py::init<>());
100 clsPolicy.def(py::init<bool, const Dictionary&>());
101 clsPolicy.def(py::init<const std::string&>());
102 clsPolicy.def(py::init<Policy&, bool>(),
"pol"_a,
"deep"_a =
false);
103 clsPolicy.def(py::init<const PolicySource&>());
105 clsPolicy.def_static(
"createPolicyFromUrn", &Policy::createPolicyFromUrn,
"urn"_a,
"validate"_a =
true);
106 clsPolicy.def_static(
"createPolicy", (
Policy * (*)(
PolicySource&,
bool,
bool)) & Policy::createPolicy,
107 "input"_a,
"doIncludes"_a =
true,
"validate"_a =
true);
108 clsPolicy.def_static(
"createPolicy",
109 (
Policy * (*)(
const std::string&,
bool,
bool)) & Policy::createPolicy,
"input"_a,
110 "doIncludes"_a =
true,
"validate"_a =
true);
111 clsPolicy.def_static(
"createPolicy",
113 "input"_a,
"repos"_a,
"validate"_a =
true);
114 clsPolicy.def_static(
"createPolicy",
115 (
Policy * (*)(
const std::string&,
const std::string&,
bool)) & Policy::createPolicy,
116 "input"_a,
"repos"_a,
"validate"_a =
true);
117 clsPolicy.def(
"getValueType",
119 clsPolicy.def(
"nameCount", &Policy::nameCount);
120 clsPolicy.def(
"names", (
int (
Policy::*)(std::list<std::string>&,
bool,
bool)
const) & Policy::names,
121 "names"_a,
"topLevelOnly"_a =
false,
"append"_a =
false);
122 clsPolicy.def(
"paramNames",
123 (
int (
Policy::*)(std::list<std::string>&,
bool,
bool)
const) & Policy::paramNames,
124 "names"_a,
"topLevelOnly"_a =
false,
"append"_a =
false);
125 clsPolicy.def(
"policyNames",
126 (
int (
Policy::*)(std::list<std::string>&,
bool,
bool)
const) & Policy::policyNames,
127 "names"_a,
"topLevelOnly"_a =
false,
"append"_a =
false);
128 clsPolicy.def(
"fileNames",
129 (
int (
Policy::*)(std::list<std::string>&,
bool,
bool)
const) & Policy::fileNames,
"names"_a,
130 "topLevelOnly"_a =
false,
"append"_a =
false);
132 "topLevelOnly"_a =
false);
134 "topLevelOnly"_a =
false);
136 "topLevelOnly"_a =
false);
138 "topLevelOnly"_a =
false);
139 clsPolicy.def(
"isDictionary", &Policy::isDictionary);
140 clsPolicy.def(
"canValidate", &Policy::canValidate);
141 clsPolicy.def(
"getDictionary", &Policy::getDictionary);
142 clsPolicy.def(
"setDictionary", &Policy::setDictionary);
144 clsPolicy.def(
"validate", [](
Policy const&
self) {
return self.validate(); });
145 clsPolicy.def(
"validate", [](
Policy const&
self, ValidationError* errs) {
return self.validate(errs); });
146 clsPolicy.def(
"valueCount", &Policy::valueCount);
147 clsPolicy.def(
"isArray", &Policy::isArray);
148 clsPolicy.def(
"exists", &Policy::exists);
149 clsPolicy.def(
"isBool", &Policy::isBool);
150 clsPolicy.def(
"isInt", &Policy::isInt);
151 clsPolicy.def(
"isDouble", &Policy::isDouble);
152 clsPolicy.def(
"isString", &Policy::isString);
153 clsPolicy.def(
"isPolicy", &Policy::isPolicy);
154 clsPolicy.def(
"isFile", &Policy::isFile);
155 clsPolicy.def(
"getTypeInfo", &Policy::getTypeInfo);
156 clsPolicy.def(
"getPolicy", (
Policy::Ptr (
Policy::*)(
const std::string&)) & Policy::getPolicy);
157 clsPolicy.def(
"getFile", &Policy::getFile);
158 clsPolicy.def(
"getBool", &Policy::getBool);
159 clsPolicy.def(
"getInt", &Policy::getInt);
160 clsPolicy.def(
"getDouble", &Policy::getDouble);
161 clsPolicy.def(
"getString", &Policy::getString);
162 clsPolicy.def(
"getPolicyArray", &Policy::getPolicyArray);
163 clsPolicy.def(
"getFileArray", &Policy::getFileArray);
164 clsPolicy.def(
"getBoolArray", &Policy::getBoolArray);
165 clsPolicy.def(
"getIntArray", &Policy::getIntArray);
166 clsPolicy.def(
"getDoubleArray", &Policy::getDoubleArray);
167 clsPolicy.def(
"getStringArray", &Policy::getStringArray);
169 clsPolicy.def(
"_set", (
void (
Policy::*)(
const std::string&,
const Policy::Ptr&)) & Policy::set);
170 clsPolicy.def(
"_set", (
void (
Policy::*)(
const std::string&,
bool)) & Policy::set);
171 clsPolicy.def(
"_set", (
void (
Policy::*)(
const std::string&,
int)) & Policy::set);
172 clsPolicy.def(
"_set", (
void (
Policy::*)(
const std::string&,
double)) & Policy::set);
173 clsPolicy.def(
"_set", (
void (
Policy::*)(
const std::string&,
const std::string&)) & Policy::set);
174 clsPolicy.def(
"add", (
void (
Policy::*)(
const std::string&,
const Policy::Ptr&)) & Policy::add);
175 clsPolicy.def(
"add", (
void (
Policy::*)(
const std::string&,
bool)) & Policy::add);
176 clsPolicy.def(
"add", (
void (
Policy::*)(
const std::string&,
int)) & Policy::add);
177 clsPolicy.def(
"add", (
void (
Policy::*)(
const std::string&,
double)) & Policy::add);
178 clsPolicy.def(
"add", (
void (
Policy::*)(
const std::string&,
const std::string&)) & Policy::add);
179 clsPolicy.def(
"remove", &Policy::remove);
180 clsPolicy.def(
"loadPolicyFiles", (
int (
Policy::*)(
bool)) & Policy::loadPolicyFiles,
"strict"_a =
true);
183 clsPolicy.def(
"loadPolicyFiles",
184 [](
Policy&
self,
const std::string& path,
bool strict =
true) ->
int {
185 return self.loadPolicyFiles(boost::filesystem::path(path), strict);
187 "repository"_a,
"strict"_a =
true);
189 clsPolicy.def(
"mergeDefaults", &Policy::mergeDefaults,
"defaultPol"_a,
"keepForValidation"_a =
true,
191 clsPolicy.def(
"str", &Policy::str,
"name"_a,
"indent"_a =
"");
192 clsPolicy.def(
"toString", &Policy::toString);
193 clsPolicy.def(
"__str__", &Policy::toString);
194 clsPolicy.def(
"asPropertySet", &Policy::asPropertySet);
std::shared_ptr< Policy > Ptr
definition of the PolicySource class
definition of the PolicyFile class
a container for holding hierarchical configuration data in memory.
an abstract class representing a source of serialized Policy parameter data.
std::vector< std::string > StringArray
definition of the Dictionary class
PYBIND11_PLUGIN(defaultPolicyFile)
ValueType
an enumeration for the supported policy types