35 #include <boost/filesystem/path.hpp> 57 const char *
const Policy::typeName[] = {
71 : Citizen(typeid(this)), Persistable(), _data(new PropertySet())
89 createPolicyFile(pathOrUrn,
true)->load(*
this);
98 createPolicyFile(pathOrUrn,
true)->load(*
this);
118 void extractDefaults(Policy& target,
const Dictionary& dict, ValidationError& ve) {
120 dict.definedNames(names);
123 const string& name = *it;
125 def->setDefaultIn(target, &ve);
127 if (def->getType() ==
Policy::POLICY && dict.hasSubDictionary(name)) {
129 extractDefaults(*subp, *dict.getSubDictionary(name), ve);
130 if (subp->nameCount() > 0)
131 target.add(name, subp);
150 const fs::path& repository)
156 loadedDict = _dictionary;
159 loadedDict.reset(
new Dictionary(dict));
161 loadedDict->loadPolicyFiles(repository,
true);
163 ValidationError ve(LSST_EXCEPT_HERE);
164 extractDefaults(*
this, *loadedDict, ve);
165 if (ve.getParamCount() > 0)
throw ve;
174 _data = pol._data->deepCopy();
184 _data = pol._data->deepCopy();
189 Policy* Policy::_createPolicy(
PolicySource& source,
bool doIncludes,
190 const fs::path& repository,
bool validate)
195 if (pol->isDictionary()) {
197 pol.reset(
new Policy(validate, d, repository));
200 if (doIncludes) pol->loadPolicyFiles(repository,
true);
202 return pol.release();
205 Policy* Policy::_createPolicy(
const string& input,
bool doIncludes,
206 const fs::path& repository,
bool validate)
208 fs::path repos = repository;
210 fs::path filepath(input);
211 if (filepath.has_parent_path()) repos = filepath.parent_path();
214 return _createPolicy(file, doIncludes, repos, validate);
221 UrnPolicyFile upf(urn,
true,
true);
222 return _createPolicy(upf,
true, fs::path(),
false);
236 return static_cast<bool>(_dictionary);
253 _dictionary = std::make_shared<Dictionary>(dict);
267 if (!_dictionary)
throw LSST_EXCEPT(DictionaryError,
"No dictionary set.");
268 else _dictionary->validate(*
this, errs);
279 if (nameTypeMap.
size() == 0) {
282 for (
int i = 0; i < n; ++i) {
285 string lowered(Policy::typeName[i]);
286 transform(lowered.begin(), lowered.end(), lowered.begin(), ::tolower);
297 if (tmp.
count(name) == 1)
return tmp[name];
300 if (nameTypeMap.
count(name) == 1)
return nameTypeMap[name];
320 bool topLevelOnly,
bool append,
int want)
const 322 bool shouldCheck =
true;
324 int have = 0,
count = 0;
326 src = _data->propertySetNames(topLevelOnly);
331 src = _data->names(topLevelOnly);
333 src = _data->paramNames(topLevelOnly);
337 StringArray::iterator i;
338 for(i = src.
begin(); i != src.
end(); ++i) {
347 if ((have&want) > 0) {
371 bool topLevelOnly,
bool append,
int want)
const 373 bool shouldCheck =
true;
375 int have = 0,
count = 0;
377 src = _data->propertySetNames(topLevelOnly);
382 src = _data->names(topLevelOnly);
384 src = _data->paramNames(topLevelOnly);
388 StringArray::iterator i;
389 for(i = src.
begin(); i != src.
end(); ++i) {
398 if ((have&want) > 0) {
407 template <
class T>
void Policy::_validate(
const std::string& name,
const T& value,
int curCount) {
411 def->validateBasic(name, value, curCount);
412 }
catch(NameNotFound& e) {
413 ValidationError ve(LSST_EXCEPT_HERE);
414 ve.addError(name, ValidationError::UNKNOWN_NAME);
421 template void Policy::_validate<std::string >(std::string
const&, std::string
const&, int);
422 template void Policy::_validate<bool>(std::string
const&,
bool const&, int);
423 template void Policy::_validate<double>(std::string
const&,
double const&, int);
424 template void Policy::_validate<int>(std::string
const&,
int const&, int);
442 if (tp ==
typeid(
bool)) {
445 else if(tp ==
typeid(
int)) {
448 else if (tp ==
typeid(
double)) {
451 else if (tp ==
typeid(
string)) {
460 string(
"Policy: illegal type held by PropertySet: ") + tp.
name());
467 template <>
bool Policy::getValue <bool> (
const string& name)
const {
468 return getBool(name);
470 template <>
int Policy::getValue <int> (
const string& name)
const {
473 template <>
double Policy::getValue <double> (
const string& name)
const {
474 return getDouble(name);
476 template <>
string Policy::getValue <string> (
const string& name)
const {
477 return getString(name);
480 Policy::FilePtr Policy::getValue <Policy::FilePtr> (
const string& name)
const {
481 return getFile(name);
484 Policy::ConstPtr Policy::getValue <Policy::ConstPtr> (
const string& name)
const {
485 return getPolicy(name);
488 template <>
vector<bool> Policy::getValueArray<bool>(
const string& name)
const {
489 return getBoolArray(name);
491 template <>
vector<int> Policy::getValueArray<int>(
const string& name)
const {
492 return getIntArray(name);
494 template <>
vector<double> Policy::getValueArray<double>(
const string& name)
const {
495 return getDoubleArray(name);
497 template <>
vector<string> Policy::getValueArray<string>(
const string& name)
const {
498 return getStringArray(name);
500 template <>
Policy::FilePtrArray Policy::getValueArray<Policy::FilePtr>(
const string& name)
const {
501 return getFileArray(name);
504 return getPolicyArray(name);
507 return getConstPolicyArray(name);
516 template <>
Policy::ValueType Policy::getValueType<Policy::Ptr>() {
return POLICY; }
517 template <>
Policy::ValueType Policy::getValueType<Policy::ConstPtr>() {
return POLICY; }
523 template <>
void Policy::setValue(
const string& name,
const double& value) {
525 template <>
void Policy::setValue(
const string& name,
const string& value) {
529 template <>
void Policy::setValue(
const string& name,
const FilePtr& value) {
536 template <>
void Policy::addValue(
const string& name,
const double& value) {
538 template <>
void Policy::addValue(
const string& name,
const string& value) {
542 template <>
void Policy::addValue(
const string& name,
const FilePtr& value) {
546 ConstPolicyPtrArray out;
549 for(i=psa.
begin(); i != psa.
end(); ++i)
558 for(i=psa.
begin(); i != psa.
end(); ++i)
577 for(i = pfa.
begin(); i != pfa.
end(); ++i) {
587 void Policy::set(
const string& name,
const FilePtr& value) {
588 _data->set(name, std::dynamic_pointer_cast<Persistable>(value));
591 void Policy::add(
const string& name,
const FilePtr& value) {
592 _data->add(name, std::dynamic_pointer_cast<Persistable>(value));
612 fs::path repos = repository;
614 if (repos.empty()) repos =
".";
618 fileNames(names,
true);
622 const FilePtrArray& pfiles = getFileArray(*it);
624 pols.reserve(pfiles.size());
626 FilePtrArray::const_iterator pfi;
627 for(pfi=pfiles.begin(); pfi != pfiles.end(); pfi++) {
631 Ptr policy = std::make_shared<Policy>();
633 fs::path path = (*pfi)->getPath();
635 if (path.is_complete()) {
636 (*pfi)->load(*policy);
639 fs::path localPath = repos / (*pfi)->getPath();
649 catch (ParserError& e) {
657 pols.push_back(policy);
661 for (PolicyPtrArray::iterator pi = pols.begin(); pi != pols.end(); ++pi)
666 policyNames(names,
true);
668 PolicyPtrArray policies = getPolicyArray(*it);
671 PolicyPtrArray::iterator pi;
672 for(pi = policies.begin(); pi != policies.end(); pi++)
673 result += (*pi)->loadPolicyFiles(repos, strict);
702 ValidationError *errs)
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)
727 else if (tp ==
typeid(
int)) {
728 IntArray a = def->getIntArray(*nm);
729 IntArray::iterator vi;
730 for(vi=a.begin(); vi != a.end(); ++vi)
733 else if (tp ==
typeid(
double)) {
734 DoubleArray a = def->getDoubleArray(*nm);
735 DoubleArray::iterator vi;
736 for(vi=a.begin(); vi != a.end(); ++vi)
739 else if (tp ==
typeid(
string)) {
740 StringArray a = def->getStringArray(*nm);
741 StringArray::iterator vi;
742 for(vi=a.begin(); vi != a.end(); ++vi)
745 else if (def->isFile(*nm)) {
746 FilePtrArray a = def->getFileArray(*nm);
747 FilePtrArray::iterator vi;
748 for(vi=a.begin(); vi != a.end(); ++vi)
754 string(
"Unknown type for \"") + *nm
755 +
"\": \"" + getTypeName(*nm) +
"\"");
764 if (keepForValidation) {
765 if (defaultPol.isDictionary())
766 setDictionary(Dictionary(defaultPol));
767 else if (defaultPol.canValidate())
768 setDictionary(*defaultPol.getDictionary());
773 getDictionary()->validate(*
this, errs);
778 else if (defaultPol.isDictionary())
779 Dictionary(defaultPol).validate(*
this, errs);
788 string Policy::str(
const string& name,
const string& indent)
const {
793 if (tp ==
typeid(
bool)) {
794 BoolArray b = getBoolArray(name);
795 BoolArray::iterator vi;
796 for(vi=b.begin(); vi != b.end(); ++vi) {
798 if (vi+1 != b.end()) out <<
", ";
801 else if (tp ==
typeid(
int)) {
802 IntArray i = getIntArray(name);
803 IntArray::iterator vi;
804 for(vi=i.begin(); vi != i.end(); ++vi) {
806 if (vi+1 != i.end()) out <<
", ";
809 else if (tp ==
typeid(
double)) {
810 DoubleArray d = getDoubleArray(name);
811 DoubleArray::iterator vi;
812 for(vi=d.begin(); vi != d.end(); ++vi) {
814 if (vi+1 != d.end()) out <<
", ";
817 else if (tp ==
typeid(
string)) {
818 StringArray s = _data->getArray<
string>(name);
819 StringArray::iterator vi;
820 for(vi= s.begin(); vi != s.end(); ++vi) {
821 out <<
'"' << *vi <<
'"';
822 if (vi+1 != s.end()) out <<
", ";
827 _data->getArray<PropertySet::Ptr>(name);
829 for(vi= p.
begin(); vi != p.
end(); ++vi) {
831 Policy(*vi).print(out,
"", indent+
" ");
832 out << indent <<
"}";
833 if (vi+1 != p.
end()) out <<
", ";
838 FilePtrArray f = getFileArray(name);
839 FilePtrArray::iterator vi;
840 for(vi= f.begin(); vi != f.end(); ++vi) {
841 out <<
"FILE:" << (*vi)->getPath();
842 if (vi+1 != f.end()) out <<
", ";
848 "Policy: unexpected type held by any");
851 catch (NameNotFound&) {
863 const string& indent)
const 867 if (label.
size() > 0)
868 out << indent << label <<
":\n";
870 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
Citizen(const std::type_info &)
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