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);
        }
        public static List <string> EscapeArguments(IEnumerable <object> arguments, EscapeFlags escapeMethod = EscapeFlags.Quotes)
        {
            List <string> textArguments = new List <string>();

            int count = arguments.Count();

            if (escapeMethod.HasFlag(EscapeFlags.StripNullItems) && count > 0)
            {
                List <object> argumentsList = arguments.ToList();
                int           nulls         = 0;
                // Start from end, go backwards
                for (int i = count - 1; i >= 0; --i)
                {
                    // Found the end of the null series
                    if (argumentsList[i] != null)
                    {
                        argumentsList.RemoveRange(i + 1, nulls);
                        break;
                    }
                    nulls++;
                }
                if (count == nulls)
                {
                    argumentsList.Clear();
                }
                arguments = argumentsList;
            }

            foreach (object argument in arguments)
            {
                textArguments.Add(EscapeArgument(argument, escapeMethod));
            }

            return(textArguments);
        }
Exemple #3
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);
        }
        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);
        }