+ use Python's inspect module to get calling instance of addCommand

This commit is contained in:
wmayer 2015-12-16 23:07:24 +01:00
parent f958dadb69
commit 61e1a5ca2f
3 changed files with 56 additions and 2 deletions

View File

@ -901,14 +901,48 @@ PyObject* Application::sAddCommand(PyObject * /*self*/, PyObject *args,PyObject
if (!PyArg_ParseTuple(args, "sO|s", &pName,&pcCmdObj,&pSource)) // convert args: Python->C
return NULL; // NULL triggers exception
// get the call stack to find the Python module name
//
std::string module, group;
try {
Base::PyGILStateLocker lock;
Py::Module mod(PyImport_ImportModule("inspect"), true);
Py::Callable inspect(mod.getAttr("stack"));
Py::Tuple args;
Py::List list(inspect.apply(args));
args = list.getItem(0);
// usually this is the file name of the calling script
module = args.getItem(1).as_string();
Base::FileInfo fi(module);
module = fi.fileNamePure();
group = module;
std::string::size_type len = group.size();
if (len > 3 && group.substr(len-3) == "Gui")
group = group.substr(0,len-3);
}
catch (Py::Exception& e) {
e.clear();
}
try {
Base::PyGILStateLocker lock;
Py::Object cmd(pcCmdObj);
if (cmd.hasAttr("GetCommands")) {
Application::Instance->commandManager().addCommand(new PythonGroupCommand(pName, pcCmdObj));
Command* cmd = new PythonGroupCommand(pName, pcCmdObj);
if (!module.empty()) {
cmd->setAppModuleName(module.c_str());
cmd->setGroupName(group.c_str());
}
Application::Instance->commandManager().addCommand(cmd);
}
else {
Application::Instance->commandManager().addCommand(new PythonCommand(pName, pcCmdObj, pSource));
Command* cmd = new PythonCommand(pName, pcCmdObj, pSource);
if (!module.empty()) {
cmd->setAppModuleName(module.c_str());
cmd->setGroupName(group.c_str());
}
Application::Instance->commandManager().addCommand(cmd);
}
}
catch (const Base::Exception& e) {

View File

@ -367,6 +367,24 @@ std::string Command::getUniqueObjectName(const char *BaseName) const
return getActiveGuiDocument()->getDocument()->getUniqueObjectName(BaseName);
}
void Command::setAppModuleName(const char* s)
{
#if defined (_MSC_VER)
this->sAppModule = _strdup(s);
#else
this->sAppModule = strdup(s);
#endif
}
void Command::setGroupName(const char* s)
{
#if defined (_MSC_VER)
this->sGroup = _strdup(s);
#else
this->sGroup = strdup(s);
#endif
}
//--------------------------------------------------------------------------
// UNDO REDO transaction handling

View File

@ -281,10 +281,12 @@ public:
//@{
/// returns the name to which the command belongs
const char* getAppModuleName(void) const {return sAppModule;}
void setAppModuleName(const char*);
/// Get the command name
const char* getName() const { return sName; }
/// Get the name of the grouping of the command
const char* getGroupName() const { return sGroup; }
void setGroupName(const char*);
//@}
protected: