Skip to content

Commit

Permalink
Added pycontext
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyselan committed Jan 3, 2011
1 parent a0fb960 commit a7824a9
Show file tree
Hide file tree
Showing 10 changed files with 579 additions and 36 deletions.
5 changes: 3 additions & 2 deletions export/OpenColorIO/OpenColorIO.h
Expand Up @@ -644,11 +644,12 @@ OCIO_NAMESPACE_ENTER
void setWorkingDir(const char * dirname);
const char * getWorkingDir() const;

void loadEnvironmentVariables();

void setStringVar(const char * name, const char * value);
const char * getStringVar(const char * name) const;

// Seed all string vars with the current environment
void loadEnvironment();

//! Do a file lookup.
// Evaluate all variables, and walk the full search path (as needed).
// If the filename cannot be found, an exception will be thrown.
Expand Down
8 changes: 8 additions & 0 deletions export/PyOpenColorIO/PyOpenColorIO.h
Expand Up @@ -51,6 +51,14 @@ OCIO_NAMESPACE_ENTER
ConstConfigRcPtr GetConstConfig(PyObject * config, bool allowCast);
ConfigRcPtr GetEditableConfig(PyObject * config);

// Context
PyObject * BuildConstPyContext(ConstContextRcPtr context);
PyObject * BuildEditablePyContext(ContextRcPtr context);
bool IsPyContext(PyObject * config);
bool IsPyContextEditable(PyObject * config);
ConstContextRcPtr GetConstContext(PyObject * context, bool allowCast);
ContextRcPtr GetEditableContext(PyObject * context);

// Transform
PyObject * BuildConstPyTransform(ConstTransformRcPtr transform);
PyObject * BuildEditablePyTransform(TransformRcPtr transform);
Expand Down
2 changes: 1 addition & 1 deletion src/core/Config.cpp
Expand Up @@ -192,7 +192,7 @@ OCIO_NAMESPACE_ENTER
strictParsing_(true),
sanity_(SANITY_UNKNOWN)
{
context_->loadEnvironmentVariables();
context_->loadEnvironment();

defaultLumaCoefs_.resize(3);
defaultLumaCoefs_[0] = DEFAULT_LUMA_COEFF_R;
Expand Down
4 changes: 2 additions & 2 deletions src/core/Context.cpp
Expand Up @@ -145,9 +145,9 @@ namespace
return getImpl()->workingDir_.c_str();
}

void Context::loadEnvironmentVariables()
void Context::loadEnvironment()
{
LoadEnvironmentVariables(getImpl()->envMap_);
LoadEnvironment(getImpl()->envMap_);
}

void Context::setStringVar(const char * name, const char * value)
Expand Down
2 changes: 1 addition & 1 deletion src/core/PathUtils.cpp
Expand Up @@ -92,7 +92,7 @@ OCIO_NAMESPACE_ENTER
} // path namespace


void LoadEnvironmentVariables(EnvMap & map)
void LoadEnvironment(EnvMap & map)
{
for (char **env = GetEnviron(); *env != NULL; ++env)
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/PathUtils.h
Expand Up @@ -58,7 +58,7 @@ OCIO_NAMESPACE_ENTER
typedef std::multimap< std::string, std::string, EnvMapKey< std::string > > EnvMap;

// Get map of current env key = value,
void LoadEnvironmentVariables(EnvMap & map);
void LoadEnvironment(EnvMap & map);

// Expand a string with $VAR, ${VAR} or %VAR% with the keys passed
// in the EnvMap.
Expand Down
76 changes: 47 additions & 29 deletions src/pyglue/PyConfig.cpp
Expand Up @@ -200,7 +200,7 @@ OCIO_NAMESPACE_ENTER
PyObject * PyOCIO_Config_getDefaultLumaCoefs( PyObject * self );
PyObject * PyOCIO_Config_setDefaultLumaCoefs( PyObject * self, PyObject * args );

PyObject * PyOCIO_Config_getProcessor( PyObject * self, PyObject * args );
PyObject * PyOCIO_Config_getProcessor( PyObject * self, PyObject * args, PyObject * kwargs );

///////////////////////////////////////////////////////////////////////
///
Expand Down Expand Up @@ -239,7 +239,7 @@ OCIO_NAMESPACE_ENTER
{"getDefaultLumaCoefs", (PyCFunction) PyOCIO_Config_getDefaultLumaCoefs, METH_NOARGS, "" },
{"setDefaultLumaCoefs", PyOCIO_Config_setDefaultLumaCoefs, METH_VARARGS, "" },

{"getProcessor", PyOCIO_Config_getProcessor, METH_VARARGS, "" },
{"getProcessor", (PyCFunction) PyOCIO_Config_getProcessor, METH_KEYWORDS, "" },

{NULL, NULL, 0, NULL}
};
Expand Down Expand Up @@ -874,60 +874,78 @@ OCIO_NAMESPACE_ENTER

