#5538 Python : Add methods at correct inheritance level

This commit is contained in:
Magne Sjaastad 2020-04-01 09:03:45 +02:00
parent 32daa567ed
commit 6267257896
3 changed files with 68 additions and 63 deletions

View File

@ -33,6 +33,7 @@
// for more details. // for more details.
// //
//################################################################################################## //##################################################################################################
#include "cafPdmObjectMethod.h" #include "cafPdmObjectMethod.h"
using namespace caf; using namespace caf;
@ -47,32 +48,34 @@ PdmObjectMethodFactory* PdmObjectMethodFactory::instance()
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// /// Check the object and the inheritance stack for the specified method name
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
std::shared_ptr<PdmObjectMethod> PdmObjectMethodFactory::createMethod( PdmObjectHandle* self, const QString& methodName ) std::shared_ptr<PdmObjectMethod> PdmObjectMethodFactory::createMethod( PdmObjectHandle* self, const QString& methodName )
{ {
QString className = self->xmlCapability()->classKeyword(); auto stack = self->xmlCapability()->classInheritanceStack();
auto classIt = m_factoryMap.find( className ); for ( auto className : stack )
if ( classIt != m_factoryMap.end() )
{ {
auto methodIt = classIt->second.find( methodName ); auto classIt = m_factoryMap.find( className );
if ( methodIt != classIt->second.end() ) if ( classIt != m_factoryMap.end() )
{ {
return methodIt->second->create( self ); auto methodIt = classIt->second.find( methodName );
if ( methodIt != classIt->second.end() )
{
return methodIt->second->create( self );
}
} }
} }
return std::shared_ptr<PdmObjectMethod>(); return std::shared_ptr<PdmObjectMethod>();
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// /// Return the methods registered for the className. Does not investigate the inheritance stack
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
std::vector<QString> PdmObjectMethodFactory::registeredMethodNames( const PdmObjectHandle* self ) const std::vector<QString> caf::PdmObjectMethodFactory::registeredMethodNames( const QString& className ) const
{ {
std::vector<QString> methods; std::vector<QString> methods;
QString className = self->xmlCapability()->classKeyword(); auto classIt = m_factoryMap.find( className );
auto classIt = m_factoryMap.find( className );
if ( classIt != m_factoryMap.end() ) if ( classIt != m_factoryMap.end() )
{ {
for ( auto methodPair : classIt->second ) for ( auto methodPair : classIt->second )

View File

@ -117,7 +117,7 @@ public:
return true; return true;
} }
std::vector<QString> registeredMethodNames( const PdmObjectHandle* self ) const; std::vector<QString> registeredMethodNames( const QString& className ) const;
private: private:
PdmObjectMethodFactory() = default; PdmObjectMethodFactory() = default;

View File

@ -235,59 +235,61 @@ QString PdmPythonGenerator::generate( PdmObjectFactory* factory ) const
} }
} }
} }
for ( QString methodName : PdmObjectMethodFactory::instance()->registeredMethodNames( object.get() ) )
{
std::shared_ptr<PdmObjectMethod> method =
PdmObjectMethodFactory::instance()->createMethod( object.get(), methodName );
std::vector<PdmFieldHandle*> arguments;
method->fields( arguments );
QString methodComment = method->uiCapability()->uiWhatsThis();
QString snake_method_name = camelToSnakeCase( methodName );
QStringList inputArgumentStrings;
QStringList outputArgumentStrings;
QStringList argumentComments;
outputArgumentStrings.push_back( QString( "\"%1\"" ).arg( methodName ) );
QString returnComment( "Data object" );
if ( method->resultIsPersistent() )
{
returnComment = method->defaultResult()->xmlCapability()->classKeyword();
}
for ( auto field : arguments )
{
bool isList = field->xmlCapability()->isVectorField();
QString defaultValue = isList ? "[]" : "None";
auto scriptability = field->capability<PdmFieldScriptability>();
auto argumentName = camelToSnakeCase( scriptability->scriptFieldName() );
auto dataType = dataTypeString( field, false );
if ( isList ) dataType = "List of " + dataType;
inputArgumentStrings.push_back( QString( "%1=%2" ).arg( argumentName ).arg( defaultValue ) );
outputArgumentStrings.push_back( QString( "%1=%1" ).arg( argumentName ) );
argumentComments.push_back( QString( "%1 (%2): %3" )
.arg( argumentName )
.arg( dataType )
.arg( field->uiCapability()->uiWhatsThis() ) );
}
QString fullComment = QString( " \"\"\"\n %1\n Arguments:\n "
"%2\n Returns:\n %3\n \"\"\"" )
.arg( methodComment )
.arg( argumentComments.join( "\n " ) )
.arg( returnComment );
QString methodCode = QString( " def %1(self, %2):\n%3\n return "
"self._call_pdm_method(%4)\n" )
.arg( snake_method_name )
.arg( inputArgumentStrings.join( ", " ) )
.arg( fullComment )
.arg( outputArgumentStrings.join( ", " ) );
classMethodsGenerated[field->ownerClass()][snake_method_name] = methodCode;
}
} }
} }
for ( QString methodName : PdmObjectMethodFactory::instance()->registeredMethodNames( classKeyword ) )
{
std::shared_ptr<PdmObjectMethod> method =
PdmObjectMethodFactory::instance()->createMethod( object.get(), methodName );
std::vector<PdmFieldHandle*> arguments;
method->fields( arguments );
QString methodComment = method->uiCapability()->uiWhatsThis();
QString snake_method_name = camelToSnakeCase( methodName );
if ( classMethodsGenerated[classKeyword][snake_method_name].count() ) continue;
QStringList inputArgumentStrings;
QStringList outputArgumentStrings;
QStringList argumentComments;
outputArgumentStrings.push_back( QString( "\"%1\"" ).arg( methodName ) );
QString returnComment( "Data object" );
if ( method->resultIsPersistent() )
{
returnComment = method->defaultResult()->xmlCapability()->classKeyword();
}
for ( auto field : arguments )
{
bool isList = field->xmlCapability()->isVectorField();
QString defaultValue = isList ? "[]" : "None";
auto scriptability = field->capability<PdmFieldScriptability>();
auto argumentName = camelToSnakeCase( scriptability->scriptFieldName() );
auto dataType = dataTypeString( field, false );
if ( isList ) dataType = "List of " + dataType;
inputArgumentStrings.push_back( QString( "%1=%2" ).arg( argumentName ).arg( defaultValue ) );
outputArgumentStrings.push_back( QString( "%1=%1" ).arg( argumentName ) );
argumentComments.push_back(
QString( "%1 (%2): %3" ).arg( argumentName ).arg( dataType ).arg( field->uiCapability()->uiWhatsThis() ) );
}
QString fullComment = QString( " \"\"\"\n %1\n Arguments:\n "
"%2\n Returns:\n %3\n \"\"\"" )
.arg( methodComment )
.arg( argumentComments.join( "\n " ) )
.arg( returnComment );
QString methodCode = QString( " def %1(self, %2):\n%3\n return "
"self._call_pdm_method(%4)\n" )
.arg( snake_method_name )
.arg( inputArgumentStrings.join( ", " ) )
.arg( fullComment )
.arg( outputArgumentStrings.join( ", " ) );
classMethodsGenerated[classKeyword][snake_method_name] = methodCode;
}
} }
} }