public MainViewModel() { ExecuteScriptCommand = new RelayCommand <object>(async _ => await Task.Run(() => { ScriptUnit.Execute(); })); PauseCommand = new RelayCommand <object>(async _ => await Task.Run(() => { ScriptUnit.Break(); })); ResumeCommand = new RelayCommand <object>(async _ => await Task.Run(() => { ScriptUnit.Resume(); })); Script = new PythonScript("./script.py"); ScriptUnit = new PythonScriptUnit(Script, true); ConsoleVM = new ConsoleViewModel(); DeviceA = new Device(); DeviceB = new Device(); ConsoleVM.AppendGlobal("devA", DeviceA); ConsoleVM.AppendGlobal("devB", DeviceB); ScriptUnit.AppendGlobal("devA", DeviceA); ScriptUnit.AppendGlobal("devB", DeviceB); }
public PythonScriptUnit(PythonScript sourceScript, bool enableDebug = true, ScriptScope scriptScope = null) { this.name = sourceScript.Name; this.script = sourceScript; Dictionary <string, object> options = new Dictionary <string, object>(); isDebugEnabled = enableDebug; options["Debug"] = enableDebug; engine = Python.CreateEngine(options); //setup trace delegate traceDelegate = new IronPython.Runtime.Exceptions.TracebackDelegate(TraceCallback); if (enableDebug) { engine.SetTrace(traceDelegate); } if (scriptScope == null) { scope = engine.CreateScope(); } else { scope = scriptScope; } builtinModule = engine.GetBuiltinModule(); builtinModule.SetVariable("WaitAll", new Action <Task[]>((tasks) => Task.WaitAll(tasks))); builtinModule.SetVariable("TaskSleep", new Func <double, Task>((secs) => TaskSleep(secs))); builtinModule.SetVariable("StartLambda", new Func <Func <dynamic>, Task>((action) => Task.Run(action))); builtinModule.SetVariable("AllowBreak", new Action(() => AllowBreak())); builtinModule.SetVariable("Break", new Action(() => Break())); builtinModule.SetVariable("WaitBreak", new Action(() => { })); //empty method so the break will happen there source = engine.CreateScriptSourceFromString(script.Content, SourceCodeKind.File); try { compiled = source.Compile(); currentState = ScriptUnitState.Ready; PublishEvent(new ScriptUnitStateEvent(this, currentState)); } catch (SyntaxErrorException ex) { currentState = ScriptUnitState.Error; PublishEvent(new ScriptUnitStateEvent(this, currentState)); throw ex; } }