PageRenderTime 43ms CodeModel.GetById 9ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 0ms

/src/pyglue/PyContext.cpp

http://github.com/imageworks/OpenColorIO
C++ | 374 lines | 299 code | 40 blank | 35 comment | 8 complexity | 9d6672076f23969aa95a2441ab576202 MD5 | raw file
  1/*
  2Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3All Rights Reserved.
  4
  5Redistribution and use in source and binary forms, with or without
  6modification, are permitted provided that the following conditions are
  7met:
  8* Redistributions of source code must retain the above copyright
  9  notice, this list of conditions and the following disclaimer.
 10* Redistributions in binary form must reproduce the above copyright
 11  notice, this list of conditions and the following disclaimer in the
 12  documentation and/or other materials provided with the distribution.
 13* Neither the name of Sony Pictures Imageworks nor the names of its
 14  contributors may be used to endorse or promote products derived from
 15  this software without specific prior written permission.
 16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 17"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 18LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 19A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 20OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 21SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 23DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 24THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 26OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 27*/
 28
 29#include <Python.h>
 30#include <OpenColorIO/OpenColorIO.h>
 31
 32#include "PyUtil.h"
 33#include "PyDoc.h"
 34
 35OCIO_NAMESPACE_ENTER
 36{
 37    
 38    PyObject * BuildConstPyContext(ConstContextRcPtr context)
 39    {
 40        return BuildConstPyOCIO<PyOCIO_Context, ContextRcPtr,
 41            ConstContextRcPtr>(context, PyOCIO_ContextType);
 42    }
 43    
 44    PyObject * BuildEditablePyContext(ContextRcPtr context)
 45    {
 46        return BuildEditablePyOCIO<PyOCIO_Context, ContextRcPtr,
 47            ConstContextRcPtr>(context, PyOCIO_ContextType);
 48    }
 49    
 50    bool IsPyContext(PyObject * pyobject)
 51    {
 52        return IsPyOCIOType(pyobject, PyOCIO_ContextType);
 53    }
 54    
 55    bool IsPyContextEditable(PyObject * pyobject)
 56    {
 57        return IsPyEditable<PyOCIO_Context>(pyobject, PyOCIO_ContextType);
 58    }
 59    
 60    ConstContextRcPtr GetConstContext(PyObject * pyobject, bool allowCast)
 61    {
 62        return GetConstPyOCIO<PyOCIO_Context, ConstContextRcPtr>(pyobject,
 63            PyOCIO_ContextType, allowCast);
 64    }
 65    
 66    ContextRcPtr GetEditableContext(PyObject * pyobject)
 67    {
 68        return GetEditablePyOCIO<PyOCIO_Context, ContextRcPtr>(pyobject,
 69            PyOCIO_ContextType);
 70    }
 71    
 72    namespace
 73    {
 74        
 75        ///////////////////////////////////////////////////////////////////////
 76        ///
 77        
 78        int PyOCIO_Context_init(PyOCIO_Context * self, PyObject * args, PyObject * kwds);
 79        void PyOCIO_Context_delete(PyOCIO_Context * self, PyObject * args);
 80        PyObject * PyOCIO_Context_isEditable(PyObject * self);
 81        PyObject * PyOCIO_Context_createEditableCopy(PyObject * self);
 82        PyObject * PyOCIO_Context_getCacheID(PyObject * self);
 83        PyObject * PyOCIO_Context_getSearchPath(PyObject * self);
 84        PyObject * PyOCIO_Context_setSearchPath(PyObject * self, PyObject * args);
 85        PyObject * PyOCIO_Context_getWorkingDir(PyObject * self);
 86        PyObject * PyOCIO_Context_setWorkingDir(PyObject * self, PyObject * args);
 87        PyObject * PyOCIO_Context_getStringVar(PyObject * self, PyObject * args);
 88        PyObject * PyOCIO_Context_setStringVar(PyObject * self, PyObject * args);
 89        PyObject * PyOCIO_Context_getNumStringVars(PyObject * self, PyObject * args);
 90        PyObject * PyOCIO_Context_getStringVarNameByIndex(PyObject * self, PyObject * args);
 91        PyObject * PyOCIO_Context_clearStringVars(PyObject * self);
 92        PyObject * PyOCIO_Context_setEnvironmentMode(PyObject * self, PyObject * args);
 93        PyObject * PyOCIO_Context_getEnvironmentMode(PyObject * self);
 94        PyObject * PyOCIO_Context_loadEnvironment(PyObject * self);
 95        PyObject * PyOCIO_Context_resolveStringVar(PyObject * self, PyObject * args);
 96        PyObject * PyOCIO_Context_resolveFileLocation(PyObject * self, PyObject * args);
 97        
 98        ///////////////////////////////////////////////////////////////////////
 99        ///
100        
101        PyMethodDef PyOCIO_Context_methods[] = {
102            { "isEditable",
103            (PyCFunction) PyOCIO_Context_isEditable, METH_NOARGS, CONTEXT_ISEDITABLE__DOC__ },
104            { "createEditableCopy",
105            (PyCFunction) PyOCIO_Context_createEditableCopy, METH_NOARGS, CONTEXT_CREATEEDITABLECOPY__DOC__ },
106            { "getCacheID",
107            (PyCFunction) PyOCIO_Context_getCacheID, METH_NOARGS, CONTEXT_GETCACHEID__DOC__ },
108            { "getSearchPath",
109            (PyCFunction) PyOCIO_Context_getSearchPath, METH_NOARGS, CONTEXT_GETSEARCHPATH__DOC__ },
110            { "setSearchPath",
111            PyOCIO_Context_setSearchPath, METH_VARARGS, CONTEXT_SETSEARCHPATH__DOC__ },
112            { "getWorkingDir",
113            (PyCFunction) PyOCIO_Context_getWorkingDir, METH_NOARGS, CONTEXT_GETWORKINGDIR__DOC__ },
114            { "setWorkingDir",
115            PyOCIO_Context_setWorkingDir, METH_VARARGS, CONTEXT_SETWORKINGDIR__DOC__ },
116            { "getStringVar",
117            PyOCIO_Context_getStringVar, METH_VARARGS, CONTEXT_GETSTRINGVAR__DOC__ },
118            { "setStringVar",
119            PyOCIO_Context_setStringVar, METH_VARARGS, CONTEXT_SETSTRINGVAR__DOC__ },
120            { "getNumStringVars",
121            PyOCIO_Context_getNumStringVars, METH_VARARGS, CONTEXT_GETNUMSTRINGVARS__DOC__ },
122            { "getStringVarNameByIndex",
123            PyOCIO_Context_getStringVarNameByIndex, METH_VARARGS, CONTEXT_GETSTRINGVARNAMEBYINDEX__DOC__ },
124            { "clearStringVars",
125            (PyCFunction) PyOCIO_Context_clearStringVars, METH_NOARGS, CONTEXT_CLEARSTRINGVARS__DOC__ },
126            { "setEnvironmentMode",
127            PyOCIO_Context_setEnvironmentMode, METH_VARARGS, CONTEXT_SETENVIRONMENTMODE__DOC__ },
128            { "getEnvironmentMode",
129            (PyCFunction) PyOCIO_Context_getEnvironmentMode, METH_NOARGS, CONTEXT_GETENVIRONMENTMODE__DOC__ },
130            { "loadEnvironment",
131            (PyCFunction) PyOCIO_Context_loadEnvironment, METH_NOARGS, CONTEXT_LOADENVIRONMENT__DOC__ },
132            { "resolveStringVar",
133            PyOCIO_Context_resolveStringVar, METH_VARARGS, CONTEXT_RESOLVESTRINGVAR__DOC__ },
134            { "resolveFileLocation",
135            PyOCIO_Context_resolveFileLocation, METH_VARARGS, CONTEXT_RESOLVEFILELOCATION__DOC__ },
136            { NULL, NULL, 0, NULL }
137        };
138        
139    }
140    
141    ///////////////////////////////////////////////////////////////////////////
142    ///
143    
144    PyTypeObject PyOCIO_ContextType = {
145        PyVarObject_HEAD_INIT(NULL, 0)              //ob_size
146        "OCIO.Context",                             //tp_name
147        sizeof(PyOCIO_Context),                     //tp_basicsize
148        0,                                          //tp_itemsize
149        (destructor)PyOCIO_Context_delete,          //tp_dealloc
150        0,                                          //tp_print
151        0,                                          //tp_getattr
152        0,                                          //tp_setattr
153        0,                                          //tp_compare
154        0,                                          //tp_repr
155        0,                                          //tp_as_number
156        0,                                          //tp_as_sequence
157        0,                                          //tp_as_mapping
158        0,                                          //tp_hash 
159        0,                                          //tp_call
160        0,                                          //tp_str
161        0,                                          //tp_getattro
162        0,                                          //tp_setattro
163        0,                                          //tp_as_buffer
164        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   //tp_flags
165        CONTEXT__DOC__,                             //tp_doc 
166        0,                                          //tp_traverse 
167        0,                                          //tp_clear 
168        0,                                          //tp_richcompare 
169        0,                                          //tp_weaklistoffset 
170        0,                                          //tp_iter 
171        0,                                          //tp_iternext 
172        PyOCIO_Context_methods,                     //tp_methods 
173        0,                                          //tp_members 
174        0,                                          //tp_getset 
175        0,                                          //tp_base 
176        0,                                          //tp_dict 
177        0,                                          //tp_descr_get 
178        0,                                          //tp_descr_set 
179        0,                                          //tp_dictoffset 
180        (initproc) PyOCIO_Context_init,             //tp_init 
181        0,                                          //tp_alloc 
182        0,                                          //tp_new 
183        0,                                          //tp_free
184        0,                                          //tp_is_gc
185    };
186    
187    namespace
188    {
189        
190        ///////////////////////////////////////////////////////////////////////
191        ///
192        
193        int PyOCIO_Context_init(PyOCIO_Context *self, PyObject * /*args*/, PyObject * /*kwds*/)
194        {
195            OCIO_PYTRY_ENTER()
196            return BuildPyObject<PyOCIO_Context, ConstContextRcPtr, ContextRcPtr>(self, Context::Create());
197            OCIO_PYTRY_EXIT(-1)
198        }
199        
200        void PyOCIO_Context_delete(PyOCIO_Context *self, PyObject * /*args*/)
201        {
202            DeletePyObject<PyOCIO_Context>(self);
203        }
204        
205        PyObject * PyOCIO_Context_isEditable(PyObject * self)
206        {
207            return PyBool_FromLong(IsPyContextEditable(self));
208        }
209        
210        PyObject * PyOCIO_Context_createEditableCopy(PyObject * self)
211        {
212            OCIO_PYTRY_ENTER()
213            ConstContextRcPtr context = GetConstContext(self, true);
214            ContextRcPtr copy = context->createEditableCopy();
215            return BuildEditablePyContext(copy);
216            OCIO_PYTRY_EXIT(NULL)
217        }
218        
219        PyObject * PyOCIO_Context_getCacheID(PyObject * self)
220        {
221            OCIO_PYTRY_ENTER()
222            ConstContextRcPtr context = GetConstContext(self, true);
223            return PyString_FromString(context->getCacheID());
224            OCIO_PYTRY_EXIT(NULL)
225        }
226        
227        PyObject * PyOCIO_Context_getSearchPath(PyObject * self)
228        {
229            OCIO_PYTRY_ENTER()
230            ConstContextRcPtr context = GetConstContext(self, true);
231            return PyString_FromString(context->getSearchPath());
232            OCIO_PYTRY_EXIT(NULL)
233        }
234        
235        PyObject * PyOCIO_Context_setSearchPath(PyObject * self, PyObject * args)
236        {
237            OCIO_PYTRY_ENTER()
238            char* path = 0;
239            if (!PyArg_ParseTuple(args, "s:setSearchPath",
240                &path)) return NULL;
241            ContextRcPtr context = GetEditableContext(self);
242            context->setSearchPath(path);
243            Py_RETURN_NONE;
244            OCIO_PYTRY_EXIT(NULL)
245        }
246        
247        PyObject * PyOCIO_Context_getWorkingDir(PyObject * self)
248        {
249            OCIO_PYTRY_ENTER()
250            ConstContextRcPtr context = GetConstContext(self, true);
251            return PyString_FromString(context->getWorkingDir());
252            OCIO_PYTRY_EXIT(NULL)
253        }
254        
255        PyObject * PyOCIO_Context_setWorkingDir(PyObject * self, PyObject * args)
256        {
257            OCIO_PYTRY_ENTER()
258            char* dirname = 0;
259            if (!PyArg_ParseTuple(args, "s:setWorkingDir",
260                &dirname)) return NULL;
261            ContextRcPtr context = GetEditableContext(self);
262            context->setWorkingDir(dirname);
263            Py_RETURN_NONE;
264            OCIO_PYTRY_EXIT(NULL)
265        }
266        
267        PyObject * PyOCIO_Context_getStringVar(PyObject * self, PyObject * args)
268        {
269            OCIO_PYTRY_ENTER()
270            char* name = 0;
271            if (!PyArg_ParseTuple(args, "s:getStringVar",
272                &name)) return NULL;
273            ConstContextRcPtr context = GetConstContext(self, true);
274            return PyString_FromString(context->getStringVar(name));
275            OCIO_PYTRY_EXIT(NULL)
276        }
277        
278        PyObject * PyOCIO_Context_setStringVar(PyObject * self, PyObject * args)
279        {
280            OCIO_PYTRY_ENTER()
281            char* name = 0;
282            char* value = 0;
283            if (!PyArg_ParseTuple(args, "ss:setStringVar",
284                &name, &value)) return NULL;
285            ContextRcPtr context = GetEditableContext(self);
286            context->setStringVar(name, value);
287            Py_RETURN_NONE;
288            OCIO_PYTRY_EXIT(NULL)
289        }
290        
291        PyObject * PyOCIO_Context_getNumStringVars(PyObject * self, PyObject * /*args*/)
292        {
293            OCIO_PYTRY_ENTER()
294            ConstContextRcPtr context = GetConstContext(self, true);
295            return PyInt_FromLong(context->getNumStringVars());
296            OCIO_PYTRY_EXIT(NULL)
297        }
298        
299        PyObject * PyOCIO_Context_getStringVarNameByIndex(PyObject * self, PyObject * args)
300        {
301            OCIO_PYTRY_ENTER()
302            int index = 0;
303            if (!PyArg_ParseTuple(args,"i:getStringVarNameByIndex",
304                &index)) return NULL;
305            ConstContextRcPtr context = GetConstContext(self, true);
306            return PyString_FromString(context->getStringVarNameByIndex(index));
307            OCIO_PYTRY_EXIT(NULL)
308        }
309        
310        PyObject * PyOCIO_Context_clearStringVars(PyObject * self)
311        {
312            OCIO_PYTRY_ENTER()
313            ContextRcPtr context = GetEditableContext(self);
314            context->clearStringVars();
315            Py_RETURN_NONE;
316            OCIO_PYTRY_EXIT(NULL)
317        }
318        
319        PyObject * PyOCIO_Context_setEnvironmentMode(PyObject * self, PyObject * args)
320        {
321            OCIO_PYTRY_ENTER()
322            EnvironmentMode mode;
323            if (!PyArg_ParseTuple(args, "O&:setEnvironmentMode",
324                ConvertPyObjectToEnvironmentMode, &mode)) return NULL;
325            ContextRcPtr context = GetEditableContext(self);
326            context->setEnvironmentMode(mode);
327            Py_RETURN_NONE;
328            OCIO_PYTRY_EXIT(NULL)
329        }
330        
331        PyObject * PyOCIO_Context_getEnvironmentMode(PyObject * self)
332        {
333            OCIO_PYTRY_ENTER()
334            ConstContextRcPtr context = GetConstContext(self, true);
335            EnvironmentMode mode = context->getEnvironmentMode();
336            return PyString_FromString(EnvironmentModeToString(mode));
337            OCIO_PYTRY_EXIT(NULL)
338        }
339        
340        PyObject * PyOCIO_Context_loadEnvironment(PyObject * self)
341        {
342            OCIO_PYTRY_ENTER()
343            ContextRcPtr context = GetEditableContext(self);
344            context->loadEnvironment();
345            Py_RETURN_NONE;
346            OCIO_PYTRY_EXIT(NULL)
347        }
348        
349        PyObject * PyOCIO_Context_resolveStringVar(PyObject * self, PyObject * args)
350        {
351            OCIO_PYTRY_ENTER()
352            char* str = 0;
353            if (!PyArg_ParseTuple(args,"s:resolveStringVar",
354                &str)) return NULL;
355            ConstContextRcPtr context = GetConstContext(self, true);
356            return PyString_FromString(context->resolveStringVar(str));
357            OCIO_PYTRY_EXIT(NULL)
358        }
359        
360        PyObject * PyOCIO_Context_resolveFileLocation(PyObject * self, PyObject * args)
361        {
362            OCIO_PYTRY_ENTER()
363            char* filename = 0;
364            if (!PyArg_ParseTuple(args,"s:resolveFileLocation",
365                &filename)) return NULL;
366            ConstContextRcPtr context = GetConstContext(self, true);
367            return PyString_FromString(context->resolveFileLocation(filename));
368            OCIO_PYTRY_EXIT(NULL)
369        }
370        
371    }
372
373}
374OCIO_NAMESPACE_EXIT