Ejemplo n.º 1
0
        /// <summary>
        /// Init the py interpreter
        /// </summary>
        public static void Initialize()
        {
            pyEngine = Python.CreateEngine(); // Create an engine to access IronPython
            objOps = pyEngine.CreateOperations(); // Performing tasks with the script
            scope = pyEngine.CreateScope(); // Default scope for executing the script

            var paths = pyEngine.GetSearchPaths().ToList();
            //paths.Add(PluginEngine.PluginsPyPath);
            paths.Add(Path.Combine(Folders.ScriptsDirectory, "lib"));

            if (!string.IsNullOrEmpty(TESVSnip.UI.Services.Options.Value.IronPythonDirectory))
                paths.Add(Path.Combine(TESVSnip.UI.Services.Options.Value.IronPythonDirectory, "lib"));
            pyEngine.SetSearchPaths(paths);

            var runtime = pyEngine.Runtime;
            runtime.LoadAssembly(Assembly.GetExecutingAssembly());
            runtime.LoadAssembly(typeof(TESVSnip.Framework.TypeConverter).Assembly);
            runtime.LoadAssembly(typeof(TESVSnip.Domain.Model.BaseRecord).Assembly);
            runtime.LoadAssembly(typeof(String).Assembly);
            runtime.LoadAssembly(typeof(IronPython.Hosting.Python).Assembly);
            runtime.LoadAssembly(typeof(System.Dynamic.DynamicObject).Assembly);

            watcher = new FileSystemWatcher
                {
                    Path = Path.GetFullPath(Path.GetDirectoryName(RendererPyPath)),
                    Filter = Path.GetFileNameWithoutExtension(RendererPyPath) + ".*",
                    IncludeSubdirectories = false
                };
            watcher.Changed += watcher_Changed;
            watcher.EnableRaisingEvents = true;
            watchedFiles.Add(Path.GetFullPath(RendererPyPath));
            watchedFiles.Add(Path.ChangeExtension(RendererPyPath, ".css"));

            Reload();
        }
Ejemplo n.º 2
0
 public DynamicExportObject(Connection conn, ObjectPath object_path, object obj)
     : base(conn, object_path, obj)
 {
     //this.obj = obj;
     ScriptRuntime runtime = ScriptRuntime.CreateFromConfiguration ();
     ops = runtime.CreateOperations ();
 }
Ejemplo n.º 3
0
        internal static dynamic convertFromValue(FScheme.Value exp, ObjectOperations invoker)
        {
            if (exp.IsList)
                return ((FScheme.Value.List)exp).Item.Select(x => convertFromValue(x, invoker)).ToList();
            else if (exp.IsNumber)
                return ((FScheme.Value.Number)exp).Item;
            else if (exp.IsString)
                return ((FScheme.Value.String)exp).Item;
            else if (exp.IsContainer)
                return ((FScheme.Value.Container)exp).Item;
            else if (exp.IsFunction)
            {
                var func = ((FScheme.Value.Function)exp).Item;

                PyWrapper wrapped =
                    args =>
                        convertFromValue(
                            func.Invoke(args.Select(a => convertToValue(a, invoker) as FScheme.Value).ToFSharpList()), invoker);

                return wrapped;
            }
            else
                throw new Exception("Not allowed to pass Functions into a Python Script.");
        }
Ejemplo n.º 4
0
 public static void Shutdown()
 {
     if (watcher != null)
     {
         watcher = null;
         watcher.Changed -= watcher_Changed;
         watcher.EnableRaisingEvents = false;
     }
     source = null;
     compiledCode = null;
     rendererClass = null;
     rendererImpl = null;
     pyEngine = null;
     objOps = null;
 }
Ejemplo n.º 5
0
 public void CreateOperations_MultipleTimes()
 {
     const int n = 5;
     ObjectOperations[] OpA = new ObjectOperations[n];
     for (int i = 0; i < n; i++) {
         Assert.IsNotNull(OpA[i] = _testEng.CreateOperations());
         if (i > 0) Assert.IsTrue(OpA[i] != OpA[i - 1]);
     }
 }
