Exemplo n.º 1
0
 public string CallFunction(
     string function,
     List <object> arguments  = null,
     EscapeFlags escapeMethod = EscapeFlags.Quotes | EscapeFlags.StripNullItems
     )
 {
     return(CallFunction(new PythonFunction(function), arguments, escapeMethod));
 }
Exemplo n.º 2
0
        /// <summary>
        /// Writes the text representation of the specified object to kodi.log with "sys.stdout.write"
        /// </summary>
        /// <param name="value"></param>
        public static void Write(object value, EscapeFlags escapeMethod = EscapeFlags.Quotes)
        {
            string valueStr = value.ToString();

            valueStr = PythonInterop.EscapeArgument(valueStr, escapeMethod);

            PythonInterop.Eval(string.Format("sys.stdout.write({0})", valueStr));
        }
Exemplo n.º 3
0
        public static void WriteLine(string value, EscapeFlags escapeMethod = EscapeFlags.Quotes)
        {
            string valueStr;

            valueStr  = PythonInterop.EscapeArgument(value, escapeMethod);
            valueStr += PythonInterop.EscapeArgument(NewLine, EscapeFlags.Quotes);
            PythonInterop.Eval(string.Format("sys.stdout.write({0})", valueStr));
        }
Exemplo n.º 4
0
 public dynamic CallFunction(
     string function,
     List <object> arguments  = null,
     EscapeFlags escapeMethod = EscapeFlags.Quotes | EscapeFlags.StripNullItems
     )
 {
     return(CallFunction(PyFunction.ClassFunction(function), arguments, escapeMethod));
 }
Exemplo n.º 5
0
 public dynamic CallAssign(
     string function,
     List <object> arguments  = null,
     EscapeFlags escapeMethod = EscapeFlags.Quotes | EscapeFlags.StripNullItems,
     PyVariable target        = null
     )
 {
     return(CallAssign(PyFunction.ClassFunction(function), arguments, escapeMethod, target));
 }
