pythonmodule.cpp
1/***************************************************************************
2 * pythonmodule.cpp
3 * This file is part of the KDE project
4 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 ***************************************************************************/
19
20#include "pythonmodule.h"
21#include "pythoninterpreter.h"
22
23#include <tqregexp.h>
24
25using namespace Kross::Python;
26
27namespace Kross { namespace Python {
28
30 class PythonModulePrivate
31 {
32 public:
33
38 PythonInterpreter* m_interpreter;
39
44 TQMap<TQString, PythonExtension*> m_modules;
45
46 };
47
48}}
49
51 : Py::ExtensionModule<PythonModule>("__main__")
52 , d(new PythonModulePrivate())
53{
54#ifdef KROSS_PYTHON_MODULE_DEBUG
55 krossdebug( TQString("Kross::Python::PythonModule::Constructor") );
56#endif
57
58 d->m_interpreter = interpreter;
59
60 add_varargs_method("_import", &PythonModule::import, "FIXME: Documentation");
61
62 initialize("The PythonModule is the __main__ python environment used as global object namespace.");
63}
64
66{
67#ifdef KROSS_PYTHON_MODULE_DEBUG
68 krossdebug( TQString("Kross::Python::PythonModule::Destructor name='%1'").arg(name().c_str()) );
69#endif
70
71 delete d;
72}
73
75{
76 return moduleDictionary();
77}
78
79Py::Object PythonModule::import(const Py::Tuple& args)
80{
81 if(args.size() > 0) {
82 TQString modname = args[0].as_string().c_str();
83 if(modname.startsWith("kross")) {
84#ifdef KROSS_PYTHON_MODULE_DEBUG
85 krossdebug( TQString("Kross::Python::PythonModule::import() module=%1").arg(modname) );
86#endif
87 if( modname.find( TQRegExp("[^a-zA-Z0-9\\_\\-]") ) >= 0 ) {
88 krosswarning( TQString("Denied import of Kross module '%1' cause of untrusted chars.").arg(modname) );
89 }
90 else {
91 Kross::Api::Module::Ptr module = Kross::Api::Manager::scriptManager()->loadModule(modname);
92 if(module)
93 return PythonExtension::toPyObject( Kross::Api::Object::Ptr(module) );
94 krosswarning( TQString("Loading of Kross module '%1' failed.").arg(modname) );
95 }
96
97 }
98 }
99 return Py::None();
100}
TDESharedPtr< Module > Ptr
Shared pointer to implement reference-counting.
Definition module.h:41
TDESharedPtr< Object > Ptr
Shared pointer to implement reference-counting.
Definition object.h:57
Python interpreter bridge.
The PythonModule is the main python environment used as global object namespace.
PythonModule(PythonInterpreter *interpreter)
Constructor.
virtual ~PythonModule()
Destructor.
The Python plugin for the Kross scripting framework.
The Kross scripting bridge to embed scripting functionality into an application.
Definition callable.h:31
void krossdebug(const TQString &s)
Debugging function.
void krosswarning(const TQString &s)
Warning function.