From 404e8f96a20b42395c557ab8cd02cfddc5f3640c 2008-09-07 20:46:50 From: ezust Date: 2008-09-07 20:46:50 Subject: [PATCH] Added an example QFileImportInterface which doesn't work yet! git-svn-id: svn://svn.code.sf.net/p/pythonqt/code/trunk@42 ea8d5007-eb21-0410-b261-ccb3ea6e24a9 --- diff --git a/examples/CPPPyWrapperExample/CPPPyWrapperExample.cpp b/examples/CPPPyWrapperExample/CPPPyWrapperExample.cpp index aed81f0..1f01134 100644 --- a/examples/CPPPyWrapperExample/CPPPyWrapperExample.cpp +++ b/examples/CPPPyWrapperExample/CPPPyWrapperExample.cpp @@ -1,9 +1,12 @@ #include #include +#include "QFileImportInterface.h" + int main (int argc, char* argv[]) { QApplication app(argc, argv); PythonQt::init(); PythonQtObjectPtr mainModule = PythonQt::self()->getMainModule(); + mainModule.evalScript(QString("import sys\n")); Q_ASSERT(!mainModule.isNull()); { // evaluate a python file embedded in executable as resource: @@ -17,10 +20,22 @@ int main (int argc, char* argv[]) { // tag goes out of scope, reference count decremented. } qDebug() << "test1"; +/* + { + // Allow the python system path to recognize QFile paths in the sys.path + QFileImportInterface qfii; + // append the Qt resource root directory to the sys.path + mainModule.evalScript("sys.path.append(':')\n"); + mainModule.evalScript("import eyed3tagger\n"); + PythonQtObjectPtr tag = mainModule.evalScript("eyed3tagger.EyeD3Tagger()\n", Py_eval_input); + Q_ASSERT(!tag.isNull()); + tag.call("setFileName", QVariantList() << "t.mp3"); + QVariant fn = tag.call("fileName", QVariantList()); + Q_ASSERT(fn.toString() == QString("t.mp3")); + } + qDebug() << "test2"; */ { // alternative using import and loading it as a real module from sys.path // import sys first - mainModule.evalScript(QString("import sys\n")); - // append the current directory to the sys.path mainModule.evalScript(QString("sys.path.append('%1')\n").arg(QDir::currentPath())); mainModule.evalScript("import eyed3tagger\n"); PythonQtObjectPtr tag = mainModule.evalScript("eyed3tagger.EyeD3Tagger()\n", Py_eval_input); @@ -29,7 +44,6 @@ int main (int argc, char* argv[]) { QVariant fn = tag.call("fileName", QVariantList()); Q_ASSERT(fn.toString() == QString("t.mp3")); } - qDebug() << "finished"; return 0; } diff --git a/examples/CPPPyWrapperExample/CPPPyWrapperExample.pro b/examples/CPPPyWrapperExample/CPPPyWrapperExample.pro index 931c034..626ac4b 100644 --- a/examples/CPPPyWrapperExample/CPPPyWrapperExample.pro +++ b/examples/CPPPyWrapperExample/CPPPyWrapperExample.pro @@ -2,7 +2,9 @@ CONFIG += debug VPATH += INCLUDEPATH += . $$(PYTHONQT_ROOT)/src /usr/include/python2.5 -SOURCES += CPPPyWrapperExample.cpp +SOURCES += CPPPyWrapperExample.cpp QFileImportInterface.cpp +HEADERS += QFileImportInterface.h + LIBS += -L$$(PYTHONQT_ROOT)/lib -lPythonQt -lutil diff --git a/examples/CPPPyWrapperExample/QFileImportInterface.cpp b/examples/CPPPyWrapperExample/QFileImportInterface.cpp new file mode 100644 index 0000000..707bea8 --- /dev/null +++ b/examples/CPPPyWrapperExample/QFileImportInterface.cpp @@ -0,0 +1,42 @@ +#include +#include + +#include +#include "QFileImportInterface.h" + +QFileImportInterface::QFileImportInterface() { + m_oldInterface = PythonQt::importInterface(); + PythonQt::self()->setImporter(this); +} + +QFileImportInterface::~QFileImportInterface() { + PythonQt::self()->setImporter(m_oldInterface); +} + +QByteArray QFileImportInterface::readFileAsBytes (const QString &filename) { + qDebug() << "readFileAsBytes: " << filename; + QFile f(filename); + return f.readAll(); +} + +QByteArray QFileImportInterface::readSourceFile (const QString &filename, bool &ok) { + QFile f(filename); + if (!exists(filename)) { + ok = false; + return QByteArray(); + } + else { + ok = true; + return readFileAsBytes(filename); + } +} + +bool QFileImportInterface::exists (const QString &filename) { + QFile f(filename); + return f.exists(); +} + +QDateTime QFileImportInterface::lastModifiedDate (const QString &filename) { + QFileInfo fi(filename); + return fi.lastModified(); +} diff --git a/examples/CPPPyWrapperExample/QFileImportInterface.h b/examples/CPPPyWrapperExample/QFileImportInterface.h new file mode 100644 index 0000000..86aa56b --- /dev/null +++ b/examples/CPPPyWrapperExample/QFileImportInterface.h @@ -0,0 +1,20 @@ +#ifndef QFILEIMPORTINTERFACE_H +#define QFILEIMPORTINTERFACE_H + +#include + +/** Under Construction : This does not work yet. +*/ +class QFileImportInterface : public PythonQtImportFileInterface { +public: + QFileImportInterface(); + ~QFileImportInterface(); + QByteArray readFileAsBytes (const QString &filename); + QByteArray readSourceFile (const QString &filename, bool &ok); + bool exists (const QString &filename); + QDateTime lastModifiedDate (const QString &filename); +private: + PythonQtImportFileInterface *m_oldInterface; +}; + +#endif // #ifndef QFILEIMPORTINTERFACE_H