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