LSSTApplications  20.0.0
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
lsst::pex::policy::SupportedFormats::Ptr
std::shared_ptr< SupportedFormats > Ptr
Definition: SupportedFormats.h:54
std::string
STL class.
lsst::pex::policy::PolicyFile::EXT_PAF
static const std::string EXT_PAF
Definition: PolicyFile.h:199
PolicyParser.h
definition of the PolicyParser class
lsst.synpipe.compareModel.parser
parser
Definition: compareModel.py:108
lsst::pex::policy::PolicyFile::COMMENT
static const boost::regex COMMENT
reg-exp for an empty line
Definition: PolicyFile.h:203
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.
lsst::pex::policy::PolicyFile::CONTENTID
static const boost::regex CONTENTID
reg-exp for the start of a comment
Definition: PolicyFile.h:208
std::regex_match
T regex_match(T... args)
boost
Definition: Polygon.cc:25
boost::filesystem
Definition: PolicyFile.cc:47
boost::filesystem::absolute
path absolute(const path &p)
Definition: PolicyFile.cc:48
pex
Definition: __init__.py:1
lsst::pex::policy::PolicyFile::SPACE_RE
static const boost::regex SPACE_RE
the XML file extension, ".xml"
Definition: PolicyFile.h:202
parserexceptions.h
definition of Policy parsing exceptions
PAFParserFactory.h
definition of the PAFParserFactory class
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
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
exceptions.h
definition of Policy-specific exceptions classes
std::getline
T getline(T... args)
lsst::pex::policy::paf::PAFParserFactory
a class for creating PAFParser objects
Definition: PAFParserFactory.h:53
lsst::pex::exceptions
Definition: Exception.h:37
std::unique_ptr
STL class.
lsst::pex::policy::PolicyFile::EXT_XML
static const std::string EXT_XML
the PAF file extension, ".paf"
Definition: PolicyFile.h:200
std::ifstream
STL class.