Classes | Typedefs | Functions
WireCell::Persist Namespace Reference

Classes

class  Parser
 

Typedefs

typedef std::map< std::string, std::stringexternalvars_t
 

Functions

bool exists (const std::string &filename)
 Return true file exists (no file resolution performed). More...
 
std::string resolve (const std::string &filename)
 
std::string slurp (const std::string &filename)
 
void dump (const std::string &filename, const Json::Value &top, bool pretty=false)
 
std::string dumps (const Json::Value &top, bool pretty=false)
 As above but dump to a JSON text string. More...
 
Json::Value load (const std::string &filename, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
 
Json::Value loads (const std::string &text, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
 
std::string evaluate_jsonnet_file (const std::string &filename, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
 
std::string evaluate_jsonnet_text (const std::string &text, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
 
Json::Value json2object (const std::string &text)
 
template<typename Iterable >
Json::Value iterable2json (Iterable const &cont)
 

Typedef Documentation

This can hold either Jsonnet external variable/value pairs The value is a string representation of a simple scalar value, or a data structure if used as code. This type may also be used to hold JSON path/value pairs.

Definition at line 69 of file Persist.h.

Function Documentation

void WireCell::Persist::dump ( const std::string filename,
const Json::Value top,
bool  pretty = false 
)

Save the data structure held by the given top Json::Value in to a file of the given name. The format of the file is determined by the file name extension. Valid extensions are:

  • .json :: JSON text format
  • .json.bz2 :: JSON text format compressed with bzip2

If pretty is true then format the JSON text with indents. If also compressed, this formatting can actually lead to smaller files.

default to .json.bz2 regardless of extension.

Definition at line 37 of file Persist.cxx.

38 {
39  string ext = file_extension(filename);
40 
41  /// default to .json.bz2 regardless of extension.
42  std::fstream fp(filename.c_str(), std::ios::binary|std::ios::out);
43  boost::iostreams::filtering_stream<boost::iostreams::output> outfilt;
44  if (ext == ".bz2") {
45  outfilt.push(boost::iostreams::bzip2_compressor());
46  }
47  outfilt.push(fp);
48  if (pretty) {
49  Json::StyledWriter jwriter;
50  outfilt << jwriter.write(jroot);
51  }
52  else {
53  Json::FastWriter jwriter;
54  outfilt << jwriter.write(jroot);
55  }
56 }
string filename
Definition: train.py:213
static std::string file_extension(const std::string &filename)
Definition: Persist.cxx:28
std::string WireCell::Persist::dumps ( const Json::Value top,
bool  pretty = false 
)

As above but dump to a JSON text string.

Definition at line 58 of file Persist.cxx.

59 {
60  stringstream ss;
61  ss << cfg;
62  return ss.str();
63 }
cfg
Definition: dbjson.py:29
std::string WireCell::Persist::evaluate_jsonnet_file ( const std::string filename,
const externalvars_t extvar = externalvars_t(),
const externalvars_t extcode = externalvars_t() 
)

Explicitly evaluate contents of file with Jsonnet. If no support for Jsonnet is built, return the contents of file. Return empty string if Jsonnet evaluation failes.

WireCell::IOError is thrown if file is not found. WireCell::ValueError is thrown parsing fails.

Definition at line 185 of file Persist.cxx.

188 {
190  if (fname.empty()) {
191  THROW(IOError() << errmsg{"no such file: " + filename + ", maybe you need to add to WIRECELL_PATH."});
192  }
193 
194  jsonnet::Jsonnet parser;
195  init_parser(parser, extvar, extcode);
196 
197  std::string output;
198  const bool ok = parser.evaluateFile(fname, &output);
199  if (!ok) {
200  error(parser.lastError());
201  THROW(ValueError() << errmsg{parser.lastError()});
202  }
203  return output;
204 }
Thrown when an error involving accessing input or output has occurred.
Definition: Exceptions.h:46
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
#define THROW(e)
Definition: Exceptions.h:25
Thrown when a wrong value has been encountered.
Definition: Exceptions.h:37
static void init_parser(jsonnet::Jsonnet &parser, const Persist::externalvars_t &extvar, const Persist::externalvars_t &extcode)
Definition: Persist.cxx:170
std::string resolve(const std::string &filename)
Definition: Persist.cxx:99
std::string WireCell::Persist::evaluate_jsonnet_text ( const std::string text,
const externalvars_t extvar = externalvars_t(),
const externalvars_t extcode = externalvars_t() 
)

Explicitly evaluate text with JSonnet. If no support for Jsonnet is built, return the text. Return empty string if Jsonnet evaluation failes.

WireCell::ValueError is thrown parsing fails.

Definition at line 205 of file Persist.cxx.

208 {
209  jsonnet::Jsonnet parser;
210  init_parser(parser, extvar, extcode);
211 
212  std::string output;
213  bool ok = parser.evaluateSnippet("<stdin>", text, &output);
214  if (!ok) {
215  error(parser.lastError());
216  THROW(ValueError() << errmsg{parser.lastError()});
217  }
218  return output;
219 }
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
static void init_parser(jsonnet::Jsonnet &parser, const Persist::externalvars_t &extvar, const Persist::externalvars_t &extcode)
Definition: Persist.cxx:170
bool WireCell::Persist::exists ( const std::string filename)

Return true file exists (no file resolution performed).

Definition at line 79 of file Persist.cxx.

80 {
82 }
string filename
Definition: train.py:213
bool exists(std::string path)
template<typename Iterable >
Json::Value WireCell::Persist::iterable2json ( Iterable const &  cont)

Convert a collection to a Json::Value

Definition at line 117 of file Persist.h.

117  {
118  Json::Value v;
119  for (auto&& element: cont) {
120  v.append(element);
121  }
122  return v;
123  }
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2106
Json::Value WireCell::Persist::json2object ( const std::string text)

Explicitly convert JSON text to Json::Value object

Definition at line 161 of file Persist.cxx.

162 {
163  Json::Value res;
164  stringstream ss(text);
165  ss >> res;
166  return res;
167 }
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2106
Json::Value WireCell::Persist::load ( const std::string filename,
const externalvars_t extvar = externalvars_t(),
const externalvars_t extcode = externalvars_t() 
)

Load a file and return the top JSON value.

If extension is .jsonnet and Jsonnet support is compiled in, evaluate the file and use the resulting JSON. Other supported extensions include raw (.json) or compressed (.json.bz2) files.

WireCell::IOError is thrown if file is not found.

Definition at line 121 of file Persist.cxx.

124 {
125  string ext = file_extension(filename);
126  if (ext == ".jsonnet") { // use libjsonnet++ file interface
127  string text = evaluate_jsonnet_file(filename, extvar, extcode);
128  return json2object(text);
129  }
130 
132  if (fname.empty()) {
133  THROW(IOError() << errmsg{"no such file: " + filename + ". Maybe you need to add to WIRECELL_PATH."});
134  }
135 
136 
137  // use jsoncpp file interface
138  std::fstream fp(fname.c_str(), std::ios::binary|std::ios::in);
139  boost::iostreams::filtering_stream<boost::iostreams::input> infilt;
140  if (ext == ".bz2" ) {
141  info("loading compressed json file: {}", fname);
142  infilt.push(boost::iostreams::bzip2_decompressor());
143  }
144  infilt.push(fp);
145  std::string text;
146  Json::Value jroot;
147  infilt >> jroot;
148  //return update(jroot, extvar); fixme
149  return jroot;
150 }
std::string evaluate_jsonnet_file(const std::string &filename, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
Definition: Persist.cxx:185
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
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
std::string resolve(const std::string &filename)
Definition: Persist.cxx:99
Json::Value WireCell::Persist::loads ( const std::string text,
const externalvars_t extvar = externalvars_t(),
const externalvars_t extcode = externalvars_t() 
)

Load a JSON or Jsonnet string, returning a Json::Value.

Definition at line 152 of file Persist.cxx.

155 {
156  const std::string jtext = evaluate_jsonnet_text(text, extvar, extcode);
157  return json2object(jtext);
158 }
std::string evaluate_jsonnet_text(const std::string &text, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
Definition: Persist.cxx:205
Json::Value json2object(const std::string &text)
Definition: Persist.cxx:161
std::string string
Definition: nybbler.cc:12
std::string WireCell::Persist::resolve ( const std::string filename)

Return full path to a file of the given filename. If the file is not directly located and is a relative path then the file will be first located in the current working directory. Failing that if the WIRECELL_PATH environment variable is defined and set as a :-separated list it will be checked. Failure to resolve returns an empty string.

Definition at line 99 of file Persist.cxx.

100 {
101  if (filename.empty()) {
102  return "";
103  }
104  if (filename[0] == '/') {
105  return filename;
106  }
107 
108  std::vector<boost::filesystem::path> tocheck{boost::filesystem::current_path(),};
109  for (auto pathname : get_path()) {
110  tocheck.push_back(boost::filesystem::path(pathname));
111  }
112  for (auto pobj : tocheck) {
113  boost::filesystem::path full = pobj / filename;
114  if (boost::filesystem::exists(full)) {
115  return boost::filesystem::canonical(full).string();
116  }
117  }
118  return "";
119 }
static std::vector< std::string > get_path()
Definition: Persist.cxx:84
string filename
Definition: train.py:213
bool exists(std::string path)
std::string WireCell::Persist::slurp ( const std::string filename)

Return a string holding the entire contents of the file. File resolution is performed. WireCell::IOError is thrown if file is not found.

Definition at line 65 of file Persist.cxx.

66 {
68  if (fname.empty()) {
69  THROW(IOError() << errmsg{"no such file: " + filename + ". Maybe you need to add to WIRECELL_PATH."});
70  }
71 
72  std::ifstream fstr(filename);
73  std::stringstream buf;
74  buf << fstr.rdbuf();
75  return buf.str();
76 }
Thrown when an error involving accessing input or output has occurred.
Definition: Exceptions.h:46
std::string string
Definition: nybbler.cc:12
boost::error_info< struct tag_errmsg, std::string > errmsg
Definition: Exceptions.h:54
string filename
Definition: train.py:213
#define THROW(e)
Definition: Exceptions.h:25
std::string resolve(const std::string &filename)
Definition: Persist.cxx:99