Public Types | Public Member Functions | Private Attributes | List of all members
WireCell::Persist::Parser Class Reference

#include <Persist.h>

Public Types

typedef std::vector< std::stringpathlist_t
 

Public Member Functions

 Parser (const pathlist_t &load_paths=pathlist_t(), const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
 
Json::Value load (const std::string &filename)
 
Json::Value loads (const std::string &text)
 
std::string resolve (const std::string &filename)
 Below is a reimplimenatiaon of the above but in class form..... /. More...
 

Private Attributes

jsonnet::Jsonnet m_jsonnet
 
std::vector< boost::filesystem::path > m_load_paths
 

Detailed Description

Definition at line 128 of file Persist.h.

Member Typedef Documentation

Definition at line 130 of file Persist.h.

Constructor & Destructor Documentation

WireCell::Persist::Parser::Parser ( const pathlist_t load_paths = pathlist_t(),
const externalvars_t extvar = externalvars_t(),
const externalvars_t extcode = externalvars_t() 
)

Definition at line 221 of file Persist.cxx.

224 {
225  m_jsonnet.init();
226 
227  // Loading: 1) cwd, 2) passed in paths 3) environment
228  m_load_paths.push_back(boost::filesystem::current_path());
229  for (auto path : load_paths) {
230  m_load_paths.push_back(boost::filesystem::path(path));
231  }
232  for (auto path : get_path()) {
233  m_load_paths.push_back(boost::filesystem::path(path));
234  }
235  // load paths into jsonnet backwards to counteract its reverse ordering
236  for (auto pit = m_load_paths.rbegin(); pit != m_load_paths.rend(); ++pit) {
237  m_jsonnet.addImportPath(boost::filesystem::canonical(*pit).string());
238  }
239 
240 
241  // external variables
242  for (auto& vv : extvar) {
243  m_jsonnet.bindExtVar(vv.first, vv.second);
244  }
245  // external code
246  for (auto& vv : extcode) {
247  m_jsonnet.bindExtCodeVar(vv.first, vv.second);
248  }
249 }
std::vector< boost::filesystem::path > m_load_paths
Definition: Persist.h:147
static std::vector< std::string > get_path()
Definition: Persist.cxx:84
jsonnet::Jsonnet m_jsonnet
Definition: Persist.h:146

Member Function Documentation

Json::Value WireCell::Persist::Parser::load ( const std::string filename)

Definition at line 276 of file Persist.cxx.

277 {
279  if (fname.empty()) {
280  THROW(IOError() << errmsg{"no such file: " + filename
281  + ". Maybe you need to add to WIRECELL_PATH."});
282  }
283  string ext = file_extension(filename);
284 
285  if (ext == ".jsonnet" or ext.empty()) { // use libjsonnet++ file interface
286  std::string output;
287  const bool ok = m_jsonnet.evaluateFile(fname, &output);
288  if (!ok) {
289  error(m_jsonnet.lastError());
290  THROW(ValueError() << errmsg{m_jsonnet.lastError()});
291  }
292  return json2object(output);
293  }
294 
295  // also support JSON, possibly compressed
296 
297  // use jsoncpp file interface
298  std::fstream fp(fname.c_str(), std::ios::binary|std::ios::in);
299  boost::iostreams::filtering_stream<boost::iostreams::input> infilt;
300  if (ext == ".bz2" ) {
301  info("loading compressed json file: {}", fname);
302  infilt.push(boost::iostreams::bzip2_decompressor());
303  }
304  infilt.push(fp);
305  std::string text;
306  Json::Value jroot;
307  infilt >> jroot;
308  //return update(jroot, extvar); fixme
309  return jroot;
310 }
std::string resolve(const std::string &filename)
Below is a reimplimenatiaon of the above but in class form..... /.
Definition: Persist.cxx:258
Thrown when an error involving accessing input or output has occurred.
Definition: Exceptions.h:46
Json::Value json2object(const std::string &text)
Definition: Persist.cxx:161
std::string string
Definition: nybbler.cc:12
boost::error_info< struct tag_errmsg, std::string > errmsg
Definition: Exceptions.h:54
error
Definition: include.cc:26
string filename
Definition: train.py:213
static std::string file_extension(const std::string &filename)
Definition: Persist.cxx:28
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2106
#define THROW(e)
Definition: Exceptions.h:25
Thrown when a wrong value has been encountered.
Definition: Exceptions.h:37
jsonnet::Jsonnet m_jsonnet
Definition: Persist.h:146
Json::Value WireCell::Persist::Parser::loads ( const std::string text)

Definition at line 312 of file Persist.cxx.

313 {
314  std::string output;
315  bool ok = m_jsonnet.evaluateSnippet("<stdin>", text, &output);
316  if (!ok) {
317  error(m_jsonnet.lastError());
318  THROW(ValueError() << errmsg{m_jsonnet.lastError()});
319  }
320  return json2object(output);
321 }
Json::Value json2object(const std::string &text)
Definition: Persist.cxx:161
std::string string
Definition: nybbler.cc:12
boost::error_info< struct tag_errmsg, std::string > errmsg
Definition: Exceptions.h:54
error
Definition: include.cc:26
#define THROW(e)
Definition: Exceptions.h:25
Thrown when a wrong value has been encountered.
Definition: Exceptions.h:37
jsonnet::Jsonnet m_jsonnet
Definition: Persist.h:146
std::string WireCell::Persist::Parser::resolve ( const std::string filename)

Below is a reimplimenatiaon of the above but in class form..... /.

Definition at line 258 of file Persist.cxx.

259 {
260  if (filename.empty()) {
261  return "";
262  }
263  if (filename[0] == '/') {
264  return filename;
265  }
266 
267  for (auto pobj : m_load_paths) {
268  boost::filesystem::path full = pobj / filename;
269  if (boost::filesystem::exists(full)) {
270  return boost::filesystem::canonical(full).string();
271  }
272  }
273  return "";
274 }
std::vector< boost::filesystem::path > m_load_paths
Definition: Persist.h:147
string filename
Definition: train.py:213
bool exists(std::string path)

Member Data Documentation

jsonnet::Jsonnet WireCell::Persist::Parser::m_jsonnet
private

Definition at line 146 of file Persist.h.

std::vector<boost::filesystem::path> WireCell::Persist::Parser::m_load_paths
private

Definition at line 147 of file Persist.h.


The documentation for this class was generated from the following files: