コード例 #1
0
        private string GetVariableText(TaskInfo info)
        {
            string          text = info.Text;
            MatchCollection mc   = s_regexOwnData.Matches(text);

            foreach (Match m in mc)
            {
                string s = string.Empty;
                switch (m.Groups[1].Value.ToUpper())
                {
                case "COUNT":
                    if (!string.IsNullOrEmpty(info.SearchManagerClassName))
                    {
                        ISearchManager searchManager = ServiceProvider.GetService <IManagerFactory>().GenerateSearchManager(info.SearchManagerClassName, info.SearchManagerClassParams);
                        s = searchManager.GetCount(SearchExpression.Parse(EntityHelper.ReplaceExpression(info.SearchExpression))).ToString();

                        m_sms[info.Name] = searchManager;
                    }
                    break;

                case "EXP":
                    string exp = m.Groups[3].Value.Trim();
                    object ret = PythonHelper.ExecutePythonExpression(exp, null);
                    s = ret == null ? string.Empty : ret.ToString();
                    break;

                default:
                    throw new NotSupportedException("Invalid text of " + text);
                }
                text = text.Replace(m.Groups[0].Value, s);
            }
            return(text);
        }
コード例 #2
0
        /// <summary>
        /// 执行Python。通过source和是否存在文件自动判别执行类型
        /// 如果是文件(.py结尾)
        /// 首先在Script目录下寻找.py源文件名,再在数据库ResourceInfo中寻找
        /// 再在Script目录下寻找单个.py编译成的同名Assembly。
        /// 如果还找不到,则在工作目录下找"PythonScript.dll,执行对应方法。
        /// 在.py文件中,执行的是execute方法(如果带当前窗口参数,参数是masterForm)
        /// 如果不是,则按照Statement执行。
        /// </summary>
        /// <param name="source"></param>
        /// <param name="processParams"></param>
        /// <returns></returns>
        public static object TryExecutePython(string source, Dictionary <string, object> processParams)
        {
            try
            {
                if (source.EndsWith(".py", StringComparison.InvariantCulture))
                {
                    string filePath = ServiceProvider.GetService <IApplicationDirectory>().GetLocalResourcePath(source);
                    if (System.IO.File.Exists(filePath))
                    {
                        return(PythonHelper.ExecutePythonFile(filePath, processParams));
                    }
                    else
                    {
                        ResourceContent pythonResource = ResourceInfoHelper.ResolveResource(source, ResourceType.PythonSource);

                        if (pythonResource != null)
                        {
                            switch (pythonResource.Type)
                            {
                            case ResourceContentType.File:
                                return(PythonHelper.ExecutePythonFile(pythonResource.Content.ToString(), processParams));

                            case ResourceContentType.String:
                                return(PythonHelper.ExecutePythonStatement(pythonResource.Content.ToString(), processParams));

                            default:
                                throw new NotSupportedException("Invalid Resource Content Type!");
                            }
                        }
                        else
                        {
                            string singleAssemblyPath = System.IO.Path.ChangeExtension(filePath, ".dll");
                            if (System.IO.File.Exists(singleAssemblyPath))
                            {
                                return(PythonHelper.ExecutePythonAssembly(singleAssemblyPath, System.IO.Path.GetFileName(filePath), processParams));
                            }
                            else
                            {
                                string combileAssemblyPath = System.IO.Path.Combine(ServiceProvider.GetService <IApplicationDirectory>().GetMainDirectory(), s_pythonCompiledAssembly);
                                if (System.IO.File.Exists(combileAssemblyPath))
                                {
                                    return(PythonHelper.ExecutePythonAssembly(combileAssemblyPath, System.IO.Path.GetFileName(filePath), processParams));
                                }
                                else
                                {
                                    return(null);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (source.Contains("result = "))
                    {
                        return(PythonHelper.ExecutePythonStatement(source, processParams));
                    }
                    else
                    {
                        return(PythonHelper.ExecutePythonExpression(source, processParams));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ArgumentException(string.Format("Python source {0} is invalid!", source), ex);
            }
        }