50 lines
2.2 KiB
C#

using Python.Runtime;
using System;
using static System.Net.Mime.MediaTypeNames;
using System.IO;
using System.Reflection;
using System.Dynamic;
namespace dopt.TOM
{
public class Plugin : SharpPython.BasePlugin
{
internal const string langMainBaseFolderName = "bin";
internal const string langMainStopSearchFolderName = "python";
internal const string doptTOMPluginLibraryUsage = "1";
protected dynamic tomWrapperPipelines;
public Plugin(string runtimePath = "") : base(SharpPython.PyOptimLevels.O, threaded: false, runtimePath: runtimePath, verbose: true)
{
// keep these env variables within C#, but set them again in Python to mitigate issues in propagation from C# to Python
Environment.SetEnvironmentVariable("LANG_MAIN_BASE_FOLDERNAME", langMainBaseFolderName, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("LANG_MAIN_STOP_SEARCH_FOLDERNAME", langMainStopSearchFolderName, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("DOPT_TOM_PLUGIN_LIBRARY_USAGE", doptTOMPluginLibraryUsage, EnvironmentVariableTarget.Process);
base.Initialise();
using (Py.GIL())
{
dynamic os = Py.Import("os");
// set environment variables directly in Python
using (PyDict kwargs = new PyDict())
{
kwargs.SetItem("LANG_MAIN_BASE_FOLDERNAME", new PyString("bin"));
kwargs.SetItem("LANG_MAIN_STOP_SEARCH_FOLDERNAME", new PyString("python"));
kwargs.SetItem("DOPT_TOM_PLUGIN_LIBRARY_USAGE", new PyString("1"));
// Call function with no positional arguments, only keyword arguments
os.environ.update.Invoke(new PyTuple(), kwargs);
}
tomWrapperPipelines = Py.Import("tom_plugin.pipeline");
}
}
public void RunOnCSV(string identifier, string filename)
{
AssertNotDisposed();
using (Py.GIL())
{
tomWrapperPipelines.run_on_csv_data(identifier, filename);
}
}
}
}