Пример #1
0
        public void SetVariable(ScriptScope scope, string name, object value)
        {
            ContractUtils.RequiresNotNull(scope, "scope");
            ContractUtils.RequiresNotNull(name, "name");

            scope.SetVariable(name, value);
        }
Пример #2
0
        public static void AddVariables(ScriptingHosting.ScriptScope scope, IEnumerable <KeyValuePair <string, object> > variables)
        {
            foreach (var kv in variables)
            {
                scope.SetVariable(kv.Key, kv.Value);
            }

            return;
        }
Пример #3
0
    /// <summary>
    /// Constructor. Creates a Python engine and a main scope where scripts can
    /// be executed. Also creates modules that can be added in the main scope.
    /// The standard output channel is changed in the constructor so that
    /// logging can be done to the provided console via the LogToConsole
    /// delegate.
    /// </summary>
    /// <param name="logToConsole">
    /// Function used to log to console. Injected to ScriptEngine.\
    /// </param>
    public ScriptEngine(LogToConsole logToConsole)
    {
        _logToConsole = logToConsole;
        _engine       = Python.CreateEngine();

        // Create the main scope
        _mainScope = _engine.CreateScope();

        // This expression is used when initializing the scope. Changing the
        // standard output channel. Needs the function 'write' to be defined.
        string initExpression = @"
import sys
sys.stdout = standardOutput";

        _mainScope.SetVariable("standardOutput", this);

        // Run initialization, also executes the main config file.
        ExecuteScript(initExpression);
    }
Пример #4
0
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = new MainPage();

            var runtime = Silverlight.DynamicEngine.CreateRuntime();
            _python = runtime.GetEngine("python");

            _scope = _python.CreateScope();
            _repl = Silverlight.Repl.Show(_python, _scope);
            _scope.SetVariable("app", this);

            try {
                test("Execute strings", "4", "2 + 2");
                test("Import .NET namespace", "hi", @"import System
            System.String('hi')");
                _python.Execute(@"import foo
            foo.test(app)
            foo.test_import(app)", _scope);
            } catch(Exception ex) {
                _repl.OutputBuffer.WriteLine("[FAIL]");
                _repl.OutputBuffer.Write(_python.GetService<Hosting.ExceptionOperations>().FormatException(ex));
            }
        }
        public Script(bool redirectOutput = false, DataDisplay datadisplayer = null)
        {
            Dictionary <string, object> options = new Dictionary <string, object>();

            options["Debug"] = true;

            if (engine != null)
            {
                engine.Runtime.Shutdown();
            }

            engine = Python.CreateEngine(options);

            var paths = engine.GetSearchPaths();

            paths.Add(Settings.GetRunningDirectory() + "Lib.zip");
            paths.Add(Settings.GetRunningDirectory() + "lib");
            paths.Add(Settings.GetRunningDirectory());
            engine.SetSearchPaths(paths);

            scope = engine.CreateScope();


            var all  = System.Reflection.Assembly.GetExecutingAssembly();
            var asss = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var ass in asss)
            {
                engine.Runtime.LoadAssembly(ass);
            }

            scope.SetVariable("Ports", MainV2.Comports);
            scope.SetVariable("MAV", MainV2.comPort);
            scope.SetVariable("cs", MainV2.comPort.MAV.cs);
            scope.SetVariable("Script", this);
            scope.SetVariable("mavutil", this);
            scope.SetVariable("Joystick", MainV2.joystick);
            //added classes for DAS
            dataDisplay = datadisplayer;
            scope.SetVariable("DAS", dataDisplay);


            engine.CreateScriptSourceFromString("print 'hello world from python'").Execute(scope);
            engine.CreateScriptSourceFromString("print cs.roll").Execute(scope);

            if (redirectOutput)
            {
                //Redirect output through this writer
                //this writer will not actually write to the memorystreams
                OutputWriter = new Utilities.StringRedirectWriter();
                engine.Runtime.IO.SetErrorOutput(new MemoryStream(), OutputWriter);
                engine.Runtime.IO.SetOutput(new MemoryStream(), OutputWriter);
            }
            else
            {
                OutputWriter = null;
            }

            /*
             * object thisBoxed = MainV2.comPort.MAV.cs;
             * Type test = thisBoxed.GetType();
             *
             * foreach (var field in test.GetProperties())
             * {
             *  // field.Name has the field's name.
             *  object fieldValue;
             *  try
             *  {
             *      fieldValue = field.GetValue(thisBoxed, null); // Get value
             *  }
             *  catch { continue; }
             *
             *  // Get the TypeCode enumeration. Multiple types get mapped to a common typecode.
             *  TypeCode typeCode = Type.GetTypeCode(fieldValue.GetType());
             *
             *  items.Add(field.Name);
             * }
             */
        }