////////////////////////////////////////////////////////////////////////

// TODO: Make the argument parsing way more explicit!

PyObject * PyOCIO_Config_getProcessor( PyObject * self, PyObject * args )
PyObject * PyOCIO_Config_getProcessor( PyObject * self, PyObject * args, PyObject * kwargs)
{
try
{
PyObject * arg1 = 0;
PyObject * arg2 = 0;
if (!PyArg_ParseTuple(args,"O|O:getProcessor", &arg1, &arg2)) return NULL;
// We want this call to be as flexible as possible.
// arg1 will either be a PyTransform
// or arg1, arg2 will be {str, ColorSpace}

PyObject * arg1 = Py_None;
PyObject * arg2 = Py_None;

const char * direction = 0;
PyObject * pycontext = Py_None;

const char * kwlist[] = {"arg1", "arg2", "direction", "context", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OsO",
const_cast<char**>(kwlist),
&arg1, &arg2, &direction, &pycontext))
return 0;

ConstConfigRcPtr config = GetConstConfig(self, true);

// We want this call to be as flexible as possible,
// accept anything we can think of!
// Parse the direction string
TransformDirection dir = TRANSFORM_DIR_FORWARD;
if(direction) dir = TransformDirectionFromString( direction );

// Parse the context
ConstContextRcPtr context;
if(pycontext != Py_None) context = GetConstContext(pycontext, true);
if(!context) context = config->getCurrentContext();

// A transform + (optional) dir
if(IsPyTransform(arg1))
{
ConstTransformRcPtr transform = GetConstTransform(arg1, true);

TransformDirection dir = TRANSFORM_DIR_FORWARD;
if(arg2 && PyString_Check(arg2))
{
const char * s2 = PyString_AsString(arg2);
dir = TransformDirectionFromString( s2 );
}

return BuildConstPyProcessor(config->getProcessor(transform, dir));
return BuildConstPyProcessor(config->getProcessor(context, transform, dir));
}

// Any two (Colorspaces, colorspace name, roles)
ConstColorSpaceRcPtr cs1, cs2;

if(IsPyColorSpace(arg1)) cs1 = GetConstColorSpace(arg1, true);
if(IsPyColorSpace(arg1))
{
cs1 = GetConstColorSpace(arg1, true);
}
else if(PyString_Check(arg1))
{
cs1 = config->getColorSpace(PyString_AsString(arg1));
}
if(!cs1)
{
PyErr_SetString(PyExc_ValueError,
"Could not parse first arg. Allowed types include ColorSpace, ColorSpace name, Role.");
return NULL;
}

if(IsPyColorSpace(arg2)) cs2 = GetConstColorSpace(arg2, true);
if(IsPyColorSpace(arg2))
{
cs2 = GetConstColorSpace(arg2, true);
}
else if(PyString_Check(arg2))
{
cs2 = config->getColorSpace(PyString_AsString(arg2));
}

if(cs1 && cs2)
if(!cs2)
{
return BuildConstPyProcessor(config->getProcessor(cs1, cs2));
PyErr_SetString(PyExc_ValueError,
"Could not parse second arg. Allowed types include ColorSpace, ColorSpace name, Role.");
return NULL;
}

const char * text = "Error interpreting arguments. "
"Allowed types include Transform, ColorSpace, ColorSpace name, Role";
PyErr_SetString(PyExc_ValueError, text);
return NULL;
return BuildConstPyProcessor(config->getProcessor(context, cs1, cs2));
}
catch(...)
{
Expand Down

0 comments on commit a7824a9

Please sign in to comment.