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()) {}
74 createPolicyFile(pathOrUrn,
true)->load(*
this);
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)
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;
157 _data = pol._data->deepCopy();
162 Policy* Policy::_createPolicy(
PolicySource& source,
bool doIncludes,
const fs::path& repository,
167 if (pol->isDictionary()) {
169 pol.reset(
new Policy(validate, d, repository));
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();
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) {
251 string lowered(Policy::typeName[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);
381 template void Policy::_validate<std::string>(std::string
const&, std::string
const&, int);
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 {
424 return getBool(name);
427 int Policy::getValue<int>(
const string& name)
const {
431 double Policy::getValue<double>(
const string& name)
const {
432 return getDouble(name);
435 string Policy::getValue<string>(
const string& name)
const {
436 return getString(name);
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 {
449 return getBoolArray(name);
452 vector<int> Policy::getValueArray<int>(
const string& name)
const {
453 return getIntArray(name);
456 vector<double> Policy::getValueArray<double>(
const string& name)
const {
457 return getDoubleArray(name);
460 vector<string> Policy::getValueArray<string>(
const string& name)
const {
461 return getStringArray(name);
465 return getFileArray(name);
469 return getPolicyArray(name);
473 return getConstPolicyArray(name);
560 ConstPolicyPtrArray out;
563 for (i = psa.
begin(); i != psa.
end(); ++i) out.
push_back(ConstPtr(
new Policy(*i)));
577 if (!out.get())
throw LSST_EXCEPT(TypeError, name,
string(typeName[
FILE]));
586 for (i = pfa.
begin(); i != pfa.
end(); ++i) {
588 if (!fp.get())
throw LSST_EXCEPT(TypeError, name,
string(typeName[
FILE]));
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 =
".";
626 fileNames(names,
true);
629 const FilePtrArray& pfiles = getFileArray(*it);
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();
653 }
catch (ParserError& e) {
661 pols.push_back(policy);
665 for (PolicyPtrArray::iterator pi = pols.begin(); pi != pols.end(); ++pi) add(*it, *pi);
669 policyNames(names,
true);
671 PolicyPtrArray policies = getPolicyArray(*it);
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)) {
722 BoolArray a = def->getBoolArray(*nm);
723 BoolArray::iterator vi;
724 for (vi = a.begin(); vi != a.end(); ++vi) add(*nm, *vi);
725 }
else if (tp ==
typeid(
int)) {
726 IntArray a = def->getIntArray(*nm);
727 IntArray::iterator vi;
728 for (vi = a.begin(); vi != a.end(); ++vi) add(*nm, *vi);
729 }
else if (tp ==
typeid(
double)) {
730 DoubleArray a = def->getDoubleArray(*nm);
731 DoubleArray::iterator vi;
732 for (vi = a.begin(); vi != a.end(); ++vi) add(*nm, *vi);
733 }
else if (tp ==
typeid(
string)) {
734 StringArray a = def->getStringArray(*nm);
735 StringArray::iterator vi;
736 for (vi = a.begin(); vi != a.end(); ++vi) add(*nm, *vi);
737 }
else if (def->isFile(*nm)) {
738 FilePtrArray a = def->getFileArray(*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())
755 setDictionary(Dictionary(defaultPol));
756 else if (defaultPol.canValidate())
757 setDictionary(*defaultPol.getDictionary());
761 if (canValidate()) getDictionary()->validate(*
this, errs);
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)) {
782 BoolArray b = getBoolArray(name);
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)) {
789 IntArray i = getIntArray(name);
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)) {
796 DoubleArray d = getDoubleArray(name);
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)) {
803 StringArray s = _data->getArray<
string>(name);
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 <<
", ";
820 FilePtrArray f = getFileArray(name);
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;
static std::type_info const & typeOfT()
std::shared_ptr< PolicyFile > FilePtr
std::shared_ptr< Policy > Ptr
definition of the PolicySource class
void set(const std::string &name, const Ptr &value)
Set a value with the given name.
std::vector< Ptr > PolicyPtrArray
static Policy * createPolicyFromUrn(const std::string &urn, bool validate=true)
Create a Policy from a file specified by a URN.
definition of the PolicyFile class
const ConstDictPtr getDictionary() const
The dictionary (if any) that this policy uses to validate itself, including checking set() and add() ...
bool canValidate() const
Can this policy validate itself – that is, does it have a dictionary that it can use to validate its...
virtual void load(Policy &policy) const
load the data from this Policy source into a Policy object.
std::shared_ptr< const Dictionary > ConstDictPtr
definition of Policy parsing exceptions
FilePtrArray getFileArray(const std::string &name) const
return an array of PolicyFile pointers associated with the given name.
void setDictionary(const Dictionary &dict)
Update this policy's dictionary that it uses to validate itself.
PolicyPtrArray getPolicyArray(const std::string &name) const
return an array of Policy pointers associated with the given name.
void validate(ValidationError *errs=0) const
Validate this policy, using its stored dictionary.
static bool looksLikeUrn(const std::string &s, bool strict=false)
Does s look like a URN? That is, does it start with URN_PREFIX or URN_PREFIX_ABBREV?
static const char *const typeName[]
c-string forms for the supported value types.
FilePtr getFile(const std::string &name) const
return a PolicyFile (a reference to a file with "sub-Policy" data) identified by a given name...
static FilePtr createPolicyFile(const std::string &pathOrUrn, bool strict=false)
Create a PolicyFile or UrnPolicyFile from pathOrUrn.
bool exists() const
return true if the file exists.
PolicyFile(const std::string &filepath, const SupportedFormats::Ptr &fmts=defaultFormats)
create a Policy file that points a file with given path.
void addValue(const std::string &name, const T &value)
Add a value to an array of values with a given name.
T dynamic_pointer_cast(T... args)
void setValue(const std::string &name, const T &value)
Set a value with the given name.
int mergeDefaults(const Policy &defaultPol, bool keepForValidation=true, ValidationError *errs=0)
use the values found in the given policy as default values for parameters not specified in this polic...
#define LSST_EXCEPT(type,...)
std::vector< FilePtr > FilePtrArray
virtual void print(std::ostream &out, const std::string &label="Policy", const std::string &indent="") const
print the contents of this policy to an output stream.
static ValueType getTypeByName(const std::string &name)
Given the human-readable name of a type ("bool", "int", "policy", etc), what is its ValueType (BOOL...
definition of the Dictionary class
static ValueType getValueType()
A template-ized way to get the ValueType.
std::shared_ptr< Persistable > Ptr
virtual std::string str(const std::string &name, const std::string &indent="") const
return a string representation of the value given by a name.
int loadPolicyFiles(bool strict=true)
Recursively replace all PolicyFile values with the contents of the files they refer to...
PolicySource(SupportedFormats::Ptr fmts=defaultFormats)
create a Policy file that points a file with given path.
ConstPolicyPtrArray getConstPolicyArray(const std::string &name) const
return an array of Policy pointers associated with the given name.
std::string toString() const
convert the entire contents of this policy to a string.
void add(const std::string &name, const Ptr &value)
Add a value to an array of values with a given name.
std::shared_ptr< const Policy > ConstPtr
the definition of the UrnPolicyFile class
Policy()
Create an empty policy.
std::vector< ConstPtr > ConstPolicyPtrArray
ValueType
an enumeration for the supported policy types
virtual ~Policy()
destroy this policy