1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/*
* Copyright (C) 2013-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
// python.h should always be included first before any other includes
#include "AddonPythonInvoker.h"
#include <utility>
#include <Python.h>
#include <osdefs.h>
#define MODULE "xbmc"
#define RUNSCRIPT_PREAMBLE \
"" \
"import " MODULE "\n" \
"class xbmcout:\n" \
" def __init__(self, loglevel=" MODULE ".LOGDEBUG):\n" \
" self.ll=loglevel\n" \
" def write(self, data):\n" \
" " MODULE ".log(data,self.ll)\n" \
" def close(self):\n" \
" " MODULE ".log('.')\n" \
" def flush(self):\n" \
" " MODULE ".log('.')\n" \
"import sys\n" \
"sys.stdout = xbmcout()\n" \
"sys.stderr = xbmcout(" MODULE ".LOGERROR)\n" \
""
#define RUNSCRIPT_SETUPTOOLS_HACK \
"" \
"import types,sys\n" \
"pkg_resources_code = \\\n" \
"\"\"\"\n" \
"def resource_filename(__name__,__path__):\n" \
" return __path__\n" \
"\"\"\"\n" \
"pkg_resources = types.ModuleType('pkg_resources')\n" \
"exec(pkg_resources_code, pkg_resources.__dict__)\n" \
"sys.modules['pkg_resources'] = pkg_resources\n" \
""
#define RUNSCRIPT_SETUP_ENVIROMENT_VARIABLES \
"" \
"from os import environ\n" \
"environ['SSL_CERT_FILE'] = 'system/certs/cacert.pem'\n" \
""
#define RUNSCRIPT_POSTSCRIPT \
"print('-->Python Interpreter Initialized<--')\n" \
""
#if defined(TARGET_ANDROID)
#define RUNSCRIPT_COMPLIANT \
RUNSCRIPT_PREAMBLE RUNSCRIPT_SETUPTOOLS_HACK RUNSCRIPT_POSTSCRIPT
#elif defined(TARGET_WINDOWS_STORE)
#define RUNSCRIPT_COMPLIANT \
RUNSCRIPT_PREAMBLE RUNSCRIPT_SETUP_ENVIROMENT_VARIABLES RUNSCRIPT_POSTSCRIPT
#else
#define RUNSCRIPT_COMPLIANT \
RUNSCRIPT_PREAMBLE RUNSCRIPT_POSTSCRIPT
#endif
namespace PythonBindings {
PyObject* PyInit_Module_xbmcdrm(void);
PyObject* PyInit_Module_xbmcgui(void);
PyObject* PyInit_Module_xbmc(void);
PyObject* PyInit_Module_xbmcplugin(void);
PyObject* PyInit_Module_xbmcaddon(void);
PyObject* PyInit_Module_xbmcvfs(void);
}
using namespace PythonBindings;
typedef struct
{
const char *name;
CPythonInvoker::PythonModuleInitialization initialization;
} PythonModule;
static PythonModule PythonModules[] =
{
{ "xbmcdrm", PyInit_Module_xbmcdrm },
{ "xbmcgui", PyInit_Module_xbmcgui },
{ "xbmc", PyInit_Module_xbmc },
{ "xbmcplugin", PyInit_Module_xbmcplugin },
{ "xbmcaddon", PyInit_Module_xbmcaddon },
{ "xbmcvfs", PyInit_Module_xbmcvfs }
};
CAddonPythonInvoker::CAddonPythonInvoker(ILanguageInvocationHandler *invocationHandler)
: CPythonInvoker(invocationHandler)
{
PyImport_AppendInittab("xbmcdrm", PyInit_Module_xbmcdrm);
PyImport_AppendInittab("xbmcgui", PyInit_Module_xbmcgui);
PyImport_AppendInittab("xbmc", PyInit_Module_xbmc);
PyImport_AppendInittab("xbmcplugin", PyInit_Module_xbmcplugin);
PyImport_AppendInittab("xbmcaddon", PyInit_Module_xbmcaddon);
PyImport_AppendInittab("xbmcvfs", PyInit_Module_xbmcvfs);
}
CAddonPythonInvoker::~CAddonPythonInvoker() = default;
std::map<std::string, CPythonInvoker::PythonModuleInitialization> CAddonPythonInvoker::getModules() const
{
static std::map<std::string, PythonModuleInitialization> modules;
if (modules.empty())
{
for (const PythonModule& pythonModule : PythonModules)
modules.insert(std::make_pair(pythonModule.name, pythonModule.initialization));
}
return modules;
}
const char* CAddonPythonInvoker::getInitializationScript() const
{
return RUNSCRIPT_COMPLIANT;
}
|