Kross::Python::PythonObject Class Reference

#include <pythonobject.h>

Inheritance diagram for Kross::Python::PythonObject:

Inheritance graph
[legend]
Collaboration diagram for Kross::Python::PythonObject:

Collaboration graph
[legend]
List of all members.

Detailed Description

The PythonObject class is used for Instances of Python Classes by the PythonExtension class.

Definition at line 37 of file pythonobject.h.

Public Types

typedef KSharedPtr< Object > Ptr

Public Member Functions

 PythonObject (const Py::Object &object)
virtual ~PythonObject ()
virtual const QString getClassName () const
virtual Kross::Api::Object::Ptr call (const QString &name, Kross::Api::List::Ptr arguments)
virtual QStringList getCalls ()
const QStringgetName () const
virtual const QString toString ()
Object::Ptr getParent () const
bool hasChild (const QString &name) const
Object::Ptr getChild (const QString &name) const
QMap< QString, Object::Ptr > getChildren () const
bool addChild (Object::Ptr object, const QString &name=QString::null)
void removeChild (const QString &name)
void removeAllChildren ()
virtual Object::Ptr call (const QString &name, KSharedPtr< List > arguments)

Static Public Member Functions

template<class T>
static T * fromObject (Object::Ptr object)


Member Typedef Documentation

typedef KSharedPtr<Object> Kross::Api::Object::Ptr [inherited]
 

Shared pointer to implement reference-counting.

Reimplemented in Kross::Api::Callable, Kross::Api::EventAction, Kross::Api::EventScript, Kross::Api::EventSignal, Kross::Api::EventSlot, Kross::Api::Exception, Kross::Api::List, Kross::Api::Module, Kross::Api::QtObject, Kross::Api::MainModule, Kross::Api::ScriptAction, and Kross::Api::ScriptContainer.

Definition at line 63 of file object.h.


Constructor & Destructor Documentation

PythonObject::PythonObject const Py::Object &  object  )  [explicit]
 

Constructor.

Parameters:
object The Py::Object this PythonObject provides access to.

Definition at line 27 of file pythonobject.cpp.

References QString::arg().

00028     : Kross::Api::Object(object.as_string().c_str(), 0)
00029     , m_pyobject(object)
00030 {
00031     kdDebug() << QString("PythonObject::PythonObject() constructor") << endl;
00032 
00033     Py::List x( object.dir() );
00034     for(Py::Sequence::iterator i= x.begin(); i != x.end(); ++i) {
00035         std::string s = (*i).str();
00036         if(s == "__init__")
00037             continue;
00038 
00039         //if(! m_pyobject.hasAttr( (*i).str() )) continue;
00040         Py::Object o = m_pyobject.getAttr(s);
00041 
00042         QString t;
00043         if(o.isCallable()) t += "isCallable ";
00044         if(o.isDict()) t += "isDict ";
00045         if(o.isList()) t += "isList ";
00046         if(o.isMapping()) t += "isMapping ";
00047         if(o.isNumeric()) t += "isNumeric ";
00048         if(o.isSequence()) t += "isSequence ";
00049         if(o.isTrue()) t += "isTrue ";
00050         if(o.isInstance()) t += "isInstance ";
00051         kdDebug() << QString("PythonObject::PythonObject() method '%1' (%2)").arg( (*i).str().as_string().c_str() ).arg(t) << endl;
00052 
00053         if(o.isCallable())
00054             m_calls.append( (*i).str().as_string().c_str() );
00055     }
00056 }

PythonObject::~PythonObject  )  [virtual]
 

Destructor.

Definition at line 58 of file pythonobject.cpp.

00059 {
00060 }


Member Function Documentation

bool Object::addChild Object::Ptr  object,
const QString name = QString::null
[inherited]
 

Add a new child. Replaces a possible already existing child with such a name.

Parameters:
name the name of the child
object The Object to add.
Returns:
true if the Object was added successfully else false.

Definition at line 79 of file object.cpp.

References QString::arg(), QString::isEmpty(), QString::isNull(), and QMap::replace().

Referenced by Kross::Api::MainModule::addKAction(), Kross::Api::MainModule::addQObject(), Kross::Api::MainModule::addSignal(), Kross::Api::MainModule::addSlot(), Kross::Api::QtObject::QtObject(), and TestPluginModule::TestPluginModule().

00080 {
00081     QString n = name.isNull() ? object->getName() : name;
00082 
00083 #ifdef KROSS_API_OBJECT_ADDCHILD_DEBUG
00084     kdDebug() << QString("Kross::Api::Object::addChild() object.name='%2' object.classname='%3'")
00085         .arg(n).arg(object->getClassName()) << endl;
00086 #endif
00087 
00088     if(n.isEmpty()) // prevent invalid items.
00089         return false; //throw Exception::Ptr( new Exception( QString("Failed to add child object to object '%1'. Invalid name for class '%2'.").arg(getName()).arg(object->getClassName()) ) );
00090 
00091     object->m_parent = this;
00092     m_children.replace(n, object);
00093     return true;
00094 }

virtual Object::Ptr Kross::Api::Object::call const QString name,
KSharedPtr< List arguments
[virtual, inherited]
 

Pass a call to the object and evaluated it recursive down the object-hierachy. Objects like Class are able to handle call's by just implementing this function. If the call is done the called() method will be executed recursive from bottom up the call hierachy.

Exceptions:
TypeException if the object or the name is not callable.
Parameters:
name Each call has a name that says what should be called. In the case of a Class the name is the functionname.
arguments The list of arguments passed to the call.
Returns:
The call-result as Object::Ptr instance or NULL if the call has no result.

Reimplemented in Kross::Api::EventScript, Kross::Api::EventSignal, and Kross::Api::EventSlot.

Referenced by Kross::Api::Callable::call(), and Kross::Api::Callable::callChild().

Kross::Api::Object::Ptr PythonObject::call const QString name,
Kross::Api::List::Ptr  arguments
[virtual]
 

Pass a call to the object. Objects like Class are able to handle call's by just implementating this function.

Exceptions:
TypeException if the object or the name is not callable.
Parameters:
name Each call has a name that says what should be called. In the case of a Class the name is the functionname.
arguments The list of arguments passed to the call.
Returns:
The call-result as Object* instance or NULL if the call has no result.

Definition at line 67 of file pythonobject.cpp.

References QString::latin1().

00068 {
00069     kdDebug() << QString("PythonObject::call(%1)").arg(name) << endl;
00070 
00071     if(m_pyobject.isInstance()) {
00072         //if(! m_calls.contains(n)) throw ...
00073 
00074         PyObject* r = PyObject_CallMethod(m_pyobject.ptr(), (char*) name.latin1(), 0);
00075         if(! r) { //FIXME happens too if e.g. number of arguments doesn't match !!!
00076             Py::Object errobj = Py::value(Py::Exception()); // get last error
00077             throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("Failed to call method '%1': %2").arg(name).arg(errobj.as_string().c_str())) );
00078         }
00079         Py::Object result(r, true);
00080 
00081         //kdDebug() << QString("PythonObject::call(%1) call return value = '%2'").arg(name).arg(result.as_string().c_str()) << endl;
00082         return PythonExtension::toObject(result);
00083     }
00084     /*TODO??? ELSE create class instance for class-definitions???
00085     Kross::Api::ClassBase* clazz = new Kross::Api::ClassBase("", this);
00086     return new PythonExtension(clazz);
00087     */
00088 
00089     return Kross::Api::Object::call(name, arguments);
00090 }

template<class T>
static T* Kross::Api::Object::fromObject Object::Ptr  object  )  [inline, static, inherited]
 

Try to convert the Object instance to the template class T.

Exceptions:
TypeException if the cast failed.
Parameters:
object The Object to cast.
Returns:
The to a instance from template type T casted Object.

Definition at line 198 of file object.h.

00199             {
00200                 T* t = (T*) object.data();
00201                 if(! t)
00202                     throw KSharedPtr<Exception>( new Exception(QString("Object \"%1\" invalid.").arg(object ? object->getClassName() : "")) );
00203                 return t;
00204             }

