Commits

Henning Schröder committed 55e26c9

clean up

  • Participants
  • Parent commits cd8a447

Comments (0)

Files changed (7)

 from PyQt4.QtGui import qApp
 from PyQt4.QtCore import QMetaObject
 
-
 path = os.path.dirname(os.path.abspath(__file__))
 if path not in sys.path:
 	sys.path.insert(0, path)
 
-
+from console import Console
 from pyqtproxy import class_from_metaobject, wrap_instance
 
-namespace = {}
+
 
 embpyqt = wrap_instance(qApp.property("embedded_pyqt").toPyObject())
-namespace.update(embpyqt=embpyqt)
 
-from console import Console
-embpyqt_console = Console(namespace)
 
-
-def get_class(name):
+def class_by_name(name):
 	ptr = embpyqt.metaObjectByName(name)
 	mo = sip.wrapinstance(int(ptr), QMetaObject)
 	return class_from_metaobject(mo)
 
 
-print "embpyqt:", embpyqt, dir(embpyqt)
 
-Test = get_class("Test")
-namespace.update(Test=Test)
+namespace = {}
+embpyqt_console = wrap_instance(Console(namespace))
 
-print "Test:", Test, dir(Test)
-t = Test()
-t.test()
-print t.calc(1,2)
 
-from pyqtstubs import StubCreator
-stubs = StubCreator()
-stubs.add_class(embpyqt.__class__)
-stubs.add_class(t)
-print stubs
+namespace.update(
+   embpyqt_console=embpyqt_console, 
+   embpyqt=embpyqt)
 
-embpyqt.setException("foo", "baz")
 
+for class_name in embpyqt.availableClasses():
+	cls = class_by_name(class_name)
+	class_name = str(class_name)
+	namespace[class_name] = cls
 
-print "done"
+
+for k, v in namespace.items():
+	globals()[k] = v
+
+__all__ = namespace.keys()
+
+print __file__, "loaded with", __all__

embpyqt/embeddedpyqt.cpp

 #include <QPushButton>
 #include <qdebug.h>
 
-#include "test.h"
 
 EmbeddedPyQt::EmbeddedPyQt(QObject *parent) :
     QObject(parent)
 {
     setObjectName("embeddedPyQt");
-    //REGISTER(QObject);
-    //EmbeddedPyQt::instance = this;
-    classes["Test"] = &Test::staticMetaObject;
-
-
+    QApplication::instance()->setProperty("embedded_pyqt",
+                                          qVariantFromValue((QObject*)this));
     python = new Pythonize();
     Q_ASSERT(python);
-    QApplication::instance()->setProperty("embedded_pyqt",
-                                          qVariantFromValue((QObject*)this));
-    python->runScript("embeddedpyqt.py");
+}
+
+
+void EmbeddedPyQt::init(const QString &filename) {
+    char *cfn = (char*)filename.toLocal8Bit().constData();
+    python->runScript(cfn);
     //python->appendToSysPath("");
     //python->runString("import embeddedpyqt");
 }
 }
 
 
-void EmbeddedPyQt::setException(const QString &type, const QString &message) {
+void EmbeddedPyQt::raiseException(const QString &type, const QString &message) {
     QApplication::instance()->setProperty("embedded_pyqt_exception_type",
                                          qVariantFromValue(type));
     QApplication::instance()->setProperty("embedded_pyqt_exception_msg",
 
 
 void EmbeddedPyQt::clearException() {
-    setException(NULL, NULL);
+    raiseException(NULL, NULL);
 }
 
 void* EmbeddedPyQt::metaObjectByName(const QString &name) {
     return (void*)classes[name];
 }
 
+
+QStringList EmbeddedPyQt::availableClasses() {
+    return classes.keys();
+}
+
+void EmbeddedPyQt::registerMetaObject(const QMetaObject &mo) {
+    classes[mo.className()] = &mo;
+}

embpyqt/embeddedpyqt.h

 #define EMBEDDEDPYQT_H
 
 #include <QObject>
+#include <QMetaObject>
 #include <QMap>
+#include <QStringList>
 #include "pythonize.h"
 
 
-#define RAISE(type,msg) EmbeddedPyQt::instance->setException(type, msg)
-#define REGISTER(type) EmbeddedPyQt::instance->classes[type::staticMetaObject.className()] = &type::staticMetaObject
+#define RAISE(instance, type,msg) instance->setException(type, msg)
 
 
 class EmbeddedPyQt : public QObject
 public:
     explicit EmbeddedPyQt(QObject *parent = 0);
     static EmbeddedPyQt *instance;
-    QMap<QString,const QMetaObject*> classes;
+    void init(const QString &filename);
 
 signals:
 
 public slots:
     void deleteLater();
-    void setException(const QString &type, const QString &message);
+    void raiseException(const QString &type, const QString &message);
     void clearException();
     void* metaObjectByName(const QString &name);
-
+    QStringList availableClasses();
+    void registerMetaObject(const QMetaObject &mo);
 private:
     Pythonize *python;
+    QMap<QString,const QMetaObject*> classes;
 
 };
 
 
-
 #endif // EMBEDDEDPYQT_H

embpyqt/pythonize.cpp

 
 #include "pythonize.h"
 
-#define debug 1
+#define debug 0
 
 ObjectRef::ObjectRef (ObjectRef *oi, PyObject *o)
 {
     }
 }
 
-bool Pythonize::runScript (char *scriptPath)
+bool Pythonize::runScript (const char *scriptPath)
 {
     FILE *f;
     int res;
     return res;
 }
 
-bool Pythonize::runString (char *str)
+bool Pythonize::runString (const char *str)
 {
 
     if (str == NULL || strlen (str) == 0) return false;
     return res == 0;
 }
 
-PyObject *Pythonize::importModule (char *moduleName)
+PyObject *Pythonize::importModule (const char *moduleName)
 {
     if (moduleName == NULL || strlen (moduleName) == 0) return NULL;
 

embpyqt/pythonize.h

 
     // imports a module into the interpreter
     // or gets a PyObject for an already loaded module
-    PyObject *importModule (char *moduleName);
+    PyObject *importModule (const char *moduleName);
 
     void raiseException(const char *msg);
     // returns an object from a loaded module
     }
 
     // runs a script on the current sys.path
-    bool runScript (char *scriptPath);
+    bool runScript (const char *scriptPath);
 
     // executes a string of Python in the interpreter
-    bool runString (char *str);
+    bool runString (const char *str);
 
     // runs a callable Python object
     PyObject *runFunction(PyObject *object, PyObject *args);
     ObjectRef *objects;     // a stack of PyObjects (used in destructor)
 };
 
