~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/python/api2_2x/Registry.c

Tags: upstream-2.40
ImportĀ upstreamĀ versionĀ 2.40

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* 
2
 
 * $Id: Registry.c,v 1.5 2004/10/07 19:25:40 stiv Exp $
 
2
 * $Id: Registry.c,v 1.7 2005/07/18 03:50:37 ascotan Exp $
3
3
 *
4
4
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5
5
 *
30
30
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
31
31
*/
32
32
 
33
 
#include "Registry.h"
34
 
 
35
 
#include <stdio.h>
36
 
 
 
33
#include "Registry.h" /*This must come first */
 
34
 
 
35
#include "BKE_global.h"
37
36
#include "gen_utils.h"
38
37
 
39
38
 
40
 
 
41
39
/* the Registry dictionary */
42
40
PyObject *bpy_registryDict = NULL;
43
41
 
64
62
    Each key references another dict with saved data from a specific script.\n";
65
63
 
66
64
char M_Registry_GetKey_doc[] =
67
 
        "(name) - Get a specific entry (dict) from the Registry dictionary\n\
68
 
 (name) - a string that references a specific script.\n";
 
65
        "(name, disk = False) - Get an entry (a dict) from the Registry dictionary\n\
 
66
 (name) - a string that references a specific script;\n\
 
67
 (disk = False) - search on the user (if available) or default scripts config\n\
 
68
data dir.\n";
69
69
 
70
70
char M_Registry_SetKey_doc[] =
71
 
        "(key, dict) - Store an entry in the Registry dictionary.\n\
 
71
        "(key, dict, disk = False) - Store an entry in the Registry dictionary.\n\
72
72
    If an entry with the same 'key' already exists, it is substituted.\n\
73
73
 (key) - the string to use as a key for the dict being saved.\n\
74
 
 (dict) - a dictionary with the data to be stored.\n";
 
74
 (dict) - a dictionary with the data to be stored.\n\
 
75
 (disk = False) - also write data as a config file inside the user (if\n\
 
76
available) or default scripts config data dir.\n";
75
77
 
76
78
char M_Registry_RemoveKey_doc[] =
77
 
        "(key) - Remove the dict with key 'key' from the Registry.\n";
 
79
        "(key, disk = False) - Remove the dict with key 'key' from the Registry.\n\
 
80
 (key) - the name of the key to delete;\n\
 
81
 (disk = False) - if True the respective config file is also deleted.\n";
78
82
 
79
83
/*****************************************************************************/
80
84
/* Python method structure definition for Blender.Registry module:           */
118
122
{
119
123
        PyObject *pyentry = NULL;
120
124
        PyObject *pydict = NULL;
 
125
        int disk = 0;
121
126
 
122
127
        if( !bpy_registryDict )
123
128
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
124
 
                                              "No Registry dictionary found!" );
 
129
                        "No Registry dictionary found!" );
125
130
 
126
 
        if( !PyArg_ParseTuple( args, "O!", &PyString_Type, &pyentry ) )
 
131
        if( !PyArg_ParseTuple( args, "O!|i", &PyString_Type, &pyentry, &disk ) )
127
132
                return EXPP_ReturnPyObjError( PyExc_AttributeError,
128
 
                                              "expected a string" );
 
133
                        "expected a string and optionally a bool" );
129
134
 
130
135
        pydict = PyDict_GetItem( bpy_registryDict, pyentry );   /* borrowed ... */
131
136
 
132
 
        if( !pydict )
133
 
/*    return EXPP_ReturnPyObjError (PyExc_KeyError,
134
 
            "no such key in the Registry"); */
135
 
                pydict = Py_None;       /* better to return None than an error */
136
 
 
137
 
        Py_INCREF( pydict );    /* ... so we incref it */
138
 
        /* should we copy the dict instead? */
139
 
        return pydict;
 
