PropertyContainerPyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2007     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
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 // inclution of the generated files (generated out of PropertyContainerPy.xml)
00034 #include "PropertyContainerPy.h"
00035 #include "PropertyContainerPy.cpp"
00036 
00037 using namespace App;
00038 
00039 // returns a string which represent the object e.g. when printed in python
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))     // convert args: Python->C
00049         return NULL;                             // NULL triggers exception
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))     // convert args: Python->C
00065         return NULL;                             // NULL triggers exception
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))     // convert args: Python->C
00085         return NULL;                             // NULL triggers exception
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))     // convert args: Python->C
00098         return NULL;                             // NULL triggers exception
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     // search in PropertyList
00123     Property *prop = getPropertyContainerPtr()->getPropertyByName(attr);
00124     if (prop) {
00125         PyObject* pyobj = prop->getPyObject();
00126         if (!pyobj && PyErr_Occurred()) {
00127             // the Python exception is already set
00128             throw Py::Exception();
00129         }
00130         return pyobj;
00131     }
00132     else if (Base::streq(attr, "__dict__")) {
00133         // get the properties to the C++ PropertyContainer class
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     // search in PropertyList
00154     Property *prop = getPropertyContainerPtr()->getPropertyByName(attr);
00155     if (prop) {
00156         // Read-only attributes must not be set over its Python interface
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 }

Generated on Wed Nov 23 19:00:30 2011 for FreeCAD by  doxygen 1.6.1