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
PolicyFile.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008, 2009, 2010 LSST Corporation.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
31 #include <fstream>
32 
33 #include <boost/filesystem/convenience.hpp>
34 
40 /*
41  * Workaround for boost::filesystem v2 (not needed in boost >= 1.46)
42  */
43 #include "boost/version.hpp"
44 #include "boost/filesystem/config.hpp"
45 #if BOOST_VERSION <= 104600 || BOOST_FILESYSTEM_VERSION < 3
46 namespace boost {
47 namespace filesystem {
48 path absolute(const path& p) { return complete(p); }
49 } // namespace filesystem
50 } // namespace boost
51 #endif
52 namespace fs = boost::filesystem;
53 
54 namespace lsst {
55 namespace pex {
56 namespace policy {
57 
58 //@cond
59 
60 using boost::regex;
61 using boost::regex_match;
62 using boost::regex_search;
64 using std::ifstream;
65 using std::string;
66 using std::unique_ptr;
67 
69 
70 const string PolicyFile::EXT_PAF(".paf");
71 const string PolicyFile::EXT_XML(".xml");
72 
73 const regex PolicyFile::SPACE_RE("^\\s*$");
74 const regex PolicyFile::COMMENT("^\\s*#");
75 const regex PolicyFile::CONTENTID("^\\s*#\\s*<\\?cfg\\s+\\w+(\\s+\\w+)*\\s*\\?>", regex::icase);
76 
77 /*
78  * create a Policy file that points a file with given path.
79  * @param filepath the path to the file
80  */
82  : PolicySource(fmts), Persistable(), _file(PolicyParserFactory::UNRECOGNIZED), _format(), _pfact() {}
83 
84 PolicyFile::PolicyFile(const string& filepath, const SupportedFormats::Ptr& fmts)
85  : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {}
86 
87 PolicyFile::PolicyFile(const char* filepath, const SupportedFormats::Ptr& fmts)
88  : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {}
89 
90 PolicyFile::PolicyFile(const fs::path& filepath, const SupportedFormats::Ptr& fmts)
91  : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {}
92 
93 PolicyFile::PolicyFile(const string& filepath, const PolicyParserFactory::Ptr& parserFactory)
94  : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
95  if (!_pfact.get()) _format = _pfact->getFormatName();
96 }
97 
98 PolicyFile::PolicyFile(const fs::path& filepath, const PolicyParserFactory::Ptr& parserFactory)
99  : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
100  if (!_pfact.get()) _format = _pfact->getFormatName();
101 }
102 
103 PolicyFile::PolicyFile(const string& filepath, const fs::path& reposDir, const SupportedFormats::Ptr& fmts)
104  : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {
105  if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
106 }
107 
108 PolicyFile::PolicyFile(const fs::path& filepath, const fs::path& reposDir, const SupportedFormats::Ptr& fmts)
109  : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {
110  if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
111 }
112 
113 PolicyFile::PolicyFile(const string& filepath, const fs::path& reposDir,
114  const PolicyParserFactory::Ptr& parserFactory)
115  : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
116  if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
117  if (!_pfact.get()) _format = _pfact->getFormatName();
118 }
119 
120 PolicyFile::PolicyFile(const fs::path& filepath, const fs::path& reposDir,
121  const PolicyParserFactory::Ptr& parserFactory)
122  : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
123  if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
124  if (!_pfact.get()) _format = _pfact->getFormatName();
125 }
126 
127 /*
128  * return the name of the format that the data is stored in. This may
129  * cause the first few records of the source to be read.
130  * @exception IOError if an error occurs while reading the first few
131  * characters of the source stream.
132  */
133 const string& PolicyFile::getFormatName() {
134  if (_format.size() != 0) return _format;
135  if (_file.empty()) return PolicyParserFactory::UNRECOGNIZED;
136 
137  // check the extension first
138  string ext = fs::extension(_file);
139  if (!ext.empty()) {
140  if (ext == EXT_PAF) {
141  if (_formats->supports(PAFParserFactory::FORMAT_NAME))
142  return cacheName(PAFParserFactory::FORMAT_NAME);
143  } else if (ext == EXT_XML) {
144  return cacheName("XML");
145  }
146  }
147 
148  // try reading the initial characters
149  if (fs::exists(_file)) {
150  ifstream is(_file.string().c_str());
151  if (is.fail())
153  "failure opening Policy file: " + fs::absolute(_file).string());
154 
155  // skip over comments
156  string line;
157  getline(is, line);
158  while (is.good() && (regex_match(line, SPACE_RE) ||
159  (regex_search(line, COMMENT) && !regex_search(line, COMMENT)))) {
160  }
161 
162  if (is.fail())
164  "failure reading Policy file: " + fs::absolute(_file).string());
165  if (is.eof() &&
166  (regex_match(line, SPACE_RE) || (regex_search(line, COMMENT) && !regex_search(line, COMMENT)))) {
167  // empty file; let's just assume PAF (but don't cache the name).
168  return PAFParserFactory::FORMAT_NAME;
169  }
170 
171  return cacheName(_formats->recognizeType(line));
172  }
173 
174  return PolicyParserFactory::UNRECOGNIZED;
175 }
176 
177 /*
178  * load the data from this Policy source into a Policy object
179  * @param policy the policy object to load the data into
180  * @exception ParserError if an error occurs while parsing the data
181  * @exception IOError if an I/O error occurs while reading from the
182  * source stream.
183  */
184 void PolicyFile::load(Policy& policy) const {
185  PolicyParserFactory::Ptr pfactory = _pfact;
186  if (!pfactory.get()) {
187  const string& fmtname = getFormatName();
188  if (fmtname.empty()) throw LSST_EXCEPT(ParserError, "Unknown Policy format: " + _file.string());
189 
190  pfactory = _formats->getFactory(fmtname);
191  }
192 
193  std::unique_ptr<PolicyParser> parser(pfactory->createParser(policy));
194 
195  ifstream fs(_file.string().c_str());
196  if (fs.fail())
197  throw LSST_EXCEPT(pexExcept::IoError, "failure opening Policy file: " + fs::absolute(_file).string());
198 
199  parser->parse(fs);
200  fs.close();
201 }
202 
203 //@endcond
204 
205 } // namespace policy
206 } // namespace pex
207 } // namespace lsst
std::string
STL class.
PolicyParser.h
definition of the PolicyParser class
std::regex_match
T regex_match(T... args)
lsst.pex::policy::PolicyFile::EXT_XML
static const std::string EXT_XML
the PAF file extension, ".paf"
Definition: PolicyFile.h:200
boost
Definition: Polygon.cc:25
lsst.pex::policy::SupportedFormats::Ptr
std::shared_ptr< SupportedFormats > Ptr
Definition: SupportedFormats.h:54
boost::filesystem
Definition: PolicyFile.cc:47
lsst.pex::policy::PolicyFile::SPACE_RE
static const boost::regex SPACE_RE
the XML file extension, ".xml"
Definition: PolicyFile.h:202
boost::filesystem::absolute
path absolute(const path &p)
Definition: PolicyFile.cc:48
parserexceptions.h
definition of Policy parsing exceptions
PAFParserFactory.h
definition of the PAFParserFactory class
lsst.pex::policy::PolicyFile::COMMENT
static const boost::regex COMMENT
reg-exp for an empty line
Definition: PolicyFile.h:203
lsst.pex::policy::PolicyFile::CONTENTID
static const boost::regex CONTENTID
reg-exp for the start of a comment
Definition: PolicyFile.h:208
lsst::afw.display.ds9.line
def line(points, frame=None, origin=afwImage.PARENT, symbs=False, ctype=None, size=0.5)
Definition: ds9.py:105
PolicyFile.h
definition of the PolicyFile class
exceptions.h
definition of Policy-specific exceptions classes
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
std::regex_search
T regex_search(T... args)
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst.pex::exceptions::IoError
Reports errors in external input/output operations.
Definition: Runtime.h:160
std::getline
T getline(T... args)
lsst.pex::policy::paf::PAFParserFactory
a class for creating PAFParser objects
Definition: PAFParserFactory.h:53
lsst.pex::policy::PolicyFile::EXT_PAF
static const std::string EXT_PAF
Definition: PolicyFile.h:199
lsst.pex::exceptions
Definition: Exception.h:37
lsst.pex::policy::PolicyFile::PolicyFile
PolicyFile(const std::string &filepath, const SupportedFormats::Ptr &fmts=defaultFormats)
create a Policy file that points a file with given path.
std::unique_ptr
STL class.
std::ifstream
STL class.