137
        if (!pydict) {
 
138
                if (disk > 0) {
 
139
                        /* try to get data from disk */
 
140
                        char buf[256];
 
141
                        PyOS_snprintf(buf, sizeof(buf),
 
142
                                "import Blender, BPyRegistry; BPyRegistry.LoadConfigData('%s')",
 
143
                                PyString_AsString(pyentry));
 
144
                        if (!PyRun_SimpleString(buf))
 
145
                                pydict = PyDict_GetItem(bpy_registryDict, pyentry);
 
146
                        else PyErr_Clear();
 
147
                }
 
148
 
 
149
                if (!pydict) /* no need to return a KeyError, since without doubt */
 
150
                        pydict = Py_None; /* Py_None means no key (all valid keys are dicts) */
 
151
        }
 
152
 
 
153
        return EXPP_incr_ret (pydict); /* ... so we incref it */
140
154
}
141
155
 
142
156
/*****************************************************************************/
147
161
{
148
162
        PyObject *pystr = NULL;
149
163
        PyObject *pydict = NULL;
 
164
        int disk = 0;
150
165
 
151
166
        if( !bpy_registryDict )
152
167
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
153
168
                                              "No Registry dictionary found!" );
154
169
 
155
 
        if( !PyArg_ParseTuple( args, "O!O!",
 
170
        if( !PyArg_ParseTuple( args, "O!O!|i",
156
171
                               &PyString_Type, &pystr, &PyDict_Type,
157
 
                               &pydict ) )
 
172
                               &pydict, &disk ) )
158
173
                return EXPP_ReturnPyObjError( PyExc_AttributeError,
159
174
                                              "expected a string and a dictionary" );
160
175
 
162
177
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
163
178
                                              "Registry_SetKey: couldn't update the Registry dict" );
164
179
 
 
180
        if (disk) {
 
181
                /* try to save data to disk */
 
182
                char buf[256];
 
183
                PyOS_snprintf(buf, sizeof(buf),
 
184
                        "import Blender, BPyRegistry; BPyRegistry.SaveConfigData('%s')",
 
185
                        PyString_AsString(pystr));
 
186
                if (PyRun_SimpleString(buf) != 0) {
 
187
                        PyErr_Clear();
 
188
                        if (G.f & G_DEBUG)
 
189
                                fprintf(stderr, "\nCan't save script configuration data!\n");
 
190
                }
 
191
        }
 
192
 
165
193
        Py_INCREF( Py_None );
166
194
        return Py_None;
167
195
}
173
201
static PyObject *M_Registry_RemoveKey( PyObject * self, PyObject * args )
174
202
{
175
203
        PyObject *pystr = NULL;
 
204
        int disk = 0;
176
205
 
177
206
        if( !bpy_registryDict )
178
207
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
179
208
                                              "No Registry dictionary found!" );
180
209
 
181
 
        if( !PyArg_ParseTuple( args, "O!", &PyString_Type, &pystr ) )
 
210
        if( !PyArg_ParseTuple( args, "O!|i", &PyString_Type, &pystr, &disk ) )
182
211
                return EXPP_ReturnPyObjError( PyExc_AttributeError,
183
 
                                              "expected a string" );
 
212
                                              "expected a string and optionally a bool" );
184
213
 
185
214
        if( PyDict_DelItem( bpy_registryDict, pystr ) ) /* returns 0 on success */
186
215
                return EXPP_ReturnPyObjError( PyExc_KeyError,
187
216
                                              "no such key in the Registry" );
 
217
        else if (disk) {
 
218
                /* try to delete from disk too */
 
219
                char buf[256];
 
220
                PyOS_snprintf(buf, sizeof(buf),
 
221
                        "import Blender, BPyRegistry; BPyRegistry.RemoveConfigData('%s')",
 
222
                        PyString_AsString(pystr));
 
223
                if (PyRun_SimpleString(buf) != 0) {
 
224
                        PyErr_Clear();
 
225
                        if (G.f & G_DEBUG)
 
226
                                fprintf(stderr, "\nCan't remove script configuration data file!\n");
 
227
                }
 
228
        }
188
229
 
189
230
        Py_INCREF( Py_None );
190
231
        return Py_None;