lsst.pex.policy  13.0-1-g47a359c+18
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
policy.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2016 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #include "pybind11/pybind11.h"
24 #include "pybind11/stl.h"
25 
26 #include "lsst/pex/policy/Policy.h"
29 
30 #include "lsst/pex/exceptions/Exception.h"
31 #include "lsst/pex/exceptions/python/Exception.h"
33 
34 namespace py = pybind11;
35 using namespace pybind11::literals;
36 
37 namespace lsst {
38 namespace pex {
39 namespace policy {
40 
41 PYBIND11_PLUGIN(policy) {
42  py::module mod("policy");
43 
44  exceptions::python::declareException<BadNameError, exceptions::RuntimeError>(mod, "BadNameError",
45  "RuntimeError");
46  exceptions::python::declareException<DictionaryError, exceptions::DomainError>(mod, "DictionaryError",
47  "DomainError");
48  exceptions::python::declareException<NameNotFound, exceptions::NotFoundError>(mod, "NameNotFound",
49  "NotFoundError");
50  exceptions::python::declareException<TypeError, exceptions::DomainError>(mod, "TypeError", "DomainError");
51  auto clsValidationError =
52  exceptions::python::declareException<ValidationError>(mod, "ValidationError", "LogicError");
53 
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)
70  .export_values();
71 
72  clsValidationError.def(py::init<const std::string&>());
73  clsValidationError.def(py::init<char const*, int, char const*>());
74 
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);
85 
86  py::class_<Policy, std::shared_ptr<Policy>> clsPolicy(mod, "Policy");
87 
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)
97  .export_values();
98 
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&>());
104 
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",
112  (Policy * (*)(PolicySource&, const std::string&, bool)) & Policy::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",
118  (Policy::ValueType (Policy::*)(const std::string&) const) & Policy::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);
131  clsPolicy.def("names", (Policy::StringArray (Policy::*)(bool) const) & Policy::names,
132  "topLevelOnly"_a = false);
133  clsPolicy.def("paramNames", (Policy::StringArray (Policy::*)(bool) const) & Policy::paramNames,
134  "topLevelOnly"_a = false);
135  clsPolicy.def("policyNames", (Policy::StringArray (Policy::*)(bool) const) & Policy::policyNames,
136  "topLevelOnly"_a = false);
137  clsPolicy.def("fileNames", (Policy::StringArray (Policy::*)(bool) const) & Policy::fileNames,
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);
143  // Somehow default arguments don't work here
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);
168  // _set is called from Python set
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);
181 
182  // boost::filesystem::path is equivalent to str in Python
183  clsPolicy.def("loadPolicyFiles",
184  [](Policy& self, const std::string& path, bool strict = true) -> int {
185  return self.loadPolicyFiles(boost::filesystem::path(path), strict);
186  },
187  "repository"_a, "strict"_a = true);
188 
189  clsPolicy.def("mergeDefaults", &Policy::mergeDefaults, "defaultPol"_a, "keepForValidation"_a = true,
190  "errs"_a = nullptr);
191  clsPolicy.def("str", &Policy::str, "name"_a, "indent"_a = "");
192  clsPolicy.def("toString", &Policy::toString);
193  clsPolicy.def("__str__", &Policy::toString); // Cleanup stringification later
194  clsPolicy.def("asPropertySet", &Policy::asPropertySet);
195 
196  return mod.ptr();
197 }
198 
199 } // policy
200 } // pex
201 } // lsst
std::shared_ptr< Policy > Ptr
Definition: Policy.h:172
definition of the PolicySource class
definition of the PolicyFile class
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
an abstract class representing a source of serialized Policy parameter data.
Definition: PolicySource.h:53
std::vector< std::string > StringArray
Definition: Policy.h:181
definition of the Dictionary class
PYBIND11_PLUGIN(defaultPolicyFile)
ValueType
an enumeration for the supported policy types
Definition: Policy.h:189