6 #include "libjsonnet++.h" 11 #include <boost/iostreams/copy.hpp> 12 #include <boost/iostreams/filter/bzip2.hpp> 13 #include <boost/iostreams/device/file.hpp> 14 #include <boost/iostreams/filtering_stream.hpp> 15 #include <boost/filesystem.hpp> 26 #define WIRECELL_PATH_VARNAME "WIRECELL_PATH" 30 auto ind = filename.rfind(
".");
31 if (ind == string::npos) {
34 return filename.substr(ind);
42 std::fstream
fp(filename.c_str(), std::ios::binary|std::ios::out);
43 boost::iostreams::filtering_stream<boost::iostreams::output> outfilt;
45 outfilt.push(boost::iostreams::bzip2_compressor());
49 Json::StyledWriter jwriter;
50 outfilt << jwriter.write(jroot);
53 Json::FastWriter jwriter;
54 outfilt << jwriter.write(jroot);
69 THROW(
IOError() <<
errmsg{
"no such file: " + filename +
". Maybe you need to add to WIRECELL_PATH."});
72 std::ifstream fstr(filename);
73 std::stringstream buf;
86 std::vector<std::string> ret;
101 if (filename.empty()) {
104 if (filename[0] ==
'/') {
108 std::vector<boost::filesystem::path> tocheck{boost::filesystem::current_path(),};
110 tocheck.push_back(boost::filesystem::path(pathname));
112 for (
auto pobj : tocheck) {
113 boost::filesystem::path full = pobj /
filename;
115 return boost::filesystem::canonical(full).string();
126 if (ext ==
".jsonnet") {
133 THROW(
IOError() <<
errmsg{
"no such file: " + filename +
". Maybe you need to add to WIRECELL_PATH."});
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());
164 stringstream ss(text);
176 parser.addImportPath(path);
178 for (
auto& vv : extvar) {
179 parser.bindExtVar(vv.first, vv.second);
181 for (
auto& vv : extcode) {
182 parser.bindExtCodeVar(vv.first, vv.second);
191 THROW(
IOError() <<
errmsg{
"no such file: " + filename +
", maybe you need to add to WIRECELL_PATH."});
198 const bool ok = parser.evaluateFile(fname, &output);
200 error(parser.lastError());
213 bool ok = parser.evaluateSnippet(
"<stdin>", text, &output);
215 error(parser.lastError());
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));
233 m_load_paths.push_back(boost::filesystem::path(path));
236 for (
auto pit = m_load_paths.rbegin(); pit != m_load_paths.rend(); ++pit) {
237 m_jsonnet.addImportPath(boost::filesystem::canonical(*pit).string());
242 for (
auto& vv : extvar) {
243 m_jsonnet.bindExtVar(vv.first, vv.second);
246 for (
auto& vv : extcode) {
247 m_jsonnet.bindExtCodeVar(vv.first, vv.second);
260 if (filename.empty()) {
263 if (filename[0] ==
'/') {
267 for (
auto pobj : m_load_paths) {
268 boost::filesystem::path full = pobj /
filename;
270 return boost::filesystem::canonical(full).string();
281 +
". Maybe you need to add to WIRECELL_PATH."});
285 if (ext ==
".jsonnet" or ext.empty()) {
287 const bool ok = m_jsonnet.evaluateFile(fname, &output);
289 error(m_jsonnet.lastError());
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());
315 bool ok = m_jsonnet.evaluateSnippet(
"<stdin>", text, &output);
317 error(m_jsonnet.lastError());
std::string evaluate_jsonnet_file(const std::string &filename, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
std::string resolve(const std::string &filename)
Below is a reimplimenatiaon of the above but in class form..... /.
static std::vector< std::string > get_path()
Thrown when an error involving accessing input or output has occurred.
std::string dumps(const Json::Value &top, bool pretty=false)
As above but dump to a JSON text string.
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)
void info(const char *fmt, const Args &...args)
boost::error_info< struct tag_errmsg, std::string > errmsg
#define WIRECELL_PATH_VARNAME
std::map< std::string, std::string > externalvars_t
Parser(const pathlist_t &load_paths=pathlist_t(), const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
bool exists(std::string path)
static std::string file_extension(const std::string &filename)
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
std::string getenv(std::string const &name)
Json::Value load(const std::string &filename, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
bool exists(const std::string &filename)
Return true file exists (no file resolution performed).
Thrown when a wrong value has been encountered.
Json::Value loads(const std::string &text, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
Json::Value load(const std::string &filename)
void dump(const std::string &filename, const Json::Value &top, bool pretty=false)
static void init_parser(jsonnet::Jsonnet &parser, const Persist::externalvars_t &extvar, const Persist::externalvars_t &extcode)
void split(std::string const &s, char c, OutIter dest)
std::string slurp(const std::string &filename)
std::string resolve(const std::string &filename)
Json::Value loads(const std::string &text)
void error(const char *fmt, const Args &...args)