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);
136 loadedDict = _dictionary;
138 loadedDict.reset(
new Dictionary(dict));
140 loadedDict->loadPolicyFiles(repository,
true);
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()) {
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();
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) {
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];
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) {
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 {
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 {
586 for (i = pfa.
begin(); i != pfa.
end(); ++i) {
596 _data->set(name, std::dynamic_pointer_cast<Persistable>(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;
static std::type_info const & typeOfT()
Get type info for the specified class.
std::vector< int > IntArray
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() ...
Key< Flag > const & target
bool canValidate() const
Can this policy validate itself – that is, does it have a dictionary that it can use to validate its...
int policyNames(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
load the names of parameters into a given list.
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.
#define LSST_EXCEPT_HERE
For internal use; gathers the file, line, and function for a tracepoint.
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
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.
bool isFile(const std::string &name) const
return true if the value pointed to by the given name is a PolicyFile
DoubleArray getDoubleArray(const std::string &name) const
return an array of values associated with the given name
Reports errors in external input/output operations.
FilePtr getFile(const std::string &name) const
return a PolicyFile (a reference to a file with "sub-Policy" data) identified by a given name...
StringArray getStringArray(const std::string &name) const
return an array of values associated with the given name
bool getBool(const std::string &name) const
return a boolean value associated with the given name.
Reports attempts to access elements using an invalid key.
static FilePtr createPolicyFile(const std::string &pathOrUrn, bool strict=false)
Create a PolicyFile or UrnPolicyFile from pathOrUrn.
A base class for image defects.
bool exists(const std::string &name) const
return true if a value exists in this policy for the given name.
std::vector< std::string > StringArray
std::shared_ptr< RecordT > src
ConstPtr getPolicy(const std::string &name) const
return a "sub-Policy" identified by a given name.
int getInt(const std::string &name) const
return an integer value associated with the given name.
int fileNames(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
load the names of parameters into a given list.
void addValue(const std::string &name, const T &value)
Add a value to an array of values with a given name.
const char * source()
Source function that allows astChannel to source from a Stream.
T dynamic_pointer_cast(T... args)
Reports errors in the logical structure of the program.
const std::string getString(const std::string &name) const
return a string value associated with the given name .
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...
bool isPolicy(const std::string &name) const
return true if the value pointed to by the given name is a Policy
double getDouble(const std::string &name) const
return a double value associated with the given name.
BoolArray getBoolArray(const std::string &name) const
return an array of values associated with the given name
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
std::vector< FilePtr > FilePtrArray
const char * getTypeName(const std::string &name) const
return a string name for the type associated with the parameter of a given name.
int names(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
load the names of parameters into a given list.
std::vector< double > DoubleArray
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.
Class for storing generic metadata.
Persistable(void)
Default constructor.
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.
Base class for all persistable classes.
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...
std::vector< bool > BoolArray
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
IntArray getIntArray(const std::string &name) const
return an array of values associated with the given name
Policy()
Create an empty policy.
std::vector< ConstPtr > ConstPolicyPtrArray
ValueType
an enumeration for the supported policy types
virtual ~Policy()
destroy this policy