casa  $Rev:20696$
PythonInterpreter.h
Go to the documentation of this file.
00001 //# PythonInterpreter.h: Interface for connecting and running tasks in Python.
00002 //# Copyright (C) 2005
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: $
00027 #ifndef PYTHONINTERPRETER_H_
00028 #define PYTHONINTERPRETER_H_
00029 
00030 #include <Python.h>
00031 
00032 #include <casa/BasicSL/String.h>
00033 
00034 #include <casa/namespace.h>
00035 
00036 namespace casa {
00037 
00038 // Interface for connecting to and running tasks in Python.
00039 class PythonInterpreter {
00040 public:
00041     // Constructor.  If ipythonShell is NULL, then the connection is in
00042     // standalone mode -- i.e., a new casapy instance will be created.
00043     // If ipythonShell is not NULL, it is assumed to be either an
00044     // IPython.ipapi.IPApi instance (accesible as _ip in casapy) or an
00045     // IPython.iplib.InteractiveShell object (accessible as _ip.IP in casapy).
00046     PythonInterpreter(PyObject* ipythonShell);
00047     
00048     // Destructor.
00049     ~PythonInterpreter();
00050     
00051     // Initialize the connection.  In standalone mode, creates the python
00052     // interpreter, runs casapy, and then connects to it.  showLogger has no
00053     // effect if not in standalone mode.
00054     bool init(bool showLogger = true);
00055     
00056     // Run the given command in python.
00057     void command(String command);
00058     
00059 private:
00060     // Whether the connection is initialized.
00061     bool m_initialized;
00062     
00063     // Whether the connection is standalone mode or not.
00064     bool m_standalone;
00065     
00066     // Handle to the ipython shell or the standalone interpreter.
00067     PyObject* m_shell;
00068     
00069     // Returns the aips path.
00070     static String aipsPath() {
00071 #ifndef AIPSROOT
00072         char* ap = getenv("CASAPATH");
00073         if(ap == NULL) return String();
00074         String path(ap);
00075         unsigned int i = path.find(' ');
00076         if(i >= path.size()) return String();
00077         else return path.substr(0, i);
00078 #else
00079         return AIPSROOT;
00080 #endif
00081     }
00082     
00083     // Returns the casapy python path.
00084     static String casapyPath() {
00085 #ifdef AIPSROOT
00086 #ifdef AIPSARCH
00087 #ifdef PYTHONVER
00088         stringstream ss;
00089         ss << AIPSROOT << '/' << AIPSARCH << "/python/" << PYTHONVER;
00090         return ss.str();
00091 #else
00092         return String();
00093 #endif
00094 #else
00095         return String();
00096 #endif
00097 #else
00098         return String();
00099 #endif
00100     }
00101     
00102     // Returns the python path.
00103     static String pythonPath() {
00104 #ifdef PYTHONROOT
00105         return String(PYTHONROOT);
00106 #else
00107         return String();
00108 #endif
00109     }
00110     
00111     // Returns the location of the casapy.py script.
00112     static String casapyLoc() {
00113         String p = casapyPath();
00114         if(!p.empty()) p += "/casapy.py";
00115         return p;
00116     }
00117 };
00118 
00119 }
00120 
00121 #endif /*PYTHONINTERPRETER_H_*/
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines