summaryrefslogtreecommitdiffstats
path: root/lib/kross/python/pythonmodule.cpp
blob: b54eb73ff82a2efc92313d0ad3dbbb16a13bd3e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***************************************************************************
 * pythonmodule.cpp
 * This file is part of the KDE project
 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#include "pythonmodule.h"
#include "pythoninterpreter.h"

#include <qregexp.h>

using namespace Kross::Python;

namespace Kross { namespace Python {

    /// @internal
    class PythonModulePrivate
    {
        public:

            /**
             * The \a PythonInterpreter instance this module is
             * part of.
             */
            PythonInterpreter* m_interpreter;

            /**
             * List of \a PythonExtension instances accessible
             * via this \a PythonModule instance.
             */
            QMap<QString, PythonExtension*> m_modules;

    };

}}

PythonModule::PythonModule(PythonInterpreter* interpreter)
    : Py::ExtensionModule<PythonModule>("__main__")
    , d(new PythonModulePrivate())
{
#ifdef KROSS_PYTHON_MODULE_DEBUG
    krossdebug( QString("Kross::Python::PythonModule::Constructor") );
#endif

    d->m_interpreter = interpreter;

    add_varargs_method("_import", &PythonModule::import, "FIXME: Documentation");

    initialize("The PythonModule is the __main__ python environment used as global object namespace.");
}

PythonModule::~PythonModule()
{
#ifdef KROSS_PYTHON_MODULE_DEBUG
    krossdebug( QString("Kross::Python::PythonModule::Destructor name='%1'").arg(name().c_str()) );
#endif

    delete d;
}

Py::Dict PythonModule::getDict()
{
    return moduleDictionary();
}

Py::Object PythonModule::import(const Py::Tuple& args)
{
    if(args.size() > 0) {
        QString modname = args[0].as_string().c_str();
        if(modname.startsWith("kross")) {
#ifdef KROSS_PYTHON_MODULE_DEBUG
            krossdebug( QString("Kross::Python::PythonModule::import() module=%1").arg(modname) );
#endif
            if( modname.find( QRegExp("[^a-zA-Z0-9\\_\\-]") ) >= 0 ) {
                krosswarning( QString("Denied import of Kross module '%1' cause of untrusted chars.").arg(modname) );
            }
            else {
                Kross::Api::Module::Ptr module = Kross::Api::Manager::scriptManager()->loadModule(modname);
                if(module)
                    return PythonExtension::toPyObject( Kross::Api::Object::Ptr(module) );
                krosswarning( QString("Loading of Kross module '%1' failed.").arg(modname) );
            }

        }
    }
    return Py::None();
}