PLearn 0.1
PyPLearnScript.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PyPLearnScript.h
00004 //
00005 // Copyright (C) 2005 Christian Dorion 
00006 // 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************      
00036  * $Id: PyPLearnScript.h 10191 2009-05-07 17:08:25Z nouiz $ 
00037  ******************************************************* */
00038 
00039 // Authors: Christian Dorion
00040 
00044 #ifndef PyPLearnScript_INC
00045 #define PyPLearnScript_INC
00046 
00047 #include <plearn/base/Object.h>
00048 #include <plearn/io/PPath.h>
00049 #include <plearn/io/openString.h>
00050 #include <vector>
00051 
00052 namespace PLearn {
00053 
00054 class PyPLearnScript: public Object
00055 {
00056 public:
00057     // STATIC METHODS
00058 
00059 // Althouth 'pyplearn_driver.py' is supposed to be in the path, it cannot be
00060 // executed by NSPR under Windows. Thus we have to give it to the Python
00061 // executable, which will need the full path to correctly run the script.
00062 #ifdef WIN32
00063 #define PYPLEARN_DRIVER_PATH \
00064     PPath("PLEARNDIR:scripts/pyplearn_driver.py").absolute()
00065 #else
00066 #define PYPLEARN_DRIVER_PATH "pyplearn_driver.py"
00067 #endif
00068 
00077     static PP<PyPLearnScript> process(
00078         const std::string& filename,
00079         const std::vector<std::string>& args = std::vector<std::string>(),
00080         const std::string& drivername        = PYPLEARN_DRIVER_PATH );
00081 
00086     static PStream openScriptFile(
00087         int argc, char** argv,
00088         const std::string& drivername = PYPLEARN_DRIVER_PATH );
00089 
00096     template<class Obj>
00097     static PP<Obj> load(
00098         const std::string& filename,
00099         const std::vector<std::string>& args = std::vector<std::string>(),
00100         const std::string& drivername        = PYPLEARN_DRIVER_PATH )
00101     {
00102         PP<PyPLearnScript> script = PyPLearnScript::process(filename, args, drivername);
00103 
00104         string plearn_script_string = script->plearn_script;
00105         PStream in = openString( plearn_script_string, PStream::plearn_ascii );
00106         PP<Obj> o  = new Obj();
00107         in >> o;
00108 
00109         return o;
00110     }
00111 
00112  
00113 private:
00114     typedef Object inherited;
00115 
00117     void build_();
00118 
00119 protected:
00121     static void declareOptions(OptionList& ol);
00122 
00123 public:
00124 
00125     // ************************
00126     // * public build options *
00127     // ************************
00128 
00130     std::string plearn_script;
00131   
00133     std::map<std::string, std::string> vars;
00134 
00136     bool do_help;
00137 
00139     bool do_dump;  
00140 
00145     std::string metainfos;
00146 
00148     PPath expdir;
00149 
00151     int verbosity;
00152 
00154     vector<string> module_names;
00155 
00156     // ****************
00157     // * Constructors *
00158     // ****************
00159 
00161     PyPLearnScript();
00162 
00164     virtual void run();
00165 
00167     const string& getScript() { return plearn_script; }
00168 
00170     void close();
00171   
00172     // Declares other standard object methods.
00173     PLEARN_DECLARE_OBJECT(PyPLearnScript);
00174 
00175     // simply calls inherited::build() then build_() 
00176     virtual void build();
00177 
00179     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00180 };
00181 
00182 // Declares a few other classes and functions related to this class
00183 DECLARE_OBJECT_PTR(PyPLearnScript);
00184 
00185 
00196 Object* smartLoadObject(PPath filepath, const vector<string>& args, time_t& return_date);
00197 
00199 inline Object* smartLoadObject(PPath filepath, const vector<string>& args)
00200 { time_t d=0; return smartLoadObject(filepath, args, d); }
00202 inline Object* smartLoadObject(PPath filepath, time_t& return_date)
00203 { vector<string> args; args.push_back("");/* empty script filename*/ return smartLoadObject(filepath, args,return_date); }
00206 inline Object* smartLoadObject(PPath filepath)
00207 {time_t d=0;return smartLoadObject(filepath, d);}
00208 
00209   
00210 } // end of namespace PLearn
00211 
00212 #endif
00213 
00214 
00215 /*
00216   Local Variables:
00217   mode:c++
00218   c-basic-offset:4
00219   c-file-style:"stroustrup"
00220   c-file-offsets:((innamespace . 0)(inline-open . 0))
00221   indent-tabs-mode:nil
00222   fill-column:79
00223   End:
00224 */
00225 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines