LSSTApplications  19.0.0+10,19.0.0+80,19.0.0-1-g20d9b18+31,19.0.0-1-g49a97f9+4,19.0.0-1-g8c57eb9+31,19.0.0-1-g9a028c0+13,19.0.0-1-ga72da6b+3,19.0.0-1-gb77924a+15,19.0.0-1-gbfe0924+66,19.0.0-1-gc3516c3,19.0.0-1-gefe1d0d+49,19.0.0-1-gf8cb8b4+3,19.0.0-14-g7511ce4+6,19.0.0-16-g3dc1a33c+6,19.0.0-17-g59f0e8a+4,19.0.0-17-g9c22e3c+9,19.0.0-18-g35bb99870+2,19.0.0-19-g2772d4a+9,19.0.0-2-g260436e+53,19.0.0-2-g31cdcee,19.0.0-2-g9675b69+3,19.0.0-2-gaa2795f,19.0.0-2-gbcc4de1,19.0.0-2-gd6f004e+6,19.0.0-2-gde8e5e3+5,19.0.0-2-gff6972b+15,19.0.0-21-ge306cd8,19.0.0-21-gf122e69+2,19.0.0-3-g2d43a51+2,19.0.0-3-gf3b1435+6,19.0.0-4-g56feb96,19.0.0-4-gac56cce+17,19.0.0-49-gce872c1+1,19.0.0-5-g66946eb+6,19.0.0-5-gd8897ba+6,19.0.0-51-gfc4a647e,19.0.0-7-g686a884+5,19.0.0-7-g886f805+5,19.0.0-8-g305ff64,w.2020.17
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  */
81 PolicyFile::PolicyFile(const SupportedFormats::Ptr& fmts)
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
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
definition of Policy-specific exceptions classes
STL class.
definition of the PAFParserFactory class
A base class for image defects.
path absolute(const path &p)
Definition: PolicyFile.cc:48
def line(points, frame=None, origin=afwImage.PARENT, symbs=False, ctype=None, size=0.5)
Definition: ds9.py:105
Definition: __init__.py:1
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
STL class.
STL class.