lib

pythonmodule.cpp

00001 /***************************************************************************
00002  * pythonmodule.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #include "pythonmodule.h"
00021 #include "pythoninterpreter.h"
00022 
00023 #include <qregexp.h>
00024 #include <kdebug.h>
00025 
00026 using namespace Kross::Python;
00027 
00028 namespace Kross { namespace Python {
00029 
00031     class PythonModulePrivate
00032     {
00033         public:
00034 
00039             PythonInterpreter* m_interpreter;
00040 
00045             QMap<QString, PythonExtension*> m_modules;
00046 
00047     };
00048 
00049 }}
00050 
00051 PythonModule::PythonModule(PythonInterpreter* interpreter)
00052     : Py::ExtensionModule<PythonModule>("__main__")
00053     , d(new PythonModulePrivate())
00054 {
00055 #ifdef KROSS_PYTHON_MODULE_DEBUG
00056     kdDebug() << QString("Kross::Python::PythonModule::Constructor") << endl;
00057 #endif
00058 
00059     d->m_interpreter = interpreter;
00060 
00061     add_varargs_method("_import", &PythonModule::import, "FIXME: Documentation");
00062 
00063     initialize("The PythonModule is the __main__ python environment used as global object namespace.");
00064 }
00065 
00066 PythonModule::~PythonModule()
00067 {
00068 #ifdef KROSS_PYTHON_MODULE_DEBUG
00069     kdDebug() << QString("Kross::Python::PythonModule::Destructor name='%1'").arg(name().c_str()) << endl;
00070 #endif
00071 
00072     delete d;
00073 }
00074 
00075 Py::Dict PythonModule::getDict()
00076 {
00077     return moduleDictionary();
00078 }
00079 
00080 Py::Object PythonModule::import(const Py::Tuple& args)
00081 {
00082     if(args.size() > 0) {
00083         QString modname = args[0].as_string().c_str();
00084         if(modname.startsWith("kross")) {
00085             kdDebug() << QString("Kross::Python::PythonModule::import() module=%1").arg(modname) << endl;
00086 
00087             if( modname.find( QRegExp("[^a-zA-Z0-9\\_\\-]") ) >= 0 ) {
00088                 kdWarning() << QString("Denied import of Kross module '%1' cause of untrusted chars.").arg(modname) << endl;
00089             }
00090             else {
00091                 Kross::Api::Module::Ptr module = Kross::Api::Manager::scriptManager()->loadModule(modname);
00092                 if(module)
00093                     return PythonExtension::toPyObject( Kross::Api::Object::Ptr(module) );
00094                 kdWarning() << QString("Loading of Kross module '%1' failed.").arg(modname) << endl;
00095             }
00096 
00097         }
00098     }
00099     return Py::None();
00100 }
KDE Home | KDE Accessibility Home | Description of Access Keys