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 
00025 using namespace Kross::Python;
00026 
00027 namespace Kross { namespace Python {
00028 
00030     class PythonModulePrivate
00031     {
00032         public:
00033 
00038             PythonInterpreter* m_interpreter;
00039 
00044             QMap<QString, PythonExtension*> m_modules;
00045 
00046     };
00047 
00048 }}
00049 
00050 PythonModule::PythonModule(PythonInterpreter* interpreter)
00051     : Py::ExtensionModule<PythonModule>("__main__")
00052     , d(new PythonModulePrivate())
00053 {
00054 #ifdef KROSS_PYTHON_MODULE_DEBUG
00055     krossdebug( QString("Kross::Python::PythonModule::Constructor") );
00056 #endif
00057 
00058     d->m_interpreter = interpreter;
00059 
00060     add_varargs_method("_import", &PythonModule::import, "FIXME: Documentation");
00061 
00062     initialize("The PythonModule is the __main__ python environment used as global object namespace.");
00063 }
00064 
00065 PythonModule::~PythonModule()
00066 {
00067 #ifdef KROSS_PYTHON_MODULE_DEBUG
00068     krossdebug( QString("Kross::Python::PythonModule::Destructor name='%1'").arg(name().c_str()) );
00069 #endif
00070 
00071     delete d;
00072 }
00073 
00074 Py::Dict PythonModule::getDict()
00075 {
00076     return moduleDictionary();
00077 }
00078 
00079 Py::Object PythonModule::import(const Py::Tuple& args)
00080 {
00081     if(args.size() > 0) {
00082         QString modname = args[0].as_string().c_str();
00083         if(modname.startsWith("kross")) {
00084 #ifdef KROSS_PYTHON_MODULE_DEBUG
00085             krossdebug( QString("Kross::Python::PythonModule::import() module=%1").arg(modname) );
00086 #endif
00087             if( modname.find( QRegExp("[^a-zA-Z0-9\\_\\-]") ) >= 0 ) {
00088                 krosswarning( QString("Denied import of Kross module '%1' cause of untrusted chars.").arg(modname) );
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                 krosswarning( QString("Loading of Kross module '%1' failed.").arg(modname) );
00095             }
00096 
00097         }
00098     }
00099     return Py::None();
00100 }
KDE Home | KDE Accessibility Home | Description of Access Keys