QStringList PythonObject::getCalls  )  [virtual]
 

Return a list of supported callable objects.

Returns:
List of supported calls.

Reimplemented from Kross::Api::Object.

Definition at line 92 of file pythonobject.cpp.

00093 {
00094     return m_calls;
00095 }

Object::Ptr Object::getChild const QString name  )  const [inherited]
 

Return the defined child or NULL if there is no such object with that name avaible.

Parameters:
name The name of the Object to return.
Returns:
The Object matching to the defined name or NULL if there is no such Object.

Definition at line 69 of file object.cpp.

Referenced by Kross::Api::Callable::getChild().

00070 {
00071     return m_children[name];
00072 }

QMap< QString, Object::Ptr > Object::getChildren  )  const [inherited]
 

Return all children.

Returns:
A ObjectMap of children this Object has.

Definition at line 74 of file object.cpp.

Referenced by Kross::Api::Callable::getChildrenDict(), and Kross::Api::Callable::getChildrenList().

00075 {
00076     return m_children;
00077 }

const QString PythonObject::getClassName  )  const [virtual]
 

Return the class name. This could be something like "Kross::Python::PythonObject" for this object. The value is mainly used for display purposes.

Returns:
The name of this class.

Implements Kross::Api::Object.

Definition at line 62 of file pythonobject.cpp.

00063 {
00064     return "Kross::Python::PythonObject";
00065 }

const QString & Object::getName  )  const [inherited]
 

Return the name this object has.

Returns:
Name of this object.

Reimplemented in Kross::Api::ScriptContainer.

Definition at line 49 of file object.cpp.

Referenced by Kross::Api::Event< QtObject >::call(), Kross::Api::Callable::call(), Kross::Api::Callable::getChild(), and Kross::Api::Module::~Module().

00050 {
00051     return m_name;
00052 }

Object::Ptr Object::getParent  )  const [inherited]
 

Return the parent object or NULL if this object doesn't has a parent.

Returns:
The parent-Object or NULL if this Object doesn't has a parent.

Definition at line 59 of file object.cpp.

00060 {
00061     return m_parent;
00062 }

bool Object::hasChild const QString name  )  const [inherited]
 

Returns if the defined child is avaible.

Returns:
true if child exists else false.

Reimplemented in Kross::Api::MainModule.

Definition at line 64 of file object.cpp.

References QMap::contains().

Referenced by Kross::Api::MainModule::hasChild(), and Kross::Api::Callable::hasChild().

00065 {
00066     return m_children.contains(name);
00067 }

void Object::removeAllChildren  )  [inherited]
 

Remove all children.

Definition at line 104 of file object.cpp.

References QMap::clear().

00105 {
00106 #ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
00107     kdDebug() << "Kross::Api::Object::removeAllChildren()" << endl;
00108 #endif
00109     m_children.clear();
00110 }

void Object::removeChild const QString name  )  [inherited]
 

Remove an existing child.

Parameters:
name The name of the Object to remove. If there doesn't exists an Object with such name just nothing will be done.

Definition at line 96 of file object.cpp.

References QMap::remove().

00097 {
00098 #ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
00099     kdDebug() << QString("Kross::Api::Object::removeChild() name='%1'").arg(name) << endl;
00100 #endif
00101     m_children.remove(name);
00102 }

const QString Object::toString  )  [virtual, inherited]
 

Returns:
a string representation of the object or it's content. This method is mainly used for debugging and testing purposes.

Reimplemented in Kross::Api::Dict, Kross::Api::Exception, Kross::Api::List, and Kross::Api::Variant.

Definition at line 54 of file object.cpp.

References QString::arg(), and Kross::Api::Object::getClassName().

00055 {
00056     return QString("%1 (%2)").arg(m_name).arg(getClassName());
00057 }


The documentation for this class was generated from the following files:
Generated on Thu Feb 9 18:01:36 2006 for Kross by  doxygen 1.4.6