PLearn 0.1
Public Member Functions | Static Protected Attributes
PLearn::RunCommand Class Reference

#include <RunCommand.h>

Inheritance diagram for PLearn::RunCommand:
Inheritance graph
[legend]
Collaboration diagram for PLearn::RunCommand:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 RunCommand ()
virtual void run (const vector< string > &args)
 The actual implementation of the 'RunCommand' command.

Static Protected Attributes

static PLearnCommandRegistry reg_
 This allows to register the 'RunCommand' command in the command registry.

Detailed Description

Definition at line 50 of file RunCommand.h.


Constructor & Destructor Documentation

PLearn::RunCommand::RunCommand ( ) [inline]

Definition at line 53 of file RunCommand.h.

                :
        PLearnCommand("run",

                      "runs a .plearn script",

                      "run <script.plearn> [ argname1=val argname2=val ... ] \n"
                      "Runs a .plearn script, with the arguments defined as\n"
                      "macro variables, accessible from within the script as ${argname} \n"
                      "'run' is what gets called when you give NO command name, but a valid .plearn script \n"
                      "Lookup plearn help scripts for more info on scripts. \n"
            ) 
    {}

Member Function Documentation

void PLearn::RunCommand::run ( const vector< string > &  args) [virtual]

The actual implementation of the 'RunCommand' command.

Implements PLearn::PLearnCommand.

Definition at line 63 of file RunCommand.cc.

References std::copy(), PLearn::PL_Log::enableNamedLogging(), PLearn::PPath::extension(), PLearn::PStream::good(), i, in, PLearn::PL_Log::instance(), PLearn::isfile(), PLearn::PP< T >::isNotNull(), PLearn::openFile(), PLearn::openString(), PLearn::parseBaseAndParameters(), PLearn::PStream::plearn_ascii, PLERROR, PLearn::PyPLearnScript::process(), PLearn::readFileAndMacroProcess(), PLearn::readObject(), PLearn::PStream::skipBlanksAndCommentsAndSeparators(), PLearn::split_on_first(), and PLearn::PL_Log::verbosity().

Referenced by PLearn::AutoRunCommand::run().

{
    const vector<string>* the_args = &args;
    vector<string> args_augmented;
    PPath scriptfile = args[0];
    if (!isfile(scriptfile)) {
        // There is no file with this exact name. Maybe there are parameters
        // appended to the name?
        string base;
        map<string, string> params;
        parseBaseAndParameters(scriptfile, base, params);
        if (!isfile(base))
            PLERROR("Non-existent script file: %s\n",scriptfile.c_str());
        // Add new arguments.
        args_augmented = args;
        map<string, string>::const_iterator it = params.begin();
        for (; it != params.end(); it++)
            args_augmented.push_back(it->first + "=" + it->second);
        the_args = &args_augmented;
        scriptfile = base;
    }

    string extension = scriptfile.extension();
    string script;

    PP<PyPLearnScript> pyplearn_script;
    PStream in;

    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(the_args->size()-1);
        copy(the_args->begin() + 1, the_args->end(), pyplearn_args.begin());
    
        pyplearn_script = PyPLearnScript::process(scriptfile, pyplearn_args);
        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 == "" )
            return;    

        PL_Log::instance().enableNamedLogging(pyplearn_script->module_names);
        PL_Log::instance().verbosity(pyplearn_script->verbosity);

        in = openString( script, PStream::plearn_ascii );
    }
    else if(extension=="plearn")  // perform plearn macro expansion
    {
        map<string, string> vars;
        // populate vars with the arguments passed on the command line
        for (unsigned int i=1; i<the_args->size(); i++)
        {
            string option = (*the_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;
            }
        }

        script = readFileAndMacroProcess(scriptfile, vars);
        in = openString( script, PStream::plearn_ascii );
    }
    else if(extension=="psave") // do not perform plearn macro expansion
    {
        in = openFile(scriptfile, PStream::plearn_ascii);
    }
    else
        PLERROR("Invalid extension for script file. Must be one of .pyplearn .plearn .psave");

    while ( in.good() )
    {
        PP<Object> o = readObject(in);
        o->run();
        in.skipBlanksAndCommentsAndSeparators();
    }

    if ( pyplearn_script.isNotNull() )
        pyplearn_script->close();
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

This allows to register the 'RunCommand' command in the command registry.

Definition at line 69 of file RunCommand.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines