PLearn 0.1
|
#include <PyPLearnScript.h>
Public Member Functions | |
PyPLearnScript () | |
Default constructor. | |
virtual void | run () |
Runs the embedded runnable object. | |
const string & | getScript () |
Returns the internal script representation. | |
void | close () |
Saving metainfos to the expdir. | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual PyPLearnScript * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Post-constructor. | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
Static Public Member Functions | |
static PP< PyPLearnScript > | process (const std::string &filename, const std::vector< std::string > &args=std::vector< std::string >(), const std::string &drivername=PYPLEARN_DRIVER_PATH) |
static PStream | openScriptFile (int argc, char **argv, const std::string &drivername=PYPLEARN_DRIVER_PATH) |
template<class Obj > | |
static PP< Obj > | load (const std::string &filename, const std::vector< std::string > &args=std::vector< std::string >(), const std::string &drivername=PYPLEARN_DRIVER_PATH) |
static string | _classname_ () |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
Public Attributes | |
std::string | plearn_script |
The plearn_script. | |
std::map< std::string, std::string > | vars |
Variables set at command line. | |
bool | do_help |
Script is in fact an help message. | |
bool | do_dump |
Dump the script and forget it. | |
std::string | metainfos |
PPath | expdir |
The expdir of the experiment described by the script. | |
int | verbosity |
The verbosity level to set for this experiment. | |
vector< string > | module_names |
Modules for which logging should be activated. | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Private Types | |
typedef Object | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Definition at line 54 of file PyPLearnScript.h.
typedef Object PLearn::PyPLearnScript::inherited [private] |
Reimplemented from PLearn::Object.
Definition at line 114 of file PyPLearnScript.h.
PLearn::PyPLearnScript::PyPLearnScript | ( | ) |
Default constructor.
Definition at line 202 of file PyPLearnScript.cc.
: plearn_script(""), vars(), do_help(false), do_dump(false), metainfos(""), expdir(""), verbosity(PL_Log::instance().verbosity()), module_names(PL_Log::instance().namedLogging()) {}
string PLearn::PyPLearnScript::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 219 of file PyPLearnScript.cc.
OptionList & PLearn::PyPLearnScript::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 219 of file PyPLearnScript.cc.
RemoteMethodMap & PLearn::PyPLearnScript::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 219 of file PyPLearnScript.cc.
Reimplemented from PLearn::Object.
Definition at line 219 of file PyPLearnScript.cc.
Object * PLearn::PyPLearnScript::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 219 of file PyPLearnScript.cc.
StaticInitializer PyPLearnScript::_static_initializer_ & PLearn::PyPLearnScript::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 219 of file PyPLearnScript.cc.
void PLearn::PyPLearnScript::build | ( | ) | [virtual] |
Post-constructor.
The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.
Reimplemented from PLearn::Object.
Definition at line 315 of file PyPLearnScript.cc.
References PLearn::Object::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::PyPLearnScript::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::Object.
Definition at line 298 of file PyPLearnScript.cc.
References do_dump, do_help, PLearn::endl(), expdir, PLearn::openString(), plearn_script, PLearn::pout, PLearn::PStream::raw_ascii, PLearn::readAndMacroProcess(), and vars.
Referenced by build().
{ if ( do_help || do_dump ) { pout << plearn_script << endl; plearn_script = ""; expdir = ""; // Disabling the close() method. } else { PStream is = openString( plearn_script, PStream::raw_ascii ); time_t latest = 0 ; plearn_script = readAndMacroProcess( is, vars, latest ); } }
string PLearn::PyPLearnScript::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 219 of file PyPLearnScript.cc.
void PLearn::PyPLearnScript::close | ( | ) |
Saving metainfos to the expdir.
Definition at line 278 of file PyPLearnScript.cc.
References PLearn::endl(), expdir, PLearn::isdir(), PLearn::PPath::isEmpty(), metainfos, PLearn::openFile(), PLearn::pl_repository_revision(), plearn_script, and PLearn::PStream::raw_ascii.
Referenced by run().
{ if ( !expdir.isEmpty() ) { if ( ! isdir( expdir ) ) { // TODO Why should the expdir be systematically created? // PLERROR( "The expdir '%s' managed by this PyPLearnScript was not created.", // expdir.c_str() ); } else { PStream out = openFile( expdir / "metainfos.txt", PStream::raw_ascii, "w" ); out << "__REVISION__ = " << pl_repository_revision() << endl; out << metainfos << endl; out = openFile( expdir / "experiment.plearn", PStream::raw_ascii, "w" ); out << plearn_script << endl; } } }
void PLearn::PyPLearnScript::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::Object.
Definition at line 238 of file PyPLearnScript.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::Object::declareOptions(), do_dump, do_help, expdir, metainfos, module_names, plearn_script, vars, and verbosity.
{ declareOption( ol, "plearn_script", &PyPLearnScript::plearn_script, OptionBase::buildoption, "The plearn_script" ); declareOption( ol, "vars", &PyPLearnScript::vars, OptionBase::buildoption, "Variables set at command line" ); declareOption( ol, "do_help", &PyPLearnScript::do_help, OptionBase::buildoption, "Script is in fact an help message" ); declareOption( ol, "do_dump", &PyPLearnScript::do_dump, OptionBase::buildoption, "Dump the script and forget it" ); declareOption( ol, "metainfos", &PyPLearnScript::metainfos, OptionBase::buildoption, "Informations relative to the experiment settings, to be wrote in an\n" "expdir file." ); declareOption( ol, "expdir", &PyPLearnScript::expdir, OptionBase::buildoption, "The expdir of the experiment described by the script" ); declareOption( ol, "verbosity", &PyPLearnScript::verbosity, OptionBase::buildoption, "The verbosity level to set for this experiment" ); declareOption( ol, "module_names", &PyPLearnScript::module_names, OptionBase::buildoption, "Modules for which logging should be activated" ); // Now call the parent class' declareOptions inherited::declareOptions(ol); }
static const PPath& PLearn::PyPLearnScript::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::Object.
Definition at line 173 of file PyPLearnScript.h.
{ time_t d=0; return smartLoadObject(filepath, args, d); }
PyPLearnScript * PLearn::PyPLearnScript::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 219 of file PyPLearnScript.cc.
OptionList & PLearn::PyPLearnScript::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 219 of file PyPLearnScript.cc.
OptionMap & PLearn::PyPLearnScript::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 219 of file PyPLearnScript.cc.
RemoteMethodMap & PLearn::PyPLearnScript::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 219 of file PyPLearnScript.cc.
const string& PLearn::PyPLearnScript::getScript | ( | ) | [inline] |
Returns the internal script representation.
Definition at line 167 of file PyPLearnScript.h.
References plearn_script.
{ return plearn_script; }
static PP<Obj> PLearn::PyPLearnScript::load | ( | const std::string & | filename, |
const std::vector< std::string > & | args = std::vector<std::string>() , |
||
const std::string & | drivername = PYPLEARN_DRIVER_PATH |
||
) | [inline, static] |
This static method forwards its arguments to process() and returns a pointer on an object of template type Obj by loading the resulting plearn script. Note that the object IS NOT BUILT since one may want to set other options prior to calling build().
Definition at line 97 of file PyPLearnScript.h.
References in, PLearn::openString(), PLearn::PStream::plearn_ascii, and process().
{ PP<PyPLearnScript> script = PyPLearnScript::process(filename, args, drivername); string plearn_script_string = script->plearn_script; PStream in = openString( plearn_script_string, PStream::plearn_ascii ); PP<Obj> o = new Obj(); in >> o; return o; }
void PLearn::PyPLearnScript::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::Object.
Definition at line 321 of file PyPLearnScript.cc.
References PLearn::Object::makeDeepCopyFromShallowCopy().
{ inherited::makeDeepCopyFromShallowCopy(copies); }
PStream PLearn::PyPLearnScript::openScriptFile | ( | int | argc, |
char ** | argv, | ||
const std::string & | drivername = PYPLEARN_DRIVER_PATH |
||
) | [static] |
Given a filename, does a job similar to process() but parses argc
& argv
as given to a main(). Returns a PStream valid for reading an object.
Definition at line 168 of file PyPLearnScript.cc.
References std::copy(), PLearn::extract_extension(), PLearn::isfile(), PLearn::openString(), PLearn::PStream::plearn_ascii, PLERROR, PLearn::prgname(), process(), and PLearn::stringvector().
{ prgname(argv[0]); // set program name vector<string> command_line = stringvector(argc-1, argv+1); // neglecting progname string scriptfile = command_line[0]; if (!isfile(scriptfile)) PLERROR("Non-existent script file: %s\n",scriptfile.c_str()); const string extension = extract_extension(scriptfile); string script; if (extension == ".pyplearn") { // Make a copy of args with the first argument (the name of the script) // removed, leaving the first argument to the script at index 0. vector<string> pyplearn_args(command_line.size()-1); copy(command_line.begin() + 1, command_line.end(), pyplearn_args.begin()); PP<PyPLearnScript> pyplearn_script = PyPLearnScript::process(scriptfile, pyplearn_args, drivername); script = pyplearn_script->getScript(); // When we call the pyplearn script with either // --help or --dump, everything will already have been done by // the time the PyPLearnScript is built. if ( script == "" ) exit(0); } else PLERROR("PyPLearnScript::openScript -- Expecting a .pyplearn file."); return openString(script, PStream::plearn_ascii); }
PP< PyPLearnScript > PLearn::PyPLearnScript::process | ( | const std::string & | filename, |
const std::vector< std::string > & | args = std::vector<std::string>() , |
||
const std::string & | drivername = PYPLEARN_DRIVER_PATH |
||
) | [static] |
Given a filename, call an external process with the given name (default = "pyplearn_driver.py") to preprocess it and return the preprocessed version. Arguments to the subprocess can be passed. Passing '--help' passes it unmodified to the subprogram and is assumed to print a help string. If '--dump' is passed, the Python-preprocessed script is printed to standard output.
Definition at line 60 of file PyPLearnScript.cc.
References PLearn::addFileAndDateVariables(), i, PLearn::Popen::in, PLearn::PL_Log::instance(), PLearn::join(), PLearn::split_on_first(), and PLearn::tostring().
Referenced by PLearn::getDataSet(), load(), openScriptFile(), PLearn::RunCommand::run(), and PLearn::smartLoadObject().
{ bool do_help = false; bool do_dump = false; // List of arguments passed to the pyplearn_driver.py script. vector<string> final_args; // The vars map is for PLearn arguments (i.e. arguments accessed through // the ${varname} syntax. map<string, string> vars; #ifdef WIN32 // Under Windows, we must specify that the driver needs to be run with // Python, thus the driver becomes an argument. final_args.push_back(drivername); #endif final_args.push_back(scriptfile); // This option is understood by the pyplearn driver so that is returns a // plearn representation of a PyPLearnScript object. final_args.push_back("--PyPLearnScript"); // For pyplearn files, we support the following command-line // arguments: --help to print the doc string of the .pyplearn file, // and --dump to show the result of processing the .pyplearn file // instead of running it. if (args.size() >= 1) { if (args[0] == "--help") { do_help = true; } else if (args[0] == "--dump") { do_dump = true; } } if (do_help) { final_args.push_back("--help"); } else { // Supply the command-line arguments to the pylearn driver // script. for (unsigned int i = 0; i < args.size(); i++) { string option = args[i]; // Skip --foo command-lines options. if ( option.size() < 2 || option.substr(0, 2) != "--" ) { final_args.push_back(args[i]); } } // Add the standard PLearn variables (DATE, DATETIME, etc.) to the // list of variables used when runing the PLearn preprocessor on the // output of the .pyplearn file to handle $DATE, etc. // in PLearn strings. addFileAndDateVariables(scriptfile, vars, 0); // Also add these variables (DATE, etc.) to the pyplearn driver // script so they show up as pyplearn arguments too. for (map<string, string>::const_iterator it = vars.begin(); it != vars.end(); ++it) { final_args.push_back(it->first + '=' + it->second); } // Add the command-line arguments to the list of PLearn arguments. for (unsigned int i = 0; i < args.size(); i++) { string option = args[i]; // Skip --foo command-lines options. if (option.size() < 2 || option.substr(0, 2) != "--") { pair<string,string> name_val = split_on_first(option, "="); vars[name_val.first] = name_val.second; } } } final_args.push_back(string("--enable-logging=") + join(PL_Log::instance().namedLogging(), ",")); final_args.push_back(string("--verbosity=") + tostring(PL_Log::instance().verbosity())) ; #ifdef WIN32 Popen popen("python.exe", final_args); #else Popen popen(drivername, final_args); #endif PP<PyPLearnScript> pyplearn_script = new PyPLearnScript(); popen.in >> *pyplearn_script; pyplearn_script->vars = vars; pyplearn_script->do_help = do_help; pyplearn_script->do_dump = do_dump; pyplearn_script->build(); return pyplearn_script; }
void PLearn::PyPLearnScript::run | ( | ) | [virtual] |
Runs the embedded runnable object.
Reimplemented from PLearn::Object.
Definition at line 221 of file PyPLearnScript.cc.
References close(), PLearn::PL_Log::enableNamedLogging(), PLearn::PStream::good(), in, PLearn::PL_Log::instance(), module_names, PLearn::openString(), PLearn::PStream::plearn_ascii, plearn_script, PLearn::readObject(), PLearn::PStream::skipBlanksAndCommentsAndSeparators(), verbosity, and PLearn::PL_Log::verbosity().
{ PL_Log::instance().enableNamedLogging(module_names); PL_Log::instance().verbosity(verbosity); PStream in = openString( plearn_script, PStream::plearn_ascii ); while ( in.good() ) { PP<Object> o = readObject(in); o->run(); in.skipBlanksAndCommentsAndSeparators(); } close(); }
Reimplemented from PLearn::Object.
Definition at line 173 of file PyPLearnScript.h.
Dump the script and forget it.
Definition at line 139 of file PyPLearnScript.h.
Referenced by build_(), and declareOptions().
Script is in fact an help message.
Definition at line 136 of file PyPLearnScript.h.
Referenced by build_(), and declareOptions().
The expdir of the experiment described by the script.
Definition at line 148 of file PyPLearnScript.h.
Referenced by build_(), close(), and declareOptions().
std::string PLearn::PyPLearnScript::metainfos |
Informations relative to the experiment settings, to be wrote in an expdir file.
Definition at line 145 of file PyPLearnScript.h.
Referenced by close(), and declareOptions().
vector<string> PLearn::PyPLearnScript::module_names |
Modules for which logging should be activated.
Definition at line 154 of file PyPLearnScript.h.
Referenced by declareOptions(), and run().
std::string PLearn::PyPLearnScript::plearn_script |
The plearn_script.
Definition at line 130 of file PyPLearnScript.h.
Referenced by build_(), close(), declareOptions(), getScript(), and run().
std::map<std::string, std::string> PLearn::PyPLearnScript::vars |
Variables set at command line.
Definition at line 133 of file PyPLearnScript.h.
Referenced by build_(), and declareOptions().
The verbosity level to set for this experiment.
Definition at line 151 of file PyPLearnScript.h.
Referenced by declareOptions(), and run().