aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Carroll <thecarrolls@jiminger.com>2011-04-10 17:17:03 -0400
committerJim Carroll <thecarrolls@jiminger.com>2011-04-14 10:04:46 -0400
commitf275c4a8341a4c49833cbcf244f602f59e2ff904 (patch)
tree6830ea21524ce3686c70d511a3911ed30c75f05a
parent492687b4cdf0994dbe02241fa3d86fb5fcf381a6 (diff)
Added the ability for a python script to instantiate an Addon instance without the need to pass an addon id. The default behavior is now to obtain the addon id from the Addon::ID method which is passed through python via the global dictionary. The ability to provide an id still exists if the Addon being instantiated is different from the one that's running.
-rw-r--r--xbmc/Application.cpp2
-rw-r--r--xbmc/ApplicationMessenger.cpp2
-rw-r--r--xbmc/addons/Service.cpp2
-rw-r--r--xbmc/filesystem/PluginDirectory.cpp4
-rw-r--r--xbmc/interfaces/Builtins.cpp2
-rw-r--r--xbmc/interfaces/python/XBPyThread.cpp6
-rw-r--r--xbmc/interfaces/python/XBPyThread.h3
-rw-r--r--xbmc/interfaces/python/XBPython.cpp28
-rw-r--r--xbmc/interfaces/python/XBPython.h4
-rw-r--r--xbmc/interfaces/python/xbmcmodule/PythonAddon.cpp20
-rw-r--r--xbmc/windows/GUIMediaWindow.cpp2
-rw-r--r--xbmc/windows/GUIWindowFileManager.cpp2
-rw-r--r--xbmc/windows/GUIWindowWeather.cpp2
13 files changed, 42 insertions, 37 deletions
diff --git a/xbmc/Application.cpp b/xbmc/Application.cpp
index 9412971e72..07ef456876 100644
--- a/xbmc/Application.cpp
+++ b/xbmc/Application.cpp
@@ -4651,7 +4651,7 @@ bool CApplication::ExecuteXBMCAction(std::string actionStr)
#ifdef HAS_PYTHON
if (item.IsPythonScript())
{ // a python script
- g_pythonParser.evalFile(item.m_strPath.c_str());
+ g_pythonParser.evalFile(item.m_strPath.c_str(),NULL);
}
else
#endif
diff --git a/xbmc/ApplicationMessenger.cpp b/xbmc/ApplicationMessenger.cpp
index fdf69c0c97..0f700a752a 100644
--- a/xbmc/ApplicationMessenger.cpp
+++ b/xbmc/ApplicationMessenger.cpp
@@ -505,7 +505,7 @@ case TMSG_POWERDOWN:
case TMSG_EXECUTE_SCRIPT:
#ifdef HAS_PYTHON
- g_pythonParser.evalFile(pMsg->strParam.c_str());
+ g_pythonParser.evalFile(pMsg->strParam.c_str(),NULL);
#endif
break;
diff --git a/xbmc/addons/Service.cpp b/xbmc/addons/Service.cpp
index 8a30a61e27..0f61d8fe03 100644
--- a/xbmc/addons/Service.cpp
+++ b/xbmc/addons/Service.cpp
@@ -50,7 +50,7 @@ bool CService::Start()
{
#ifdef HAS_PYTHON
case PYTHON:
- ret = (g_pythonParser.evalFile(LibPath()) != -1);
+ ret = (g_pythonParser.evalFile(LibPath(),ID()) != -1);
break;
#endif
diff --git a/xbmc/filesystem/PluginDirectory.cpp b/xbmc/filesystem/PluginDirectory.cpp
index 0dd8d2e7fc..9638b1adad 100644
--- a/xbmc/filesystem/PluginDirectory.cpp
+++ b/xbmc/filesystem/PluginDirectory.cpp
@@ -117,7 +117,7 @@ bool CPluginDirectory::StartScript(const CStdString& strPath, bool retrievingDir
bool success = false;
#ifdef HAS_PYTHON
CStdString file = m_addon->LibPath();
- if (g_pythonParser.evalFile(file, argv) >= 0)
+ if (g_pythonParser.evalFile(file, argv,m_addon->ID().c_str()) >= 0)
{ // wait for our script to finish
CStdString scriptName = m_addon->Name();
success = WaitOnScriptResult(file, scriptName, retrievingDir);
@@ -434,7 +434,7 @@ bool CPluginDirectory::RunScriptWithParams(const CStdString& strPath)
// run the script
#ifdef HAS_PYTHON
CLog::Log(LOGDEBUG, "%s - calling plugin %s('%s','%s','%s')", __FUNCTION__, addon->Name().c_str(), argv[0].c_str(), argv[1].c_str(), argv[2].c_str());
- if (g_pythonParser.evalFile(addon->LibPath(), argv) >= 0)
+ if (g_pythonParser.evalFile(addon->LibPath(), argv,addon->ID().c_str()) >= 0)
return true;
else
#endif
diff --git a/xbmc/interfaces/Builtins.cpp b/xbmc/interfaces/Builtins.cpp
index e0659288e1..33bcc3a833 100644
--- a/xbmc/interfaces/Builtins.cpp
+++ b/xbmc/interfaces/Builtins.cpp
@@ -375,7 +375,7 @@ int CBuiltins::Execute(const CStdString& execString)
if (CAddonMgr::Get().GetAddon(params[0], script))
scriptpath = script->LibPath();
- g_pythonParser.evalFile(scriptpath, argv);
+ g_pythonParser.evalFile(scriptpath, argv,script->ID());
}
}
#endif
diff --git a/xbmc/interfaces/python/XBPyThread.cpp b/xbmc/interfaces/python/XBPyThread.cpp
index 6c152c8929..ef01e0c19c 100644
--- a/xbmc/interfaces/python/XBPyThread.cpp
+++ b/xbmc/interfaces/python/XBPyThread.cpp
@@ -233,6 +233,12 @@ void XBPyThread::Process()
{
PyObject *f = PyString_FromString(_P(m_source).c_str());
PyDict_SetItemString(moduleDict, "__file__", f);
+ if (addonid)
+ {
+ PyObject *pyaddonid = PyString_FromString(addonid.c_str());
+ PyDict_SetItemString(moduleDict, "__xbmcaddonid__", pyaddonid);
+ CLog::Log(LOGDEBUG,"Instantiating addon using automatically obtained id of \"%s\"",addonid.c_str());
+ }
Py_DECREF(f);
PyRun_FileExFlags(fp, _P(m_source).c_str(), m_Py_file_input, moduleDict, moduleDict,1,NULL);
}
diff --git a/xbmc/interfaces/python/XBPyThread.h b/xbmc/interfaces/python/XBPyThread.h
index 2ffe1504c8..cacb9236df 100644
--- a/xbmc/interfaces/python/XBPyThread.h
+++ b/xbmc/interfaces/python/XBPyThread.h
@@ -37,6 +37,8 @@ public:
bool isStopping();
void stop();
+ void setAddonId(const char* _addonid) { addonid = _addonid; }
+
protected:
XBPython *m_pExecuter;
void *m_threadState;
@@ -47,6 +49,7 @@ protected:
unsigned int m_argc;
bool m_stopping;
int m_id;
+ CStdString addonid;
virtual void OnStartup();
virtual void Process();
diff --git a/xbmc/interfaces/python/XBPython.cpp b/xbmc/interfaces/python/XBPython.cpp
index 487a30d7bb..008eae9fe4 100644
--- a/xbmc/interfaces/python/XBPython.cpp
+++ b/xbmc/interfaces/python/XBPython.cpp
@@ -250,25 +250,6 @@ void XBPython::InitializeInterpreter()
"import sys\n"
"sys.stdout = xbmcout()\n"
"sys.stderr = xbmcout()\n"
- ""
- "import os\n"
- "def getcwd_xbmc():\n"
- " import __main__\n"
- " import warnings\n"
- " if hasattr(__main__, \"__file__\"):\n"
- " warnings.warn(\"os.getcwd() is depreciated for getting addon directory use os.path.dirname(__main__.__file__)\", DeprecationWarning, stacklevel=2)\n"
- " return os.path.dirname(__main__.__file__)\n"
- " else:\n"
- " return os.getcwd_original()\n"
- ""
- "def chdir_xbmc(dir):\n"
- " raise RuntimeError(\"os.chdir not supported in xbmc\")\n"
- ""
- "os_getcwd_original = os.getcwd\n"
- "os.getcwd = getcwd_xbmc\n"
- "os.chdir_orignal = os.chdir\n"
- "os.chdir = chdir_xbmc\n"
- ""
"print '-->Python Interpreter Initialized<--'\n"
"") == -1)
{
@@ -448,7 +429,7 @@ void XBPython::Process()
CStdString strAutoExecPy = _P("special://profile/autoexec.py");
if ( XFILE::CFile::Exists(strAutoExecPy) )
- evalFile(strAutoExecPy);
+ evalFile(strAutoExecPy,NULL);
else
CLog::Log(LOGDEBUG, "%s - no profile autoexec.py (%s) found, skipping", __FUNCTION__, strAutoExecPy.c_str());
}
@@ -492,13 +473,13 @@ bool XBPython::StopScript(const CStdString &path)
return false;
}
-int XBPython::evalFile(const CStdString &src)
+int XBPython::evalFile(const CStdString &src, const char* addonId)
{
std::vector<CStdString> argv;
- return evalFile(src, argv);
+ return evalFile(src, argv, addonId);
}
// execute script, returns -1 if script doesn't exist
-int XBPython::evalFile(const CStdString &src, const std::vector<CStdString> &argv)
+int XBPython::evalFile(const CStdString &src, const std::vector<CStdString> &argv, const char* addonId)
{
CSingleExit ex(g_graphicsContext);
CSingleLock lock(m_critSection);
@@ -520,6 +501,7 @@ int XBPython::evalFile(const CStdString &src, const std::vector<CStdString> &arg
m_nextid++;
XBPyThread *pyThread = new XBPyThread(this, m_nextid);
pyThread->setArgv(argv);
+ pyThread->setAddonId(addonId);
pyThread->evalFile(src);
PyElem inf;
inf.id = m_nextid;
diff --git a/xbmc/interfaces/python/XBPython.h b/xbmc/interfaces/python/XBPython.h
index 81b96352e6..1e9a5033f6 100644
--- a/xbmc/interfaces/python/XBPython.h
+++ b/xbmc/interfaces/python/XBPython.h
@@ -64,8 +64,8 @@ public:
int ScriptsSize();
int GetPythonScriptId(int scriptPosition);
- int evalFile(const CStdString &src);
- int evalFile(const CStdString &src, const std::vector<CStdString> &argv);
+ int evalFile(const CStdString &src, const char* addonId);
+ int evalFile(const CStdString &src, const std::vector<CStdString> &argv, const char* addonId);
int evalString(const CStdString &src, const std::vector<CStdString> &argv);
bool isRunning(int scriptId);
diff --git a/xbmc/interfaces/python/xbmcmodule/PythonAddon.cpp b/xbmc/interfaces/python/xbmcmodule/PythonAddon.cpp
index 9459bb0dc7..7eaaec7cff 100644
--- a/xbmc/interfaces/python/xbmcmodule/PythonAddon.cpp
+++ b/xbmc/interfaces/python/xbmcmodule/PythonAddon.cpp
@@ -49,21 +49,35 @@ namespace PYXBMC
if (!self) return NULL;
static const char *keywords[] = { "id", NULL };
- char *id = NULL;
+ const char *id = NULL;
// parse arguments
if (!PyArg_ParseTupleAndKeywords(
args,
kwds,
- (char*)"s",
+ (char*)"|s",
(char**)keywords,
- &id
+ (char**)&id
))
{
Py_DECREF(self);
return NULL;
};
+ // if the id wasn't passed then get the id from
+ // the global dictionary
+ if (id == NULL)
+ {
+ // Get a reference to the main module
+ // and global dictionary
+ PyObject* main_module = PyImport_AddModule((char*)"__main__");
+ PyObject* global_dict = PyModule_GetDict(main_module);
+ // Extract a reference to the function "func_name"
+ // from the global dictionary
+ PyObject* pyid = PyDict_GetItemString(global_dict, "__xbmcaddonid__");
+ id = PyString_AsString(pyid);
+ }
+
if (!CAddonMgr::Get().GetAddon(id, self->pAddon))
{
PyErr_SetString(PyExc_Exception, "Could not get AddonPtr!");
diff --git a/xbmc/windows/GUIMediaWindow.cpp b/xbmc/windows/GUIMediaWindow.cpp
index ee50bc0242..37772d7096 100644
--- a/xbmc/windows/GUIMediaWindow.cpp
+++ b/xbmc/windows/GUIMediaWindow.cpp
@@ -882,7 +882,7 @@ bool CGUIMediaWindow::OnClick(int iItem)
{
#ifdef HAS_PYTHON
if (!g_pythonParser.StopScript(addon->LibPath()))
- g_pythonParser.evalFile(addon->LibPath());
+ g_pythonParser.evalFile(addon->LibPath(),addon->ID());
#endif
return true;
}
diff --git a/xbmc/windows/GUIWindowFileManager.cpp b/xbmc/windows/GUIWindowFileManager.cpp
index 4f56a7fcea..9f08e5a08a 100644
--- a/xbmc/windows/GUIWindowFileManager.cpp
+++ b/xbmc/windows/GUIWindowFileManager.cpp
@@ -621,7 +621,7 @@ void CGUIWindowFileManager::OnStart(CFileItem *pItem)
#ifdef HAS_PYTHON
if (pItem->IsPythonScript())
{
- g_pythonParser.evalFile(pItem->m_strPath.c_str());
+ g_pythonParser.evalFile(pItem->m_strPath.c_str(),NULL);
return ;
}
#endif
diff --git a/xbmc/windows/GUIWindowWeather.cpp b/xbmc/windows/GUIWindowWeather.cpp
index 02d48586d2..6ca10a1b81 100644
--- a/xbmc/windows/GUIWindowWeather.cpp
+++ b/xbmc/windows/GUIWindowWeather.cpp
@@ -332,7 +332,7 @@ void CGUIWindowWeather::CallScript()
argv.push_back(CWeather::GetAreaCode(g_guiSettings.GetString(strSetting)));
// call our script, passing the areacode
- g_pythonParser.evalFile(argv[0], argv);
+ g_pythonParser.evalFile(argv[0], argv,addon->ID());
CLog::Log(LOGDEBUG, "%s - Weather script called: %s (%s)", __FUNCTION__, argv[0].c_str(), argv[1].c_str());
}