Пример #1
0
        /// <summary>
        /// Create a new instance of a python algorithm
        /// </summary>
        /// <param name="assemblyPath"></param>
        /// <param name="algorithmInstance"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        private bool TryCreatePythonAlgorithm(string assemblyPath, out IAlgorithm algorithmInstance, out string errorMessage)
        {
            algorithmInstance = null;
            errorMessage      = string.Empty;

            //File does not exist.
            if (!File.Exists(assemblyPath))
            {
                errorMessage = $"Loader.TryCreatePythonAlgorithm(): Unable to find py file: {assemblyPath}";
                return(false);
            }

            try
            {
                var pythonFile = new FileInfo(assemblyPath);
                var moduleName = pythonFile.Name.Replace(".pyc", "").Replace(".py", "");

                //Help python find the module
                Environment.SetEnvironmentVariable("PYTHONPATH", pythonFile.DirectoryName);

                // Initialize Python Engine
                if (!PythonEngine.IsInitialized)
                {
                    PythonEngine.Initialize();
                    PythonEngine.BeginAllowThreads();
                }

                // Import Python module
                using (Py.GIL())
                {
                    Log.Trace($"Loader.TryCreatePythonAlgorithm(): Python version {PythonEngine.Version}");
                    Log.Trace($"Loader.TryCreatePythonAlgorithm(): Importing python module {moduleName}");
                    var module = Py.Import(moduleName);

                    if (module == null)
                    {
                        errorMessage = $"Loader.TryCreatePythonAlgorithm(): Unable to import python module {assemblyPath}. Check for errors in the python scripts.";
                        return(false);
                    }

                    Log.Trace("Loader.TryCreatePythonAlgorithm(): Creating IAlgorithm instance.");

                    algorithmInstance = new AlgorithmPythonWrapper(module);
                    ObjectActivator.SetPythonModule(module);
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
                errorMessage = $"Loader.TryCreatePythonAlgorithm(): Unable to import python module {assemblyPath}. {e.Message}";
            }

            //Successful load.
            return(algorithmInstance != null);
        }
Пример #2
0
        /// <summary>
        /// Create a new instance of a python algorithm
        /// </summary>
        /// <param name="assemblyPath"></param>
        /// <param name="algorithmInstance"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        private bool TryCreatePythonAlgorithm(string assemblyPath, out IAlgorithm algorithmInstance, out string errorMessage)
        {
            algorithmInstance = null;
            errorMessage      = string.Empty;

            //File does not exist.
            if (!File.Exists(assemblyPath))
            {
                errorMessage = "Loader.TryCreatePythonAlgorithm(): Unable to find py file: " + assemblyPath;
                return(false);
            }

            try
            {
                //Copy the util to cache and set the
                var cache = new FileInfo(assemblyPath).DirectoryName;
                var util  = Path.Combine(cache, "AlgorithmPythonUtil.py");
                if (!File.Exists(util))
                {
                    File.Copy("AlgorithmPythonUtil.py", util);
                }

                //Help python find the module
                Environment.SetEnvironmentVariable("PYTHONPATH", cache);

                // Initialize Python Engine
                if (!PythonEngine.IsInitialized)
                {
                    PythonEngine.Initialize();
                    PythonEngine.BeginAllowThreads();
                }

                // Import Python module
                using (Py.GIL())
                {
                    Log.Trace("Loader.TryCreatePythonAlgorithm(): Locating module name..");
                    var pythonFile = new FileInfo(assemblyPath);
                    var moduleName = pythonFile.Name.Replace(".pyc", "").Replace(".py", "");

                    Log.Trace("Loader.TryCreatePythonAlgorithm(): Importing python module " + moduleName);
                    var module = Py.Import(moduleName);

                    if (module == null)
                    {
                        errorMessage = "Loader.TryCreatePythonAlgorithm(): Unable to import python module " + assemblyPath + ". Check for errors in the python scripts.";
                        return(false);
                    }

                    Log.Trace("Loader.TryCreatePythonAlgorithm(): Creating IAlgorithm instance.");

                    algorithmInstance = new AlgorithmPythonWrapper(module);
                    ObjectActivator.SetPythonModule(module);
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
                errorMessage = "Loader.TryCreatePythonAlgorithm(): Unable to import python module " + assemblyPath + ". " + e.Message;
            }

            //Successful load.
            return(algorithmInstance != null);
        }