Exemplo n.º 6
0
        public static string EscapeArgument(object argument, EscapeFlags escapeMethod = EscapeFlags.Quotes)
        {
            if (argument == null)
            {
                return("None");
            }

            //If it's a variable, return its unquoted python name
            if (argument is PyVariable)
            {
                return((argument as PyVariable).PyName);
            }

            string text = argument.ToString();

            //TODO: Find a better way
            //Invokes Enum.GetString<T>(argument)
            if (argument is Enum)
            {
                text = typeof(EnumExtensions)
                       .GetMethod("GetString")
                       .MakeGenericMethod(argument.GetType())
                       .Invoke(null, new object[] { argument }) as string;
            }

            //Don't escape primitives
            if (
                argument is bool ||
                argument is int ||
                argument is uint ||
                argument is long ||
                argument is ulong ||
                argument is float ||
                argument is double
                )
            {
                return(text);
            }

            if (escapeMethod.HasFlag(EscapeFlags.Quotes))
            {
                text = Regex.Replace(text, "\r?\n", "\\n");
                text = '"' + text.Replace("\"", "\\\"") + '"';
            }
            if (escapeMethod.HasFlag(EscapeFlags.EscapeBuiltin))
            {
                text = Regex.Replace(text, ",", "\\,");
            }
            if (escapeMethod.HasFlag(EscapeFlags.RawString))
            {
                text = "r'" + text + "'";
            }

            return(text);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Writes the text representation of the specified object to kodi.log with "sys.stdout.write"
        /// </summary>
        /// <param name="value"></param>
        public static void Write(object value, EscapeFlags escapeMethod = EscapeFlags.Quotes)
        {
            string valueStr = value.ToString();

            if (KodiBridge.RunningAddon.DebugEnabled)
            {
                Console.Error.WriteLine($"[PyConsole] {valueStr}");
            }
            valueStr = PythonInterop.EscapeArgument(valueStr, escapeMethod);

            PythonInterop.Eval(string.Format("sys.stdout.write({0})", valueStr));
        }
Exemplo n.º 8
0
        public string CallFunction(
            PythonFunction function,
            List <object> arguments  = null,
            EscapeFlags escapeMethod = EscapeFlags.Quotes | EscapeFlags.StripNullItems
            )
        {
            if (arguments == null)
            {
                arguments = new List <object>();
            }

            List <string> textArguments = PythonInterop.EscapeArguments(arguments, escapeMethod);

            return(CallFunction(function, string.Join(", ", textArguments)));
        }
Exemplo n.º 9
0
        public dynamic CallFunction(
            PyFunction function,
            List <object> arguments  = null,
            EscapeFlags escapeMethod = EscapeFlags.Quotes | EscapeFlags.StripNullItems,
            PyVariable target        = null
            )
        {
            if (arguments == null)
            {
                arguments = new List <object>();
            }

            List <string> textArguments = PythonInterop.EscapeArguments(arguments, escapeMethod);

            return(CallFunction(function, string.Join(", ", textArguments), target: target));
        }
Exemplo n.º 10
0
        public string CallFunction(
            PythonFunction function,
            List <object> arguments  = null,
            EscapeFlags escapeMethod = EscapeFlags.Quotes | EscapeFlags.StripNullItems
            )
        {
            if (arguments == null)
            {
                arguments = new List <object>();
            }

            List <string> textArguments = PythonInterop.EscapeArguments(arguments, escapeMethod);

            return(PythonInterop.EvalToResult(string.Format("{0}.{1}({2})",
                                                            this.PyName, function.Function, string.Join(", ", textArguments)
                                                            )));
        }
Exemplo n.º 11
0
        public static string EscapeArgument(object argument, EscapeFlags escapeMethod = EscapeFlags.Quotes)
        {
            if (argument == null)
            {
                return("None");
            }

            //If it's a variable, return it's unquoted python name
            if (argument is PyVariable)
            {
                return((argument as PyVariable).PyName);
            }

            string text = argument.ToString();

            //Don't escape primitives
            if (
                argument is bool ||
                argument is int ||
                argument is uint ||
                argument is long ||
                argument is ulong ||
                argument is float ||
                argument is double
                )
            {
                return(text);
            }

            if (escapeMethod.HasFlag(EscapeFlags.Quotes))
            {
                text = Regex.Replace(text, "\r?\n", "\\n");
                text = '"' + text.Replace("\"", "\\\"") + '"';
            }
            if (escapeMethod.HasFlag(EscapeFlags.EscapeBuiltin))
            {
                text = Regex.Replace(text, ",", "\\,");
            }
            if (escapeMethod.HasFlag(EscapeFlags.RawString))
            {
                text = "r'" + text + "'";
            }

            return(text);
        }
Exemplo n.º 12
0
        public static string ToPythonCode(this string text, EscapeFlags flags = EscapeFlags.Quotes)
        {
            if (flags.HasFlag(EscapeFlags.Quotes))
            {
                text = Regex.Replace(text, "\r?\n", "\\n");
                text = '"' + text.Replace("\"", "\\\"") + '"';
            }
            if (flags.HasFlag(EscapeFlags.EscapeBuiltin))
            {
                text = Regex.Replace(text, ",", "\\,");
            }
            if (flags.HasFlag(EscapeFlags.RawString))
            {
                text = "r'" + text + "'";
            }

            return(text);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Calls the specified function and assigns the result to this variable
        /// </summary>
        /// <param name="function"></param>
        /// <param name="arguments"></param>
        /// <param name="escapeMethod"></param>
        /// <returns></returns>
        public string CallAssign(
            PythonFunction function,
            List <object> arguments  = null,
            EscapeFlags escapeMethod = EscapeFlags.Quotes | EscapeFlags.StripNullItems
            )
        {
            string argumentsText = "";

            if (arguments != null)
            {
                List <string> textArguments = PythonInterop.EscapeArguments(arguments, escapeMethod);
                argumentsText = string.Join(", ", textArguments);
            }

            PythonInterop.EvalToVar(this, "{0}({1})", new List <object> {
                function.ToString(), argumentsText
            }, EscapeFlags.None);

            return(this.Value);
        }
Exemplo n.º 14
0
        public static PythonEvalReply EvalToVar(PyVariable variable, string codeFormat, List <object> arguments, EscapeFlags escapeMethod)
        {
            List <string> textArguments = EscapeArguments(arguments, escapeMethod);

            return(EvalToVar(variable, string.Format(codeFormat, textArguments.ToArray())));
        }
Exemplo n.º 15
0
 public static dynamic CallFunction(PyModule module, string functionName, List <object> arguments = null, EscapeFlags flags = EscapeFlags.Quotes)
 {
     return(CallFunction(new PyFunction(module, functionName), arguments, flags));
 }
Exemplo n.º 16
0
        public static dynamic CallFunction(PyFunction pythonFunction, IEnumerable <object> arguments = null, EscapeFlags flags = EscapeFlags.Quotes)
        {
            List <string> textArguments = null;

            if (arguments != null)
            {
                textArguments = EscapeArguments(arguments, flags);
            }

            return(CallFunction(
                       pythonFunction.ToString(),
                       textArguments
                       ));
        }
Exemplo n.º 17
0
 public static List <string> EscapeArguments(EscapeFlags escapeMethod = EscapeFlags.Quotes, params object[] arguments)
 {
     return(EscapeArguments(arguments, escapeMethod));
 }