Ejemplo n.º 6
0
        internal static FScheme.Value convertToValue(dynamic data, ObjectOperations invoker)
        {
            if (data is FScheme.Value)
                return data;
            else if (data is string)
                return FScheme.Value.NewString(data);
            else if (data is double || data is int || data is float)
                return FScheme.Value.NewNumber(data);
            else if (data is IEnumerable<dynamic>)
            {
                FSharpList<FScheme.Value> result = FSharpList<FScheme.Value>.Empty;

                //data.reverse(); // this breaks under certain circumstances

                var reversalList = new List<dynamic>();

                foreach (var x in data)
                {
                    reversalList.Add(x);
                }

                for (int i = reversalList.Count - 1; i >= 0; --i)
                {
                    var x = reversalList[i];

                    result = FSharpList<FScheme.Value>.Cons(convertToValue(x, invoker), result);
                }

                //foreach (var x in data)
                //{
                //    result = FSharpList<FScheme.Value>.Cons(convertToValue(x), result);
                //}

                return FScheme.Value.NewList(result);
            }
            else if (data is PythonFunction)
            {
                return
                    FScheme.Value.NewFunction(
                        Utils.ConvertToFSchemeFunc(
                            args =>
                                convertToValue(
                                    invoker.Invoke(
                                        data,
                                        args.Select(x => convertFromValue(x, invoker) as object)
                                            .ToArray()),
                                    invoker)));
            }
            else if (data is PyWrapper)
            {
                var func = data as PyWrapper;
                return
                    FScheme.Value.NewFunction(
                        Utils.ConvertToFSchemeFunc(
                            args =>
                                convertToValue(
                                    func(args.Select(a => convertFromValue(a, invoker)).ToArray()),
                                    invoker)));
            }
            else
                return FScheme.Value.NewContainer(data);
        }
Ejemplo n.º 7
0
 void FreePython()
 {
     prGetNProps = null;
     prFindProp = null;
     prGetPropName = null;
     prGetPropVal = null;
     prSetPropVal = null;
     prIsPropReadable = null;
     prIsPropWritable = null;
     prGetNMethods = null;
     prFindMethod = null;
     prGetMethodName = null;
     prGetNParams = null;
     prGetParamDefValue = null;
     prHasRetVal = null;
     prCallAsProc = null;
     prCallAsFunc = null;
     prCleanAll = null;
     prAddReferences = null;
     prClearExcInfo = null;
     
     ops = null;
     conn_scope = null;
     interf_scope = null;
     interactor = null;
     sruntime.Shutdown();
 }