-extern "C" {
-    Pythonize *initialize();
-    void finalize();
-
-    // adds a path to sys.path
-    bool appendToSysPath (const char* newPath);
-
-    // imports a module into the interpreter
-    // or gets a PyObject for an already loaded module
-    PyObject *importModule (char *moduleName);
-
-    // returns an object from a loaded module
-    // you must decref the object returned when done with it (new reference returned)
-    PyObject *getNewObjectRef (PyObject *module, char *object);
-
-    bool getPythonInit();
-
-    // decrements the ref count of an object
-    void decref (PyObject *object);
-
-    // runs a script on the current sys.path
-    bool runScript (char *scriptPath);
-
-    // executes a string of Python in the interpreter
-    bool runString (char *str);
-
-    // runs a callable Python object
-    PyObject *runFunction (PyObject *object, PyObject *args);
-}
 
 #endif
 #include <QtGui/QApplication>
 #include "mainwindow.h"
 #include "embpyqt/embeddedpyqt.h"
+#include "test.h"
+
 
 int main(int argc, char *argv[])
 {
 
     EmbeddedPyQt *embpyqt;
     embpyqt = new EmbeddedPyQt();
-
+    embpyqt->registerMetaObject(Test::staticMetaObject);
+    embpyqt->init("embeddedpyqt.py");
 
     return a.exec();
 }
 # -*- coding: utf-8 -*-
 import sys
 
-from PyQt4.QtCore import QMetaMethod, pyqtSlot, QObject, QVariant, QString
+from PyQt4.QtCore import QMetaMethod, pyqtSlot, QObject, QVariant, QString, QStringList
 
 
+def wrap_value(value):
+    if isinstance(value, QObject):
+        return wrap_instance(value)
+    elif isinstance(value, QString):
+        return unicode(value)
+    elif isinstance(value, QStringList):
+        return [unicode(v) for v in value]
+    elif isinstance(value, dict):
+        d = {}
+        for k, v in value.items():
+            d[unicode(v)] = wrap_value(v)
+        return d
+    elif isinstance(value, list):
+        return [wrap_value(v) for v in value]
+    else:
+        return value
+
+        
+
 
 
 class QtMethodCall(QObject):
         method = self._matching_method_for_signature(arg_types)
         call = getattr(self.obj._qt, method.name)
         result = call(*args)
-        if isinstance(result, QObject):
-            return wrap_instance(result)
-        elif isinstance(result, QString):
-            return unicode(result)
-        return result
-
+        return wrap_value(result)
 
 
     @pyqtSlot()
         if isinstance(obj, PyQtClass):
             obj = obj._qt
         value = self._mp.read(obj)
-        if isinstance(value, QObject):
-            return wrap_instance(value)
-        elif isinstance(value, QString):
-            return unicode(value)
-        return value
+        return wrap_value(value)
 
     
     def __set__(self, obj, value):
         qname = name[0].lower() + name[1:]
         if qname in pnames:
             value = self._qt.property(qname).toPyObject()
-            if isinstance(value, QObject):
-                return wrap_instance(value)
-            elif isinstance(result, QString):
-                return unicode(result)
-            return result
+            return wrap_value(value)
 
         # access child objects as attribute
         for child in self._qt.children():