LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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/scoped_ptr.hpp>
34 #include <boost/filesystem/convenience.hpp>
35 
41 /*
42  * Workaround for boost::filesystem v2 (not needed in boost >= 1.46)
43  */
44 #include "boost/version.hpp"
45 #include "boost/filesystem/config.hpp"
46 #if BOOST_VERSION <= 104600 || BOOST_FILESYSTEM_VERSION < 3
47 namespace boost { namespace filesystem {
48  path absolute(const path& p)
49  {
50  return complete(p);
51  }
52 }}
53 #endif
54 namespace fs = boost::filesystem;
55 
56 namespace lsst {
57 namespace pex {
58 namespace policy {
59 
60 //@cond
61 
62 using std::string;
63 using std::ifstream;
64 using boost::regex;
65 using boost::regex_match;
66 using boost::regex_search;
67 using boost::scoped_ptr;
69 
70 namespace pexExcept = lsst::pex::exceptions;
71 
72 const string PolicyFile::EXT_PAF(".paf");
73 const string PolicyFile::EXT_XML(".xml");
74 
75 const regex PolicyFile::SPACE_RE("^\\s*$");
76 const regex PolicyFile::COMMENT("^\\s*#");
77 const regex
78  PolicyFile::CONTENTID("^\\s*#\\s*<\\?cfg\\s+\\w+(\\s+\\w+)*\\s*\\?>",
79  regex::icase);
80 
81 /*
82  * create a Policy file that points a file with given path.
83  * @param filepath the path to the file
84  */
86  : PolicySource(fmts), Persistable(),
87  _file(PolicyParserFactory::UNRECOGNIZED), _format(), _pfact()
88 { }
89 
90 PolicyFile::PolicyFile(const string& filepath,
91  const SupportedFormats::Ptr& fmts)
92  : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
93 { }
94 
95 PolicyFile::PolicyFile(const char *filepath,
96  const SupportedFormats::Ptr& fmts)
97  : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
98 { }
99 
100 PolicyFile::PolicyFile(const fs::path& filepath,
101  const SupportedFormats::Ptr& fmts)
102  : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
103 { }
104 
105 PolicyFile::PolicyFile(const string& filepath,
106  const PolicyParserFactory::Ptr& parserFactory)
107  : PolicySource(), Persistable(),
108  _file(filepath), _format(), _pfact(parserFactory)
109 {
110  if (! _pfact.get()) _format = _pfact->getFormatName();
111 }
112 
113 PolicyFile::PolicyFile(const fs::path& filepath,
114  const PolicyParserFactory::Ptr& parserFactory)
115  : PolicySource(), Persistable(),
116  _file(filepath), _format(), _pfact(parserFactory)
117 {
118  if (! _pfact.get()) _format = _pfact->getFormatName();
119 }
120 
121 PolicyFile::PolicyFile(const string& filepath, const fs::path& reposDir,
122  const SupportedFormats::Ptr& fmts)
123  : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
124 {
125  if (! _file.has_root_path() && ! reposDir.empty())
126  _file = reposDir / _file;
127 }
128 
129 PolicyFile::PolicyFile(const fs::path& filepath, const fs::path& reposDir,
130  const SupportedFormats::Ptr& fmts)
131  : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
132 {
133  if (! _file.has_root_path() && ! reposDir.empty())
134  _file = reposDir / _file;
135 }
136 
137 PolicyFile::PolicyFile(const string& filepath, const fs::path& reposDir,
138  const PolicyParserFactory::Ptr& parserFactory)
139  : PolicySource(), Persistable(),
140  _file(filepath), _format(), _pfact(parserFactory)
141 {
142  if (! _file.has_root_path() && ! reposDir.empty())
143  _file = reposDir / _file;
144  if (! _pfact.get()) _format = _pfact->getFormatName();
145 }
146 
147 PolicyFile::PolicyFile(const fs::path& filepath, const fs::path& reposDir,
148  const PolicyParserFactory::Ptr& parserFactory)
149  : PolicySource(), Persistable(),
150  _file(filepath), _format(), _pfact(parserFactory)
151 {
152  if (! _file.has_root_path() && ! reposDir.empty())
153  _file = reposDir / _file;
154  if (! _pfact.get()) _format = _pfact->getFormatName();
155 }
156 
157 /*
158  * return the name of the format that the data is stored in. This may
159  * cause the first few records of the source to be read.
160  * @exception IOError if an error occurs while reading the first few
161  * characters of the source stream.
162  */
163 const string& PolicyFile::getFormatName() {
164  if (_format.size() != 0) return _format;
165  if (_file.empty()) return PolicyParserFactory::UNRECOGNIZED;
166 
167  // check the extension first
168  string ext = fs::extension(_file);
169  if (! ext.empty()) {
170  if (ext == EXT_PAF) {
171  if (_formats->supports(PAFParserFactory::FORMAT_NAME))
172  return cacheName(PAFParserFactory::FORMAT_NAME);
173  }
174  else if (ext == EXT_XML) {
175  return cacheName("XML");
176  }
177  }
178 
179  // try reading the initial characters
180  if (fs::exists(_file)) {
181  ifstream is(_file.string().c_str());
182  if (is.fail())
183  throw LSST_EXCEPT(pexExcept::IoError,
184  "failure opening Policy file: "
185  + fs::absolute(_file).string());
186 
187  // skip over comments
188  string line;
189  getline(is, line);
190  while (is.good() &&
191  (regex_match(line, SPACE_RE) ||
192  (regex_search(line, COMMENT) && !regex_search(line, COMMENT))))
193  { }
194 
195  if (is.fail())
196  throw LSST_EXCEPT(pexExcept::IoError,
197  "failure reading Policy file: "
198  + fs::absolute(_file).string());
199  if (is.eof() &&
200  (regex_match(line, SPACE_RE) ||
201  (regex_search(line, COMMENT) && !regex_search(line, COMMENT))))
202  {
203  // empty file; let's just assume PAF (but don't cache the name).
204  return PAFParserFactory::FORMAT_NAME;
205  }
206 
207  return cacheName(_formats->recognizeType(line));
208  }
209 
210  return PolicyParserFactory::UNRECOGNIZED;
211 }
212 
213 /*
214  * load the data from this Policy source into a Policy object
215  * @param policy the policy object to load the data into
216  * @exception ParserError if an error occurs while parsing the data
217  * @exception IOError if an I/O error occurs while reading from the
218  * source stream.
219  */
220 void PolicyFile::load(Policy& policy) const {
221 
222  PolicyParserFactory::Ptr pfactory = _pfact;
223  if (! pfactory.get()) {
224  const string& fmtname = getFormatName();
225  if (fmtname.empty())
226  throw LSST_EXCEPT(ParserError, "Unknown Policy format: " + _file.string());
227 
228  pfactory = _formats->getFactory(fmtname);
229  }
230 
231  scoped_ptr<PolicyParser> parser(pfactory->createParser(policy));
232 
233  ifstream fs(_file.string().c_str());
234  if (fs.fail())
235  throw LSST_EXCEPT(pexExcept::IoError,
236  "failure opening Policy file: "
237  + fs::absolute(_file).string());
238 
239  parser->parse(fs);
240  fs.close();
241 }
242 
243 //@endcond
244 
245 }}} // end lsst::pex::policy
definition of the PolicyParser class
definition of the PolicyFile class
definition of Policy-specific exceptions classes
definition of Policy parsing exceptions
static const boost::regex COMMENT
reg-exp for an empty line
Definition: PolicyFile.h:215
boost::shared_ptr< SupportedFormats > Ptr
definition of the PAFParserFactory class
path absolute(const path &p)
Definition: PolicyFile.cc:48
static const std::string EXT_PAF
Definition: PolicyFile.h:211
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
def exists
Definition: cuda.py:53
static const boost::regex SPACE_RE
the XML file extension, &quot;.xml&quot;
Definition: PolicyFile.h:214
PolicyFile(const std::string &filepath, const SupportedFormats::Ptr &fmts=defaultFormats)
static const boost::regex CONTENTID
reg-exp for the start of a comment
Definition: PolicyFile.h:220
static const std::string EXT_XML
the PAF file extension, &quot;.paf&quot;
Definition: PolicyFile.h:212