35 #include <boost/filesystem/path.hpp>
57 const char*
const Policy::typeName[] = {
"undefined",
"bool",
"int",
"double",
58 "string",
"Policy",
"PolicyFile"};
63 Policy::Policy() : Persistable(), _data(new PropertySet()) {}
68 Policy::Policy(
const PolicySource& source) : Persistable(), _data(new PropertySet()) { source.load(*
this); }
73 Policy::Policy(
const string& pathOrUrn) : Persistable(), _data(new PropertySet()) {
74 createPolicyFile(pathOrUrn,
true)->load(*
this);
80 Policy::Policy(
const char* pathOrUrn) : Persistable(), _data(new PropertySet()) {
81 createPolicyFile(pathOrUrn,
true)->load(*
this);
101 void extractDefaults(Policy& target,
const Dictionary& dict, ValidationError& ve) {
103 dict.definedNames(names);
106 const string& name = *it;
108 def->setDefaultIn(target, &ve);
110 if (def->getType() ==
Policy::POLICY && dict.hasSubDictionary(name)) {
112 extractDefaults(*subp, *dict.getSubDictionary(name), ve);
113 if (subp->nameCount() > 0) target.add(name, subp);
131 Policy::Policy(
bool validate,
const Dictionary& dict,
const fs::path& repository)
132 : Persistable(), _data(new PropertySet()) {
136 loadedDict = _dictionary;
138 loadedDict.reset(
new Dictionary(dict));
140 loadedDict->loadPolicyFiles(repository,
true);
142 ValidationError ve(LSST_EXCEPT_HERE);
143 extractDefaults(*
this, *loadedDict, ve);
144 if (ve.getParamCount() > 0)
throw ve;
150 Policy::Policy(
const Policy& pol) : Persistable(), _data() { _data = pol._data->deepCopy(); }
157 _data = pol._data->deepCopy();
162 Policy* Policy::_createPolicy(PolicySource& source,
bool doIncludes,
const fs::path& repository,
167 if (pol->isDictionary()) {
172 if (doIncludes) pol->loadPolicyFiles(repository,
true);
174 return pol.release();
177 Policy* Policy::_createPolicy(
const string& input,
bool doIncludes,
const fs::path& repository,
179 fs::path repos = repository;
181 fs::path filepath(input);
182 if (filepath.has_parent_path()) repos = filepath.parent_path();
184 PolicyFile file(input);
185 return _createPolicy(file, doIncludes, repos,
validate);
191 UrnPolicyFile upf(urn,
true,
true);
192 return _createPolicy(upf,
true, fs::path(),
false);
218 void Policy::setDictionary(
const Dictionary& dict) { _dictionary = std::make_shared<Dictionary>(dict); }
232 throw LSST_EXCEPT(DictionaryError,
"No dictionary set.");
234 _dictionary->validate(*
this, errs);
245 if (nameTypeMap.
size() == 0) {
248 for (
int i = 0; i < n; ++i) {
252 transform(lowered.begin(), lowered.end(), lowered.begin(), ::tolower);
263 if (tmp.
count(name) == 1)
return tmp[name];
264 }
else if (nameTypeMap.
count(name) == 1)
265 return nameTypeMap[name];
284 int Policy::_names(
vector<string>& names,
bool topLevelOnly,
bool append,
int want)
const {
285 bool shouldCheck =
true;
287 int have = 0,
count = 0;
289 src = _data->propertySetNames(topLevelOnly);
292 }
else if (want == 7)
293 src = _data->names(topLevelOnly);
295 src = _data->paramNames(topLevelOnly);
299 StringArray::iterator i;
300 for (i = src.
begin(); i != src.
end(); ++i) {
309 if ((have & want) > 0) {
332 int Policy::_names(
list<string>& names,
bool topLevelOnly,
bool append,
int want)
const {
333 bool shouldCheck =
true;
335 int have = 0,
count = 0;
337 src = _data->propertySetNames(topLevelOnly);
340 }
else if (want == 7)
341 src = _data->names(topLevelOnly);
343 src = _data->paramNames(topLevelOnly);
347 StringArray::iterator i;
348 for (i = src.
begin(); i != src.
end(); ++i) {
357 if ((have & want) > 0) {
367 void Policy::_validate(
const std::string& name,
const T& value,
int curCount) {
371 def->validateBasic(name, value, curCount);
372 }
catch (NameNotFound& e) {
373 ValidationError ve(LSST_EXCEPT_HERE);
374 ve.addError(name, ValidationError::UNKNOWN_NAME);
382 template void Policy::_validate<bool>(
std::string const&,
bool const&,
int);
383 template void Policy::_validate<double>(
std::string const&,
double const&,
int);
384 template void Policy::_validate<int>(
std::string const&,
int const&,
int);
403 if (tp ==
typeid(
bool)) {
405 }
else if (tp ==
typeid(
int)) {
407 }
else if (tp ==
typeid(
double)) {
409 }
else if (tp ==
typeid(
string)) {
415 string(
"Policy: illegal type held by PropertySet: ") + tp.
name());
423 bool Policy::getValue<bool>(
const string& name)
const {
427 int Policy::getValue<int>(
const string& name)
const {
431 double Policy::getValue<double>(
const string& name)
const {
435 string Policy::getValue<string>(
const string& name)
const {
439 Policy::FilePtr Policy::getValue<Policy::FilePtr>(
const string& name)
const {
440 return getFile(name);
443 Policy::ConstPtr Policy::getValue<Policy::ConstPtr>(
const string& name)
const {
444 return getPolicy(name);
448 vector<bool> Policy::getValueArray<bool>(
const string& name)
const {
452 vector<int> Policy::getValueArray<int>(
const string& name)
const {
456 vector<double> Policy::getValueArray<double>(
const string& name)
const {
460 vector<string> Policy::getValueArray<string>(
const string& name)
const {
465 return getFileArray(name);
469 return getPolicyArray(name);
473 return getConstPolicyArray(name);
576 FilePtr out = std::dynamic_pointer_cast<PolicyFile>(_data->getAsPersistablePtr(name));
586 for (i = pfa.
begin(); i != pfa.
end(); ++i) {
587 fp = std::dynamic_pointer_cast<PolicyFile>(*i);
595 void Policy::set(
const string& name,
const FilePtr& value) {
596 _data->set(name, std::dynamic_pointer_cast<Persistable>(value));
599 void Policy::add(
const string& name,
const FilePtr& value) {
600 _data->add(name, std::dynamic_pointer_cast<Persistable>(value));
620 fs::path repos = repository;
622 if (repos.empty()) repos =
".";
631 pols.reserve(pfiles.size());
633 FilePtrArray::const_iterator pfi;
634 for (pfi = pfiles.begin(); pfi != pfiles.end(); pfi++) {
638 Ptr policy = std::make_shared<Policy>();
640 fs::path path = (*pfi)->getPath();
642 if (path.is_complete()) {
643 (*pfi)->load(*policy);
645 fs::path localPath = repos / (*pfi)->getPath();
646 PolicyFile(localPath.string()).load(*policy);
653 }
catch (ParserError& e) {
661 pols.push_back(policy);
665 for (PolicyPtrArray::iterator pi = pols.begin(); pi != pols.end(); ++pi)
add(*it, *pi);
674 PolicyPtrArray::iterator pi;
675 for (pi = policies.begin(); pi != policies.end(); pi++)
676 result += (*pi)->loadPolicyFiles(repos, strict);
708 const Policy* def = &defaultPol;
709 if (def->isDictionary()) {
711 pol.reset(
new Policy(
false, Dictionary(*def)));
716 def->paramNames(params);
718 for (nm = params.
begin(); nm != params.
end(); ++nm) {
721 if (tp ==
typeid(
bool)) {
723 BoolArray::iterator vi;
724 for (vi = a.begin(); vi != a.end(); ++vi)
add(*nm, *vi);
725 }
else if (tp ==
typeid(
int)) {
727 IntArray::iterator vi;
728 for (vi = a.begin(); vi != a.end(); ++vi)
add(*nm, *vi);
729 }
else if (tp ==
typeid(
double)) {
731 DoubleArray::iterator vi;
732 for (vi = a.begin(); vi != a.end(); ++vi)
add(*nm, *vi);
733 }
else if (tp ==
typeid(
string)) {
735 StringArray::iterator vi;
736 for (vi = a.begin(); vi != a.end(); ++vi)
add(*nm, *vi);
737 }
else if (def->isFile(*nm)) {
739 FilePtrArray::iterator vi;
740 for (vi = a.begin(); vi != a.end(); ++vi)
add(*nm, *vi);
744 string(
"Unknown type for \"") + *nm +
"\": \"" +
getTypeName(*nm) +
"\"");
753 if (keepForValidation) {
754 if (defaultPol.isDictionary())
756 else if (defaultPol.canValidate())
766 else if (defaultPol.isDictionary())
767 Dictionary(defaultPol).validate(*
this, errs);
776 string Policy::str(
const string& name,
const string& indent)
const {
781 if (tp ==
typeid(
bool)) {
783 BoolArray::iterator vi;
784 for (vi = b.begin(); vi != b.end(); ++vi) {
786 if (vi + 1 != b.end()) out <<
", ";
788 }
else if (tp ==
typeid(
int)) {
790 IntArray::iterator vi;
791 for (vi = i.begin(); vi != i.end(); ++vi) {
793 if (vi + 1 != i.end()) out <<
", ";
795 }
else if (tp ==
typeid(
double)) {
797 DoubleArray::iterator vi;
798 for (vi = d.begin(); vi != d.end(); ++vi) {
800 if (vi + 1 != d.end()) out <<
", ";
802 }
else if (tp ==
typeid(
string)) {
804 StringArray::iterator vi;
805 for (vi = s.begin(); vi != s.end(); ++vi) {
806 out <<
'"' << *vi <<
'"';
807 if (vi + 1 != s.end()) out <<
", ";
812 for (vi = p.
begin(); vi != p.
end(); ++vi) {
814 Policy(*vi).print(out,
"", indent +
" ");
815 out << indent <<
"}";
816 if (vi + 1 != p.
end()) out <<
", ";
821 FilePtrArray::iterator vi;
822 for (vi = f.begin(); vi != f.end(); ++vi) {
823 out <<
"FILE:" << (*vi)->getPath();
824 if (vi + 1 != f.end()) out <<
", ";
830 }
catch (NameNotFound&) {
844 if (label.
size() > 0) out << indent << label <<
":\n";
846 out << indent <<
" " << *n <<
": " <<
str(*n, indent +
" ") <<
endl;