xbmc
PythonInvoker.h
1 /*
2  * Copyright (C) 2013-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "interfaces/generic/ILanguageInvoker.h"
12 #include "interfaces/legacy/Addon.h"
13 #include "interfaces/python/LanguageHook.h"
14 #include "threads/CriticalSection.h"
15 #include "threads/Event.h"
16 
17 #include <map>
18 #include <string>
19 #include <vector>
20 
21 typedef struct _object PyObject;
22 
24 {
25 public:
26  explicit CPythonInvoker(ILanguageInvocationHandler* invocationHandler);
27  ~CPythonInvoker() override;
28 
29  bool Execute(const std::string& script,
30  const std::vector<std::string>& arguments = std::vector<std::string>()) override;
31 
32  bool IsStopping() const override { return m_stop || ILanguageInvoker::IsStopping(); }
33 
34  typedef PyObject* (*PythonModuleInitialization)();
35 
36 protected:
37  // implementation of ILanguageInvoker
38  bool execute(const std::string& script, const std::vector<std::string>& arguments) override;
39  virtual void executeScript(FILE* fp, const std::string& script, PyObject* moduleDict);
40  bool stop(bool abort) override;
41  void onExecutionDone() override;
42  void onExecutionFailed() override;
43 
44  // custom virtual methods
45  virtual std::map<std::string, PythonModuleInitialization> getModules() const = 0;
46  virtual const char* getInitializationScript() const = 0;
47  virtual void onInitialization();
48  // actually a PyObject* but don't wanna draw Python.h include into the header
49  virtual void onPythonModuleInitialization(void* moduleDict);
50  virtual void onDeinitialization();
51 
52  virtual void onSuccess() {}
53  virtual void onAbort() {}
54  virtual void onError(const std::string& exceptionType = "",
55  const std::string& exceptionValue = "",
56  const std::string& exceptionTraceback = "");
57 
58  std::string m_sourceFile;
59  CCriticalSection m_critical;
60 
61 private:
62  void initializeModules(const std::map<std::string, PythonModuleInitialization>& modules);
63  bool initializeModule(PythonModuleInitialization module);
64  void getAddonModuleDeps(const ADDON::AddonPtr& addon, std::set<std::string>& paths);
65  bool execute(const std::string& script, std::vector<std::wstring>& arguments);
66  FILE* PyFile_AsFileWithMode(PyObject* py_file, const char* mode);
67 
68  PyThreadState* m_threadState;
69  bool m_stop;
70  CEvent m_stoppedEvent;
71 
73  bool m_systemExitThrown = false;
74 
75  static CCriticalSection s_critical;
76 };
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
Definition: PythonInvoker.h:23
Definition: ILanguageInvocationHandler.h:13
Definition: ILanguageInvoker.h:31