Exemple #1
0
        /// <summary>
        /// Parse a string, executing python code as required.  Code snippets should be surrounded by braces, and can
        /// return any type of value - although if the python class does not implement a sensible <c>__str__</c>, it
        /// will not make much sense.
        /// </summary>
        /// <example>
        /// (in python)
        /// <code para="str">
        /// variable_a = "foo";
        /// variable_b = "bar"
        /// </code>
        /// (in c#)
        /// <code>
        /// ParseStringForVariable("{variable_a}{variable_b}") //returns "foobar"
        /// </code>
        /// </example>
        /// <param name="str">The string to parse</param>
        /// <returns>
        /// String
        /// </returns>
        /// <remarks>This has been constructed to be as robust as possible, but as always, doing silly things will
        /// result in silly things happening.  For instance <c>__import__("sys").execute("yes | rm -rf /")</c> will
        /// do exactly what you expect on a linux machine. However, it has yet to be seen if it is possible to
        /// break the parser to execute such code without it being clearly visible.
        /// <para/>
        /// The end user should always remember to treat jobs as executables, and not to run anything received from
        /// an untrusted source without carefully checking it over first.</remarks>
        protected String ParseStringForVariable([NotNull] String str)
        {
            OnMessage(MessageLevel.Debug, "Attempting to parse string {0}", str);

            IPythonEngine pythonEngine = GetCurrentCore().PythonEngine;

            string orig        = str;
            var    expressions = FindPythonExpressions(str);

            foreach (Expression expression in expressions)
            {
                string result = expression.Template;
                try
                {
                    this.OnMessage(MessageLevel.Debug, "Python expression found: {0}", expression.Code);
                    dynamic r = pythonEngine.Evaluate(expression.Compiled);
                    result = r.ToString();
                    this.OnMessage(MessageLevel.Debug, "Expression evaluated to {0}", result);
                }
                catch (PythonException e)
                {
                    this.OnMessage(MessageLevel.Debug,
                                   "Expression could not be evaluated: Python raised exception {0}",
                                   e.Message);
                }
                str = str.Replace(expression.Template, result);
                //break;
            }


            OnMessage(MessageLevel.Debug, "String {0} parsed to {1}", orig, str);

            return(str);
        }
        /// <summary>
        /// Perform the action
        /// </summary>
        protected override void DoAction()
        {
            ParseStringForVariable(this.DestinationVarName);

            IPythonEngine engine = GetCurrentCore().PythonEngine;
            var           scope  = engine.GetNewTypedScope(Variables);

            Variables[DestinationVarName] = engine.Evaluate <object>(Expresson, scope);
        }
Exemple #3
0
 private dynamic TryEvaluate(String str)
 {
     try
     {
         IPythonEngine engine    = GetCurrentCore().PythonEngine;
         var           evaluated = engine.Evaluate(str);
         return(evaluated);
     }
     catch (PythonException)
     {
         return(str);
     }
 }
Exemple #4
0
        protected String OldParseStringForVariable([NotNull] String str)
        {
            IPythonEngine pythonEngine = GetCurrentCore().PythonEngine;

            Regex           expressionRegex = new Regex(@"(?<template>{(?<expression>.*?)})", RegexOptions.ExplicitCapture);
            MatchCollection matches         = expressionRegex.Matches(str);

            var enumerable = from Match match in matches
                             select(string) pythonEngine.Evaluate(match.Groups[@"expression"].Value).ToString();

            string injected = enumerable.Aggregate(str, expressionRegex.Replace);


            OnMessage(MessageLevel.Core, "String {0} parsed to {1}", str, injected);
            return(injected);
        }