LSSTApplications
19.0.0-14-gb0260a2+d60062ef16,20.0.0+1540ce6389,20.0.0+7c6b12c2f9,20.0.0+ae956f52c5,20.0.0+be870186d9,20.0.0+e2e26847c2,20.0.0-1-g10df615+7683e4f082,20.0.0-1-g253301a+7c6b12c2f9,20.0.0-1-g2b7511a+46a6078777,20.0.0-1-g3dda6ea+606b36f8c0,20.0.0-1-g4d801e7+901ee84527,20.0.0-1-g5b95a8c+a5fa15ec54,20.0.0-1-gb058bd0+46a6078777,20.0.0-1-gb88604f+acecce4127,20.0.0-1-gc96f8cb+61a4a056b1,20.0.0-1-gedffbd8+4f0e391d5e,20.0.0-10-g0891cd99+aadc987f3e,20.0.0-10-g9a20bd332+576ca7b471,20.0.0-17-gcdbda88+ed0d4927ab,20.0.0-2-g4dae9ad+61a4a056b1,20.0.0-2-g61b8584+85c46248f3,20.0.0-2-gb780d76+f45b7d88f4,20.0.0-2-gf072044+7c6b12c2f9,20.0.0-21-g9bbb7f7+61a4a056b1,20.0.0-22-gc512666+9eba1c4719,20.0.0-23-g8900aa8+68630f7098,20.0.0-3-g1653f94+85c46248f3,20.0.0-3-g4cc78c6+63636aeed8,20.0.0-3-g750bffe+e05f822de9,20.0.0-3-gbd60e8c+ff10c6d78d,20.0.0-32-g15a0e07c+ff1c9f120b,20.0.0-4-g97dc21a+68630f7098,20.0.0-4-gfea843c+f45b7d88f4,20.0.0-5-g357b56b+f45b7d88f4,20.0.0-6-g9a5b7a1+2c4171520d,20.0.0-61-g4de25fb+e4dd172200,20.0.0-7-gcda7bf1+85e953d7e4,w.2020.43
LSSTDataManagementBasePackage
|
Go to the documentation of this file.
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()) {}
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);
112 extractDefaults(*subp, *dict.getSubDictionary(
name), ve);
113 if (subp->nameCount() > 0)
target.add(
name, subp);
132 : Persistable(), _data(new PropertySet()) {
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()) {
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);
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);
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 {
440 return getFile(
name);
444 return getPolicy(
name);
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);
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);
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.
virtual ~Policy()
destroy this policy
PolicyPtrArray getPolicyArray(const std::string &name) const
return an array of Policy pointers associated with the given name.
virtual std::string str(const std::string &name, const std::string &indent="") const
return a string representation of the value given by a name.
std::vector< FilePtr > FilePtrArray
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?
std::vector< ConstPtr > ConstPolicyPtrArray
ValueType
an enumeration for the supported policy types
void set(const std::string &name, const Ptr &value)
FilePtr getFile(const std::string &name) const
return a PolicyFile (a reference to a file with "sub-Policy" data) identified by a given name.
std::vector< std::string > StringArray
std::shared_ptr< const Policy > ConstPtr
Reports attempts to access elements using an invalid key.
std::vector< bool > BoolArray
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
static ValueType getValueType()
A template-ized way to get the ValueType.
void validate(ValidationError *errs=0) const
Validate this policy, using its stored dictionary.
const std::string getString(const std::string &name) const
return a string value associated with the given name .
int getInt(const std::string &name) const
return an integer value associated with the given name.
std::shared_ptr< RecordT > src
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...
static ValueType getTypeByName(const std::string &name)
Given the human-readable name of a type ("bool", "int", "policy", etc), what is its ValueType (BOOL,...
int loadPolicyFiles(bool strict=true)
Recursively replace all PolicyFile values with the contents of the files they refer to.
int policyNames(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
definition of Policy parsing exceptions
ConstPolicyPtrArray getConstPolicyArray(const std::string &name) const
void remove(const std::string &name)
Remove all values with a given name.
static Policy * createPolicyFromUrn(const std::string &urn, bool validate=true)
Create a Policy from a file specified by a URN.
the definition of the UrnPolicyFile class
DoubleArray getDoubleArray(const std::string &name) const
const char * source()
Source function that allows astChannel to source from a Stream.
bool exists(const std::string &name) const
return true if a value exists in this policy for the given name.
double getDouble(const std::string &name) const
return a double value associated with the given name.
void addValue(const std::string &name, const T &value)
Add a value to an array of values with a given name.
std::vector< int > IntArray
void setValue(const std::string &name, const T &value)
Set a value with the given name.
std::vector< Ptr > PolicyPtrArray
BoolArray getBoolArray(const std::string &name) const
return an array of values associated with the given name
Reports errors in the logical structure of the program.
const ConstDictPtr getDictionary() const
The dictionary (if any) that this policy uses to validate itself, including checking set() and add() ...
IntArray getIntArray(const std::string &name) const
FilePtrArray getFileArray(const std::string &name) const
return an array of PolicyFile pointers associated with the given name.
Policy()
Create an empty policy.
definition of the PolicyFile class
A base class for image defects.
definition of the Dictionary class
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Key< Flag > const & target
std::shared_ptr< Policy > Ptr
Reports errors in external input/output operations.
static FilePtr createPolicyFile(const std::string &pathOrUrn, bool strict=false)
Create a PolicyFile or UrnPolicyFile from pathOrUrn.
std::string toString() const
convert the entire contents of this policy to a string.
bool getBool(const std::string &name) const
return a boolean value associated with the given name.
Base class for all persistable classes.
std::shared_ptr< PolicyFile > FilePtr
std::shared_ptr< const Dictionary > ConstDictPtr
#define LSST_EXCEPT_HERE
For internal use; gathers the file, line, and function for a tracepoint.
definition of the PolicySource class
Class for storing generic metadata.
bool canValidate() const
Can this policy validate itself – that is, does it have a dictionary that it can use to validate itse...
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 const char *const typeName[]
c-string forms for the supported value types.
void add(const std::string &name, const Ptr &value)
int fileNames(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
StringArray getStringArray(const std::string &name) const
std::vector< double > DoubleArray
void setDictionary(const Dictionary &dict)
Update this policy's dictionary that it uses to validate itself.
bool isPolicy(const std::string &name) const
return true if the value pointed to by the given name is a Policy
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.
bool isFile(const std::string &name) const
return true if the value pointed to by the given name is a PolicyFile