PropertyContainerPyImp.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025
00026 #ifndef _PreComp_
00027 # include <sstream>
00028 #endif
00029
00030 #include "PropertyContainer.h"
00031 #include "Property.h"
00032
00033
00034 #include "PropertyContainerPy.h"
00035 #include "PropertyContainerPy.cpp"
00036
00037 using namespace App;
00038
00039
00040 std::string PropertyContainerPy::representation(void) const
00041 {
00042 return std::string("<property container>");
00043 }
00044
00045 PyObject* PropertyContainerPy::getPropertyByName(PyObject *args)
00046 {
00047 char *pstr;
00048 if (!PyArg_ParseTuple(args, "s", &pstr))
00049 return NULL;
00050 App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr);
00051 if (prop) {
00052 return prop->getPyObject();
00053 }
00054 else {
00055 PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr);
00056 return NULL;
00057 }
00058 }
00059
00060 PyObject* PropertyContainerPy::getTypeOfProperty(PyObject *args)
00061 {
00062 Py::List ret;
00063 char *pstr;
00064 if (!PyArg_ParseTuple(args, "s", &pstr))
00065 return NULL;
00066
00067 short Type = getPropertyContainerPtr()->getPropertyType(pstr);
00068
00069 if (Type & Prop_Hidden)
00070 ret.append(Py::String("Hidden"));
00071 if (Type & Prop_ReadOnly)
00072 ret.append(Py::String("ReadOnly"));
00073 if (Type & Prop_Output)
00074 ret.append(Py::String("Output"));
00075 if (Type & Prop_Transient)
00076 ret.append(Py::String("Transient"));
00077
00078 return Py::new_reference_to(ret);
00079 }
00080
00081 PyObject* PropertyContainerPy::getGroupOfProperty(PyObject *args)
00082 {
00083 char *pstr;
00084 if (!PyArg_ParseTuple(args, "s", &pstr))
00085 return NULL;
00086
00087 const char* Group = getPropertyContainerPtr()->getPropertyGroup(pstr);
00088 if (Group)
00089 return Py::new_reference_to(Py::String(Group));
00090 else
00091 return Py::new_reference_to(Py::String(""));
00092 }
00093
00094 PyObject* PropertyContainerPy::getDocumentationOfProperty(PyObject *args)
00095 {
00096 char *pstr;
00097 if (!PyArg_ParseTuple(args, "s", &pstr))
00098 return NULL;
00099
00100 const char* Group = getPropertyContainerPtr()->getPropertyDocumentation(pstr);
00101 if (Group)
00102 return Py::new_reference_to(Py::String(Group));
00103 else
00104 return Py::new_reference_to(Py::String(""));
00105 }
00106
00107 Py::List PropertyContainerPy::getPropertiesList(void) const
00108 {
00109 Py::List ret;
00110 std::map<std::string,Property*> Map;
00111
00112 getPropertyContainerPtr()->getPropertyMap(Map);
00113
00114 for (std::map<std::string,Property*>::const_iterator It=Map.begin();It!=Map.end();++It)
00115 ret.append(Py::String(It->first));
00116
00117 return ret;
00118 }
00119
00120 PyObject *PropertyContainerPy::getCustomAttributes(const char* attr) const
00121 {
00122
00123 Property *prop = getPropertyContainerPtr()->getPropertyByName(attr);
00124 if (prop) {
00125 PyObject* pyobj = prop->getPyObject();
00126 if (!pyobj && PyErr_Occurred()) {
00127
00128 throw Py::Exception();
00129 }
00130 return pyobj;
00131 }
00132 else if (Base::streq(attr, "__dict__")) {
00133
00134 std::map<std::string,App::Property*> Map;
00135 getPropertyContainerPtr()->getPropertyMap(Map);
00136 PyObject *dict = PyDict_New();
00137 if (dict) {
00138 for ( std::map<std::string,App::Property*>::iterator it = Map.begin(); it != Map.end(); ++it )
00139 PyDict_SetItem(dict, PyString_FromString(it->first.c_str()), PyString_FromString(""));
00140 if (PyErr_Occurred()) {
00141 Py_DECREF(dict);
00142 dict = NULL;
00143 }
00144 }
00145 return dict;
00146 }
00147
00148 return 0;
00149 }
00150
00151 int PropertyContainerPy::setCustomAttributes(const char* attr, PyObject *obj)
00152 {
00153
00154 Property *prop = getPropertyContainerPtr()->getPropertyByName(attr);
00155 if (prop) {
00156
00157 short Type = getPropertyContainerPtr()->getPropertyType(prop);
00158 if (Type & Prop_ReadOnly) {
00159 std::stringstream s;
00160 s << "Object attribute '" << attr << "' is read-only";
00161 throw Py::AttributeError(s.str());
00162 }
00163
00164 prop->setPyObject(obj);
00165 return 1;
00166 }
00167
00168 return 0;
00169 }