File: pythonstrategy.cpp

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (58 lines) | stat: -rw-r--r-- 1,822 bytes parent folder | download | duplicates (2)
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
#include "pythonstrategy.h"

#include "../lua/data.h"
#include "../lua/functions.h"

#include "../python/pyfunctions.h"

#include <filesystem>

#include <pybind11/pybind11.h>
#include <pybind11/eval.h>

#include <fstream>

PythonStrategy::PythonStrategy() : _code() {
  std::ifstream file("strategy.py");
  if (file.good()) {
    file.seekg(0, std::ios::end);
    const size_t size = file.tellg();
    std::vector<char> data(size + 1, 0);
    file.seekg(0, std::ios::beg);
    file.read(data.data(), size);
    _code = data.data();
  }

  Py_Initialize();

  // The following statement add the curr path to the Python search path
  const std::filesystem::path workingDir =
      std::filesystem::canonical(std::filesystem::current_path());
  PyObject* sysPath = PySys_GetObject(const_cast<char*>("path"));
  PyList_Insert(sysPath, 0, PyUnicode_FromString(workingDir.string().c_str()));
}

PythonStrategy::~PythonStrategy() { Py_Finalize(); }

void PythonStrategy::Execute(TimeFrequencyData& tfData,
                             TimeFrequencyMetaDataCPtr metaData,
                             class ScriptData& scriptData) {
  /*
  pybind11::object
  global(pybind11::module::import("__main__").attr("__dict__"));
  // exec requires a recent pybind11 version !
  pybind11::exec(_code.c_str(), global);
  pybind11::object flagFunction = aoflagger_python::get_flag_function();

  if(flagFunction.is_none())
          throw std::runtime_error("Incorrect Python strategy: strategy did not
  provide a flag method. Make sure your strategy uses
  aoflagger.set_flag_function() to provide the flag function to the caller");
  else {
          aoflagger_lua::Data::Context context;
          aoflagger_lua::Data data(tfData, metaData, context);
          flagFunction(std::ref(data));
          tfData = data.TFData();
  }
  */
}