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 protected:
35  // implementation of ILanguageInvoker
36  bool execute(const std::string& script, const std::vector<std::string>& arguments) override;
37  virtual void executeScript(FILE* fp, const std::string& script, PyObject* moduleDict);
38  bool stop(bool abort) override;
39  void onExecutionDone() override;
40  void onExecutionFailed() override;
41 
42  // custom virtual methods
43  virtual const char* getInitializationScript() const = 0;
44  virtual void onInitialization();
45  // actually a PyObject* but don't wanna draw Python.h include into the header
46  virtual void onPythonModuleInitialization(void* moduleDict);
47  virtual void onDeinitialization();
48 
49  virtual void onSuccess() {}
50  virtual void onAbort() {}
51  virtual void onError(const std::string& exceptionType = "",
52  const std::string& exceptionValue = "",
53  const std::string& exceptionTraceback = "");
54 
55  std::string m_sourceFile;
56  CCriticalSection m_critical;
57 
58 private:
59  void getAddonModuleDeps(const ADDON::AddonPtr& addon, std::set<std::string>& paths);
60  bool execute(const std::string& script, std::vector<std::wstring>& arguments);
61  FILE* PyFile_AsFileWithMode(PyObject* py_file, const char* mode);
62 
63  PyThreadState* m_threadState;
64  bool m_stop = false;
65  CEvent m_stoppedEvent;
66 
68  bool m_systemExitThrown = false;
69 
70  static CCriticalSection s_critical;
71 
72 private:
73  struct PyObjectDeleter
74  {
75  void operator()(PyObject* p) const;
76  };
77 
78 public:
79  typedef std::unique_ptr<PyObject, PyObjectDeleter> PyObjectPtr;
80 };
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