Adonthell  0.4
python_class.h
Go to the documentation of this file.
1 /*
2  $Id: python_class.h,v 1.10 2005/04/16 17:56:32 ksterker Exp $
3 
4  Copyright (C) 2001 Kai Sterker <kaisterker@linuxgames.com>
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 
16 /**
17  * @file python_class.h
18  * @author Kai Sterker <kaisterker@linuxgames.com>
19  *
20  * @brief Defines the python class. This file is named this way
21  * so it doesn't conflicts with Python.h Python's include
22  * file on non-case aware systems.
23  *
24  *
25  */
26 
27 
28 #ifndef PYTHON_CLASS_H__
29 #define PYTHON_CLASS_H__
30 
31 #include "Python.h"
32 #include "compile.h"
33 #include "eval.h"
34 #include "node.h"
35 #include "fileops.h"
36 
37 /**
38  * Grant simplified access to the Python interpreter.
39  *
40  */
41 class python
42 {
43 public:
44  /**
45  * Initialise Python and insert the Adonthell include paths.
46  *
47  *
48  * @return true in case of success, false otherwise.
49  */
50  static void init ();
51 
52  /**
53  * Cleanup Python.
54  *
55  */
56  static void cleanup ();
57 
58  /**
59  * Adds a directory to Python's include path.
60  *
61  * @param name directory to add to Python's include path.
62  */
63  static void insert_path( char * name);
64 
65  /**
66  * Execute Python statements contained in a string.
67  *
68  * @param s string containing Python statements to execute.
69  */
70  static void exec_string(char * s);
71 
72  /**
73  * Executes a Python script.
74  *
75  * @param filename name of the file to execute.
76  *
77  * @return true in case of success, false otherwise.
78  */
79  static bool exec_file (string filename);
80 
81  /**
82  * Imports a Python module.
83  *
84  * @param filename file name of the module to import.
85  *
86  * @return pointer to the imported module.
87  */
88  static PyObject *import_module (string filename);
89 
90  /**
91  * Dumps any error information to stderr.
92  *
93  */
94  static void show_traceback( void );
95 
96  /**
97  * Magic function that makes any C object available to Python!
98  *
99  * @param instance pointer to the instance to pass.
100  * @param class_name name of the class of the passed instance.
101  *
102  * @return pointer to the passed %object.
103  */
104  static PyObject *pass_instance (void* instance, const char* class_name);
105 
106  /**
107  * Loads a Python tuple previously saved with put_tuple ().
108  *
109  * @param file Opened file where to load the tuple from.
110  *
111  * @return Restored Python tuple.
112  */
113  static PyObject * get_tuple (igzstream & file);
114 
115  /**
116  * Save a Python tuple into a file.
117  *
118  * @warning The Python tuple MUST ONLY be composed of Python strings
119  * or integers!
120  *
121  * @param tuple Python tuple to save.
122  * @param file Opened file where to save the tuple to.
123  */
124  static void put_tuple (PyObject * tuple, ogzstream & file);
125 
126  static PyObject *module;
127 };
128 
129 #ifndef SWIG
130 namespace data
131 {
132  /**
133  * Global namespace to use in scripts.
134  *
135  */
136  extern PyObject *globals;
137 }
138 #endif
139 
140 #endif // PYTHON_CLASS_H__