Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/PythonQt/src/PythonQtImporter.h
3206 views
1
#ifndef _PYTHONQTIMPORTER_
2
#define _PYTHONQTIMPORTER_
3
4
/*
5
*
6
* Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
7
*
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
12
*
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
17
*
18
* Further, this software is distributed without any warranty that it is
19
* free of the rightful claim of any third person regarding infringement
20
* or the like. Any license provided herein, whether implied or
21
* otherwise, applies only to this software file. Patent licenses, if
22
* any, provided herein do not apply to combinations of this program with
23
* other software, or any other product whatsoever.
24
*
25
* You should have received a copy of the GNU Lesser General Public
26
* License along with this library; if not, write to the Free Software
27
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
*
29
* Contact information: MeVis Research GmbH, Universitaetsallee 29,
30
* 28359 Bremen, Germany or:
31
*
32
* http://www.mevis.de
33
*
34
*/
35
36
//----------------------------------------------------------------------------------
37
/*!
38
// \file PythonQtImporter.h
39
// \author Florian Link
40
// \author Last changed by $Author: stk $
41
// \date 2004-06
42
*/
43
//----------------------------------------------------------------------------------
44
45
#include "Python.h"
46
#include "structmember.h"
47
#include "osdefs.h"
48
#include "marshal.h"
49
#include "compile.h"
50
#include <time.h>
51
52
#include <qobject.h>
53
#include <qstring.h>
54
55
56
//! defines a python object that stores a Qt slot info
57
typedef struct _PythonQtImporter {
58
PyObject_HEAD
59
QString* _path;
60
} PythonQtImporter;
61
62
63
//! implements importing of python files into PythonQt
64
/*! also compiles/marshalls/unmarshalls py/pyc files and handles time stamps correctly
65
*/
66
class PythonQtImport
67
{
68
public:
69
enum module_info {
70
MI_ERROR,
71
MI_NOT_FOUND,
72
MI_MODULE,
73
MI_PACKAGE
74
};
75
76
//! initialize
77
static void init();
78
79
//! writes the python code to disk, marshalling and writing the time stamp
80
static void writeCompiledModule(PyCodeObject *co, const QString& filename, long mtime);
81
82
/*! Given the contents of a .py[co] file in a buffer, unmarshal the data
83
and return the code object. Return None if it the magic word doesn't
84
match (we do this instead of raising an exception as we fall back
85
to .py if available and we don't want to mask other errors).
86
Returns a new reference. */
87
static PyObject *unmarshalCode(const QString& path, const QByteArray& data, time_t mtime);
88
89
//! Given a string buffer containing Python source code, compile it
90
//! return and return a code object as a new reference.
91
static PyObject *compileSource(const QString& path, const QByteArray& data);
92
93
//! Return the code object for the module named by 'fullname' from the
94
//! Zip archive as a new reference.
95
static PyObject *getCodeFromData(const QString& path, int isbytecode = 0, int ispackage = 0,
96
time_t mtime = 0);
97
98
//! Get the code object associated with the module specified by
99
//! 'fullname'.
100
static PyObject * getModuleCode(PythonQtImporter *self, char *fullname,
101
int *p_ispackage, QString& modpath);
102
103
104
//! gets the compiled code for the given *.py file if there is a valid pyc file, otherwise compiles the file and writes the pyc
105
static PyObject* getCodeFromPyc(const QString& file);
106
107
//! Return if module exists and is a package or a module
108
static module_info getModuleInfo(PythonQtImporter* self, const QString& fullname);
109
110
//! get the last name of a dot chain (first.second.last)
111
static QString getSubName(const QString& str);
112
113
//! Given a buffer, return the long that is represented by the first
114
//! 4 bytes, encoded as little endian. This partially reimplements
115
//! marshal.c:r_long()
116
static long getLong(unsigned char *buf);
117
118
//! get time stamp of file
119
static time_t getMTimeOfSource(const QString& path);
120
121
//! replace extension of file
122
static QString replaceExtension(const QString& str, const QString& ext);
123
124
};
125
126
#endif
127
128
129