Path: blob/devel/ElmerGUI/PythonQt/src/PythonQtImporter.h
3206 views
#ifndef _PYTHONQTIMPORTER_1#define _PYTHONQTIMPORTER_23/*4*5* Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.6*7* This library is free software; you can redistribute it and/or8* modify it under the terms of the GNU Lesser General Public9* License as published by the Free Software Foundation; either10* version 2.1 of the License, or (at your option) any later version.11*12* This library is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU15* Lesser General Public License for more details.16*17* Further, this software is distributed without any warranty that it is18* free of the rightful claim of any third person regarding infringement19* or the like. Any license provided herein, whether implied or20* otherwise, applies only to this software file. Patent licenses, if21* any, provided herein do not apply to combinations of this program with22* other software, or any other product whatsoever.23*24* You should have received a copy of the GNU Lesser General Public25* License along with this library; if not, write to the Free Software26* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA27*28* Contact information: MeVis Research GmbH, Universitaetsallee 29,29* 28359 Bremen, Germany or:30*31* http://www.mevis.de32*33*/3435//----------------------------------------------------------------------------------36/*!37// \file PythonQtImporter.h38// \author Florian Link39// \author Last changed by $Author: stk $40// \date 2004-0641*/42//----------------------------------------------------------------------------------4344#include "Python.h"45#include "structmember.h"46#include "osdefs.h"47#include "marshal.h"48#include "compile.h"49#include <time.h>5051#include <qobject.h>52#include <qstring.h>535455//! defines a python object that stores a Qt slot info56typedef struct _PythonQtImporter {57PyObject_HEAD58QString* _path;59} PythonQtImporter;606162//! implements importing of python files into PythonQt63/*! also compiles/marshalls/unmarshalls py/pyc files and handles time stamps correctly64*/65class PythonQtImport66{67public:68enum module_info {69MI_ERROR,70MI_NOT_FOUND,71MI_MODULE,72MI_PACKAGE73};7475//! initialize76static void init();7778//! writes the python code to disk, marshalling and writing the time stamp79static void writeCompiledModule(PyCodeObject *co, const QString& filename, long mtime);8081/*! Given the contents of a .py[co] file in a buffer, unmarshal the data82and return the code object. Return None if it the magic word doesn't83match (we do this instead of raising an exception as we fall back84to .py if available and we don't want to mask other errors).85Returns a new reference. */86static PyObject *unmarshalCode(const QString& path, const QByteArray& data, time_t mtime);8788//! Given a string buffer containing Python source code, compile it89//! return and return a code object as a new reference.90static PyObject *compileSource(const QString& path, const QByteArray& data);9192//! Return the code object for the module named by 'fullname' from the93//! Zip archive as a new reference.94static PyObject *getCodeFromData(const QString& path, int isbytecode = 0, int ispackage = 0,95time_t mtime = 0);9697//! Get the code object associated with the module specified by98//! 'fullname'.99static PyObject * getModuleCode(PythonQtImporter *self, char *fullname,100int *p_ispackage, QString& modpath);101102103//! gets the compiled code for the given *.py file if there is a valid pyc file, otherwise compiles the file and writes the pyc104static PyObject* getCodeFromPyc(const QString& file);105106//! Return if module exists and is a package or a module107static module_info getModuleInfo(PythonQtImporter* self, const QString& fullname);108109//! get the last name of a dot chain (first.second.last)110static QString getSubName(const QString& str);111112//! Given a buffer, return the long that is represented by the first113//! 4 bytes, encoded as little endian. This partially reimplements114//! marshal.c:r_long()115static long getLong(unsigned char *buf);116117//! get time stamp of file118static time_t getMTimeOfSource(const QString& path);119120//! replace extension of file121static QString replaceExtension(const QString& str, const QString& ext);122123};124125#endif126127128129