LSSTApplications  20.0.0
LSSTDataManagementBasePackage
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 
33 
34 namespace py = pybind11;
35 using namespace pybind11::literals;
36 
37 namespace lsst {
38 namespace pex {
39 namespace policy {
40 
41 PYBIND11_MODULE(policy, mod) {
42  exceptions::python::declareException<BadNameError, exceptions::RuntimeError>(mod, "BadNameError",
43  "RuntimeError");
44  exceptions::python::declareException<DictionaryError, exceptions::DomainError>(mod, "DictionaryError",
45  "DomainError");
46  exceptions::python::declareException<NameNotFound, exceptions::NotFoundError>(mod, "NameNotFound",
47  "NotFoundError");
48  exceptions::python::declareException<TypeError, exceptions::DomainError>(mod, "TypeError", "DomainError");
49  auto clsValidationError =
50  exceptions::python::declareException<ValidationError>(mod, "ValidationError", "LogicError");
51 
52  py::enum_<ValidationError::ErrorType>(clsValidationError, "ErrorType")
53  .value("OK", ValidationError::ErrorType::OK)
54  .value("WRONG_TYPE", ValidationError::ErrorType::WRONG_TYPE)
55  .value("MISSING_REQUIRED", ValidationError::ErrorType::MISSING_REQUIRED)
56  .value("NOT_AN_ARRAY", ValidationError::ErrorType::NOT_AN_ARRAY)
57  .value("ARRAY_TOO_SHORT", ValidationError::ErrorType::ARRAY_TOO_SHORT)
58  .value("TOO_FEW_VALUES", ValidationError::ErrorType::TOO_FEW_VALUES)
59  .value("TOO_MANY_VALUES", ValidationError::ErrorType::TOO_MANY_VALUES)
60  .value("WRONG_OCCURRENCE_COUNT", ValidationError::ErrorType::WRONG_OCCURRENCE_COUNT)
61  .value("VALUE_DISALLOWED", ValidationError::ErrorType::VALUE_DISALLOWED)
62  .value("VALUE_OUT_OF_RANGE", ValidationError::ErrorType::VALUE_OUT_OF_RANGE)
63  .value("BAD_VALUE", ValidationError::ErrorType::BAD_VALUE)
64  .value("UNKNOWN_NAME", ValidationError::ErrorType::UNKNOWN_NAME)
65  .value("BAD_DEFINITION", ValidationError::ErrorType::BAD_DEFINITION)
66  .value("NOT_LOADED", ValidationError::ErrorType::NOT_LOADED)
67  .value("UNKNOWN_ERROR", ValidationError::ErrorType::UNKNOWN_ERROR)
68  .export_values();
69 
70  clsValidationError.def(py::init<const std::string&>());
71  clsValidationError.def(py::init<char const*, int, char const*>());
72 
73  clsValidationError.def_readonly_static("EMPTY", &ValidationError::EMPTY);
74  clsValidationError.def("getErrorMessageFor", &ValidationError::getErrorMessageFor);
75  clsValidationError.def("getParamCount", &ValidationError::getParamCount);
76  clsValidationError.def("paramNames", &ValidationError::paramNames);
77  clsValidationError.def("getParamNames", &ValidationError::getParamNames);
78  clsValidationError.def("getErrors",
79  (int (ValidationError::*)(const std::string&) const) & ValidationError::getErrors);
80  clsValidationError.def("getErrors", (int (ValidationError::*)() const) & ValidationError::getErrors);
81  clsValidationError.def("describe", &ValidationError::describe);
82  clsValidationError.def("what", &ValidationError::what);
83 
84  py::class_<Policy, std::shared_ptr<Policy>> clsPolicy(mod, "Policy");
85 
86  py::enum_<Policy::ValueType>(clsPolicy, "ValueType")
87  .value("UNDETERMINED", Policy::ValueType::UNDETERMINED)
88  .value("UNDEF", Policy::ValueType::UNDEF)
89  .value("BOOL", Policy::ValueType::BOOL)
90  .value("INT", Policy::ValueType::INT)
91  .value("DOUBLE", Policy::ValueType::DOUBLE)
92  .value("STRING", Policy::ValueType::STRING)
93  .value("POLICY", Policy::ValueType::POLICY)
94  .value("FILE", Policy::ValueType::FILE)
95  .export_values();
96 
97  clsPolicy.def(py::init<>());
98  clsPolicy.def(py::init<bool, const Dictionary&>());
99  clsPolicy.def(py::init<const std::string&>());
100  clsPolicy.def(py::init<Policy&, bool>(), "pol"_a, "deep"_a = false);
101  clsPolicy.def(py::init<const PolicySource&>());
102 
103  clsPolicy.def_static("createPolicyFromUrn", &Policy::createPolicyFromUrn, "urn"_a, "validate"_a = true);
104  clsPolicy.def_static("createPolicy", (Policy * (*)(PolicySource&, bool, bool)) & Policy::createPolicy,
105  "input"_a, "doIncludes"_a = true, "validate"_a = true);
106  clsPolicy.def_static("createPolicy",
107  (Policy * (*)(const std::string&, bool, bool)) & Policy::createPolicy, "input"_a,
108  "doIncludes"_a = true, "validate"_a = true);
109  clsPolicy.def_static("createPolicy",
110  (Policy * (*)(PolicySource&, const std::string&, bool)) & Policy::createPolicy,
111  "input"_a, "repos"_a, "validate"_a = true);
112  clsPolicy.def_static("createPolicy",
113  (Policy * (*)(const std::string&, const std::string&, bool)) & Policy::createPolicy,
114  "input"_a, "repos"_a, "validate"_a = true);
115  clsPolicy.def("getValueType",
116  (Policy::ValueType (Policy::*)(const std::string&) const) & Policy::getValueType);
117  clsPolicy.def("nameCount", &Policy::nameCount);
118  clsPolicy.def("names", (int (Policy::*)(std::list<std::string>&, bool, bool) const) & Policy::names,
119  "names"_a, "topLevelOnly"_a = false, "append"_a = false);
120  clsPolicy.def("paramNames",
121  (int (Policy::*)(std::list<std::string>&, bool, bool) const) & Policy::paramNames,
122  "names"_a, "topLevelOnly"_a = false, "append"_a = false);
123  clsPolicy.def("policyNames",
124  (int (Policy::*)(std::list<std::string>&, bool, bool) const) & Policy::policyNames,
125  "names"_a, "topLevelOnly"_a = false, "append"_a = false);
126  clsPolicy.def("fileNames",
127  (int (Policy::*)(std::list<std::string>&, bool, bool) const) & Policy::fileNames, "names"_a,
128  "topLevelOnly"_a = false, "append"_a = false);
129  clsPolicy.def("names", (Policy::StringArray (Policy::*)(bool) const) & Policy::names,
130  "topLevelOnly"_a = false);
131  clsPolicy.def("paramNames", (Policy::StringArray (Policy::*)(bool) const) & Policy::paramNames,
132  "topLevelOnly"_a = false);
133  clsPolicy.def("policyNames", (Policy::StringArray (Policy::*)(bool) const) & Policy::policyNames,
134  "topLevelOnly"_a = false);
135  clsPolicy.def("fileNames", (Policy::StringArray (Policy::*)(bool) const) & Policy::fileNames,
136  "topLevelOnly"_a = false);
137  clsPolicy.def("isDictionary", &Policy::isDictionary);
138  clsPolicy.def("canValidate", &Policy::canValidate);
139  clsPolicy.def("getDictionary", &Policy::getDictionary);
140  clsPolicy.def("setDictionary", &Policy::setDictionary);
141  // Somehow default arguments don't work here
142  clsPolicy.def("validate", [](Policy const& self) { return self.validate(); });
143  clsPolicy.def("validate", [](Policy const& self, ValidationError* errs) { return self.validate(errs); });
144  clsPolicy.def("valueCount", &Policy::valueCount);
145  clsPolicy.def("isArray", &Policy::isArray);
146  clsPolicy.def("exists", &Policy::exists);
147  clsPolicy.def("isBool", &Policy::isBool);
148  clsPolicy.def("isInt", &Policy::isInt);
149  clsPolicy.def("isDouble", &Policy::isDouble);
150  clsPolicy.def("isString", &Policy::isString);
151  clsPolicy.def("isPolicy", &Policy::isPolicy);
152  clsPolicy.def("isFile", &Policy::isFile);
153  clsPolicy.def("getTypeInfo", &Policy::getTypeInfo);
154  clsPolicy.def("getPolicy", (Policy::Ptr (Policy::*)(const std::string&)) & Policy::getPolicy);
155  clsPolicy.def("getFile", &Policy::getFile);
156  clsPolicy.def("getBool", &Policy::getBool);
157  clsPolicy.def("getInt", &Policy::getInt);
158  clsPolicy.def("getDouble", &Policy::getDouble);
159  clsPolicy.def("getString", &Policy::getString);
160  clsPolicy.def("getPolicyArray", &Policy::getPolicyArray);
161  clsPolicy.def("getFileArray", &Policy::getFileArray);
162  clsPolicy.def("getBoolArray", &Policy::getBoolArray);
163  clsPolicy.def("getIntArray", &Policy::getIntArray);
164  clsPolicy.def("getDoubleArray", &Policy::getDoubleArray);
165  clsPolicy.def("getStringArray", &Policy::getStringArray);
166  // _set is called from Python set
167  clsPolicy.def("_set", (void (Policy::*)(const std::string&, const Policy::Ptr&)) & Policy::set);
168  clsPolicy.def("_set", (void (Policy::*)(const std::string&, bool)) & Policy::set);
169  clsPolicy.def("_set", (void (Policy::*)(const std::string&, int)) & Policy::set);
170  clsPolicy.def("_set", (void (Policy::*)(const std::string&, double)) & Policy::set);
171  clsPolicy.def("_set", (void (Policy::*)(const std::string&, const std::string&)) & Policy::set);
172  clsPolicy.def("add", (void (Policy::*)(const std::string&, const Policy::Ptr&)) & Policy::add);
173  clsPolicy.def("add", (void (Policy::*)(const std::string&, bool)) & Policy::add);
174  clsPolicy.def("add", (void (Policy::*)(const std::string&, int)) & Policy::add);
175  clsPolicy.def("add", (void (Policy::*)(const std::string&, double)) & Policy::add);
176  clsPolicy.def("add", (void (Policy::*)(const std::string&, const std::string&)) & Policy::add);
177  clsPolicy.def("remove", &Policy::remove);
178  clsPolicy.def("loadPolicyFiles", (int (Policy::*)(bool)) & Policy::loadPolicyFiles, "strict"_a = true);
179 
180  // boost::filesystem::path is equivalent to str in Python
181  clsPolicy.def("loadPolicyFiles",
182  [](Policy& self, const std::string& path, bool strict = true) -> int {
183  return self.loadPolicyFiles(boost::filesystem::path(path), strict);
184  },
185  "repository"_a, "strict"_a = true);
186 
187  clsPolicy.def("mergeDefaults", &Policy::mergeDefaults, "defaultPol"_a, "keepForValidation"_a = true,
188  "errs"_a = nullptr);
189  clsPolicy.def("str", &Policy::str, "name"_a, "indent"_a = "");
190  clsPolicy.def("toString", &Policy::toString);
191  clsPolicy.def("__str__", &Policy::toString); // Cleanup stringification later
192  clsPolicy.def("asPropertySet", &Policy::asPropertySet);
193 }
194 
195 } // policy
196 } // pex
197 } // lsst
std::string
STL class.
std::shared_ptr
STL class.
std::list< std::string >
lsst::pex::policy::Policy::ValueType
ValueType
an enumeration for the supported policy types
Definition: Policy.h:186
Exception.h
std::vector< std::string >
lsst::pex::policy::Policy
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:167
lsst::meas::modelfit.display.densityPlot.mergeDefaults
def mergeDefaults(kwds, defaults)
Definition: densityPlot.py:56
pex
Definition: __init__.py:1
Policy.h
lsst::pex::policy::PolicySource
an abstract class representing a source of serialized Policy parameter data.
Definition: PolicySource.h:52
PolicyFile.h
definition of the PolicyFile class
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
Dictionary.h
definition of the Dictionary class
lsst::pex::policy::PYBIND11_MODULE
PYBIND11_MODULE(policy, mod)
Definition: policy.cc:41
PolicySource.h
definition of the PolicySource class
pybind11
Definition: _GenericMap.cc:40
Exception.h
set
daf::base::PropertySet * set
Definition: fits.cc:912