Ejemplo n.º 8
0
        void InitDLR()
        {
            try
            {
                // Возможно app.config придется упразднять
                // ?собрать IronPython1C
                
#if (ONEPY45)
                ScriptRuntimeSetup setup = ScriptRuntimeSetup.ReadConfiguration(Path.Combine(AssemblyDirectory, "OnePy45.dll.config"));
#else
#if (ONEPY35)
                ScriptRuntimeSetup setup = ScriptRuntimeSetup.ReadConfiguration(Path.Combine(AssemblyDirectory, "OnePy35.dll.config"));
#endif
#endif
                //setup.Options.Add("PreferComDispatch", ScriptingRuntimeHelpers.True);
                sruntime = new ScriptRuntime(setup);
                ScriptEngine eng = sruntime.GetEngine("Python");
                
                #region Установка путей поиска модулей
                var sp = eng.GetSearchPaths();
                sp.Add(Environment.CurrentDirectory);
                sp.Add(Path.Combine(Environment.CurrentDirectory, @"Lib"));
                sp.Add(Path.Combine(Environment.CurrentDirectory, @"Lib\site-packages"));
                sp.Add(Path.Combine(Environment.CurrentDirectory, @"IronPython.lib"));
                sp.Add(Path.Combine(Environment.CurrentDirectory, @"IronPython"));
                sp.Add(Path.Combine(Environment.CurrentDirectory, @"IronPython\DLLs"));
                sp.Add(Path.Combine(Environment.CurrentDirectory, @"IronPython\Lib"));
                sp.Add(Path.Combine(Environment.CurrentDirectory, @"IronPython\Lib\site-packages"));
                sp.Add(Path.Combine(AssemblyDirectory, @"IronPython.lib"));
                sp.Add(Path.Combine(AssemblyDirectory, @"IronPython"));
                sp.Add(Path.Combine(AssemblyDirectory, @"IronPython\DLLs"));
                sp.Add(Path.Combine(AssemblyDirectory, @"IronPython\Lib"));
                sp.Add(Path.Combine(AssemblyDirectory, @"IronPython\Lib\site-packages"));
                
                foreach (string ap in RuntimeConfig.additional_paths)
                {
                    sp.Add(ap);
                }
                
                sp.Add(AssemblyDirectory);
                eng.SetSearchPaths(sp);
                #endregion
                
                ScriptSource conn_src = eng.CreateScriptSource(new AssemblyStreamContentProvider("OnePy.#1"), "cm5ACF5D43F2DA488BB5414714845ACBDE.py");
                ScriptSource interf_src = eng.CreateScriptSource(new AssemblyStreamContentProvider("OnePy.#2"), "interfacing.py");
                
                var comp_options = (PythonCompilerOptions)eng.GetCompilerOptions();
                comp_options.Optimized = false;
                comp_options.Module &= ~ModuleOptions.Optimized; 
                interf_scope = eng.CreateScope();
                interf_src.Compile(comp_options).Execute(interf_scope);
                conn_scope = eng.CreateScope();
                conn_src.Compile(comp_options).Execute(conn_scope);
                ops = eng.CreateOperations();
                Object calcClass = conn_scope.GetVariable("OnePyConnector");
                interactor = new Interactor();
                Object calcObj = ops.Invoke(calcClass, interactor);
                
                #region Получение ссылок на методы
                prGetNProps = ops.GetMember<Func<Object, Object>>(calcObj, "GetNProps");
                prFindProp = ops.GetMember<Func<Object, Object, Object>>(calcObj, "FindProp");
                prGetPropName = ops.GetMember<Func<Object, Object, Object, Object>>(calcObj, "GetPropName");
                prGetPropVal = ops.GetMember<Func<Object, Object, Object>>(calcObj, "GetPropVal");
                prSetPropVal = ops.GetMember<Func<Object, Object, Object>>(calcObj, "SetPropVal");
                prIsPropReadable = ops.GetMember<Func<Object, Object, Object>>(calcObj, "IsPropReadable");
                prIsPropWritable = ops.GetMember<Func<Object, Object, Object>>(calcObj, "IsPropWritable");
                prGetNMethods = ops.GetMember<Func<Object, Object>>(calcObj, "GetNMethods");
                prFindMethod = ops.GetMember<Func<Object, Object, Object>>(calcObj, "FindMethod");
                prGetMethodName = ops.GetMember<Func<Object, Object, Object, Object>>(calcObj, "GetMethodName");
                prGetNParams = ops.GetMember<Func<Object, Object, Object>>(calcObj, "GetNParams");
                prGetParamDefValue = ops.GetMember<Func<Object, Object, Object, Object>>(calcObj, "GetParamDefValue");
                prHasRetVal = ops.GetMember<Func<Object, Object, Object>>(calcObj, "HasRetVal");
                prCallAsProc = ops.GetMember<Func<Object, Object, Object>>(calcObj, "CallAsProc");
                prCallAsFunc = ops.GetMember<Func<Object, Object, Object, Object>>(calcObj, "CallAsFunc");
                prCleanAll = ops.GetMember<Func<Object>>(calcObj, "CleanAll");
                prAddReferences = ops.GetMember<Func<Object, Object>>(calcObj, "AddReferences");
                prClearExcInfo = ops.GetMember<Func<Object>>(calcObj, "ClearExcInfo");
                #endregion

            }
            catch (Exception e)
            {
                ProcessError(e);
                throw;
            }

        }
Ejemplo n.º 9
0
 private void ExecuteRubyDefinition()
 {
     _rubyOperations = _engine.CreateOperations();
     var scope = _engine.CreateScope();
     scope.SetVariable("ctxt", this);
     _engine.ExecuteFile(DefinitionPath, scope);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CSF.Zpt.ExpressionEvaluators.PythonExpressions.PythonExpressionHost"/> class.
 /// </summary>
 public PythonExpressionHost()
 {
     _engine = Python.CreateEngine();
       _operations = _engine.Operations;
 }