Example #1
0
        public static string RunScript(CMSDataContext db, string script)
        {
            if (!script.HasValue())
            {
                return("no VitalStats script");
            }

            var qf     = new QueryFunctions(db);
            var engine = Python.CreateEngine();
            var ms     = new MemoryStream();
            var sw     = new StreamWriter(ms);

            engine.Runtime.IO.SetOutput(ms, sw);
            engine.Runtime.IO.SetErrorOutput(ms, sw);
            var sc = engine.CreateScriptSourceFromString(script);

            try
            {
                var code  = sc.Compile();
                var scope = engine.CreateScope();
                scope.SetVariable("q", qf);
                scope.SetVariable("db", db);
                code.Execute(scope);
                ms.Position = 0;
                var sr = new StreamReader(ms);
                return(sr.ReadToEnd());
            }
            catch (Exception ex)
            {
                return("VitalStats script error: " + ex.Message);
            }
        }
Example #2
0
        public static string VitalStats(CMSDataContext Db)
        {
            var qf = new QueryFunctions(Db);
            var script = Db.Content("VitalStats");
            if (script == null)
                return "no VitalStats script";
            #if DEBUG2
            var options = new Dictionary<string, object>();
            options["Debug"] = true;
            var engine = Python.CreateEngine(options);
            var paths = engine.GetSearchPaths();
            paths.Add(path);
            engine.SetSearchPaths(paths);
            var sc = engine.CreateScriptSourceFromFile(HttpContext.Current.Server.MapPath("/MembershipAutomation2.py"));
            #else
            var engine = Python.CreateEngine();
            var sc = engine.CreateScriptSourceFromString(script.Body);
            #endif

            try
            {
                var code = sc.Compile();
                var scope = engine.CreateScope();
                code.Execute(scope);

                dynamic VitalStats = scope.GetVariable("VitalStats");
                dynamic m = VitalStats();
                return m.Run(qf);
            }
            catch (Exception ex)
            {
                return "VitalStats script error: " + ex.Message;
            }
        }
Example #3
0
        public static string VitalStats(CMSDataContext db)
        {
            var script = db.Content("VitalStats");

            if (script == null)
            {
                return("no VitalStats script");
            }

            if (!script.Body.Contains("class VitalStats"))
            {
                return(RunScript(db, script.Body));
            }

            var engine = Python.CreateEngine();
            var sc     = engine.CreateScriptSourceFromString(script.Body);
            var qf     = new QueryFunctions(db);

            try
            {
                var code  = sc.Compile();
                var scope = engine.CreateScope();
                code.Execute(scope);

                dynamic vitalStats = scope.GetVariable("VitalStats");
                dynamic m          = vitalStats();
                return(m.Run(qf));
            }
            catch (Exception ex)
            {
                return("VitalStats script error: " + ex.Message);
            }
        }
Example #4
0
        }                                  // this is set automatically for the output page

        public PythonEvents(string dbname, string script)
        {
            var engine = Python.CreateEngine();
            var ms     = new MemoryStream();
            var sw     = new StreamWriter(ms);

            engine.Runtime.IO.SetOutput(ms, sw);
            engine.Runtime.IO.SetErrorOutput(ms, sw);
            var sc    = engine.CreateScriptSourceFromString(script);
            var code  = sc.Compile();
            var scope = engine.CreateScope();

            db      = new CMSDataContext(Util.GetConnectionString(dbname));
            db.Host = dbname;
            scope.SetVariable("model", this);
            var qf = new QueryFunctions(db);

            scope.SetVariable("q", qf);
            code.Execute(scope);
            db.SubmitChanges();
            ms.Position = 0;
            var sr = new StreamReader(ms);

            Output = sr.ReadToEnd();
        }
Example #5
0
        public static string RunScript(string dbname, string script)
        {
            var engine = Python.CreateEngine();
            var ms     = new MemoryStream();
            var sw     = new StreamWriter(ms);

            engine.Runtime.IO.SetOutput(ms, sw);
            engine.Runtime.IO.SetErrorOutput(ms, sw);
            var sc = engine.CreateScriptSourceFromString(script);

            try
            {
                var code  = sc.Compile();
                var scope = engine.CreateScope();
                var pe    = new PythonEvents(dbname);
                scope.SetVariable("model", pe);
                var qf = new QueryFunctions(pe.db);
                scope.SetVariable("q", qf);
                code.Execute(scope);
                pe.db.SubmitChanges();
                ms.Position = 0;
                var sr = new StreamReader(ms);
                var s  = sr.ReadToEnd();
                return(s);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Example #6
0
        public string CallScript(string scriptname)
        {
            var script = db.ContentOfTypePythonScript(scriptname);
            var engine = Python.CreateEngine();
            var ms     = new MemoryStream();
            var sw     = new StreamWriter(ms);

            engine.Runtime.IO.SetOutput(ms, sw);
            engine.Runtime.IO.SetErrorOutput(ms, sw);
            var sc    = engine.CreateScriptSourceFromString(script);
            var code  = sc.Compile();
            var scope = engine.CreateScope();
            var pe    = new PythonEvents(db.Host);

            scope.SetVariable("model", pe);
            var qf = new QueryFunctions(pe.db);

            scope.SetVariable("q", qf);
            code.Execute(scope);
            pe.db.SubmitChanges();
            ms.Position = 0;
            var sr = new StreamReader(ms);
            var s  = sr.ReadToEnd();

            return(s);
        }
Example #7
0
        public static string RunScript(CMSDataContext db, string script)
        {
            if (!script.HasValue())
                return "no VitalStats script";

            var qf = new QueryFunctions(db);
            var engine = Python.CreateEngine();
            var ms = new MemoryStream();
            var sw = new StreamWriter(ms);
            engine.Runtime.IO.SetOutput(ms, sw);
            engine.Runtime.IO.SetErrorOutput(ms, sw);
            var sc = engine.CreateScriptSourceFromString(script);
            try
            {
                var code = sc.Compile();
                var scope = engine.CreateScope();
                scope.SetVariable("q", qf);
                scope.SetVariable("db", db);
                code.Execute(scope);
                ms.Position = 0;
                var sr = new StreamReader(ms);
                return sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                return "VitalStats script error: " + ex.Message;
            }
        }
Example #8
0
        public static string ExecutePython(string script, PythonModel model, bool fromFile = false)
        {
            var engine = fromFile
                ? Python.CreateEngine(new Dictionary <string, object> {
                ["Debug"] = true
            })
                : Python.CreateEngine();
            var pc    = HostingHelpers.GetLanguageContext(engine) as PythonContext;
            var hooks = pc?.SystemState.Get__dict__()["path_hooks"] as List;

            hooks?.Clear();
            var searchPaths = engine.GetSearchPaths();

            searchPaths.Add(ConfigurationManager.AppSettings["PythonLibPath"]);
            engine.SetSearchPaths(searchPaths);

            using (var ms = new MemoryStream())
                using (var sw = new StreamWriter(ms))
                {
                    engine.Runtime.IO.SetOutput(ms, sw);
                    engine.Runtime.IO.SetErrorOutput(ms, sw);

                    try
                    {
                        var sc = fromFile
                        ? engine.CreateScriptSourceFromFile(script)
                        : engine.CreateScriptSourceFromString(script);
                        var code = sc.Compile();

                        var scope = engine.CreateScope();
                        scope.SetVariable("model", model);
                        scope.SetVariable("Data", model.Data);

                        var qf = new QueryFunctions(model.db, model.dictionary);
                        scope.SetVariable("q", qf);
                        code.Execute(scope);

                        ms.Position = 0;

                        using (var sr = new StreamReader(ms))
                        {
                            var s = sr.ReadToEnd();
                            return(s.Replace("\r\r\n", "\n"));
                        }
                    }
                    catch (Exception ex)
                    {
                        var err = engine.GetService <ExceptionOperations>().FormatException(ex);
                        throw new Exception(err);
                    }
                }
        }
Example #9
0
        private static string ExecutePython(string scriptContent, PythonModel model)
        {
            var engine = Python.CreateEngine();

//            var libs = new []
//            {
//                @"E:\GitHub\bvcms\CmsWeb\Lib"
//            };
//            engine.SetSearchPaths(libs);

            using (var ms = new MemoryStream())
                using (var sw = new StreamWriter(ms))
                {
                    engine.Runtime.IO.SetOutput(ms, sw);
                    engine.Runtime.IO.SetErrorOutput(ms, sw);

                    try
                    {
                        var sc   = engine.CreateScriptSourceFromString(scriptContent);
                        var code = sc.Compile();

                        var scope = engine.CreateScope();
                        scope.SetVariable("model", model);
                        scope.SetVariable("Data", model.Data);

                        var qf = new QueryFunctions(model.db, model.dictionary);
                        scope.SetVariable("q", qf);
                        code.Execute(scope);

                        ms.Position = 0;

                        using (var sr = new StreamReader(ms))
                        {
                            var s = sr.ReadToEnd();
                            return(s);
                        }
                    }
                    catch (Exception ex)
                    {
                        var err = engine.GetService <ExceptionOperations>().FormatException(ex);
                        throw new Exception(err);
                    }
                }
        }
Example #10
0
        public static string OldVitalStats(CMSDataContext db, string script)
        {
            var engine = Python.CreateEngine();
            var sc = engine.CreateScriptSourceFromString(script);
            var qf = new QueryFunctions(db);

            try
            {
                var code = sc.Compile();
                var scope = engine.CreateScope();
                code.Execute(scope);

                dynamic vitalStats = scope.GetVariable("VitalStats");
                dynamic m = vitalStats();
                return m.Run(qf);
            }
            catch (Exception ex)
            {
                return "VitalStats script error: " + ex.Message;
            }
        }
Example #11
0
        public static string OldVitalStats(CMSDataContext db, string script)
        {
            var engine = Python.CreateEngine();
            var sc     = engine.CreateScriptSourceFromString(script);
            var qf     = new QueryFunctions(db);

            try
            {
                var code  = sc.Compile();
                var scope = engine.CreateScope();
                code.Execute(scope);

                dynamic vitalStats = scope.GetVariable("VitalStats");
                dynamic m          = vitalStats();
                return(m.Run(qf));
            }
            catch (Exception ex)
            {
                return("VitalStats script error: " + ex.Message);
            }
        }
        public static string ExecutePython(string script, PythonModel model, bool fromFile = false)
        {
            ScriptEngine engine = CreateEngine(fromFile);

            using (var ms = new MemoryStream())
                using (var sw = new StreamWriter(ms))
                {
                    engine.Runtime.IO.SetOutput(ms, sw);
                    engine.Runtime.IO.SetErrorOutput(ms, sw);

                    try
                    {
                        var sc = fromFile
                        ? engine.CreateScriptSourceFromFile(script)
                        : engine.CreateScriptSourceFromString(script);
                        var code = sc.Compile();

                        var scope = engine.CreateScope();
                        scope.SetVariable("model", model);
                        scope.SetVariable("Data", model.Data);

                        var qf = new QueryFunctions(model.db, model.dictionary);
                        scope.SetVariable("q", qf);
                        code.Execute(scope);

                        ms.Position = 0;

                        using (var sr = new StreamReader(ms))
                        {
                            var s = sr.ReadToEnd();
                            return(s.Replace("\r\r\n", "\n"));
                        }
                    }
                    catch (Exception ex)
                    {
                        var err = engine.GetService <ExceptionOperations>().FormatException(ex);
                        throw new Exception(err);
                    }
                }
        }
Example #13
0
        public static string VitalStats(CMSDataContext Db)
        {
            var qf     = new QueryFunctions(Db);
            var script = Db.Content("VitalStats");

            if (script == null)
            {
                return("no VitalStats script");
            }
#if DEBUG2
            var options = new Dictionary <string, object>();
            options["Debug"] = true;
            var engine = Python.CreateEngine(options);
            var paths  = engine.GetSearchPaths();
            paths.Add(path);
            engine.SetSearchPaths(paths);
            var sc = engine.CreateScriptSourceFromFile(HttpContext.Current.Server.MapPath("/MembershipAutomation2.py"));
#else
            var engine = Python.CreateEngine();
            var sc     = engine.CreateScriptSourceFromString(script.Body);
#endif

            try
            {
                var code  = sc.Compile();
                var scope = engine.CreateScope();
                code.Execute(scope);

                dynamic VitalStats = scope.GetVariable("VitalStats");
                dynamic m          = VitalStats();
                return(m.Run(qf));
            }
            catch (Exception ex)
            {
                return("VitalStats script error: " + ex.Message);
            }
        }
Example #14
0
        private static string ExecutePython(string scriptContent, PythonEvents model)
        {
            // we could consider only passing in an explicit IPythonApi to the script so that only things defined
            // on the interface are accessible to the script; however, I'm worried that we may have some scripts
            // that are using functions not defined on the API docs site (which is what the interface is based on)

            if (scriptContent.StartsWith("@# pyhtml #@"))
                scriptContent = RunPyHtml(scriptContent, model.pythonPath, model.pyrazorPath);

            var engine = Python.CreateEngine();

            using (var ms = new MemoryStream())
            using (var sw = new StreamWriter(ms))
            {
                engine.Runtime.IO.SetOutput(ms, sw);
                engine.Runtime.IO.SetErrorOutput(ms, sw);

                try
                {
                    var sc = engine.CreateScriptSourceFromString(scriptContent);
                    var code = sc.Compile();

                    var scope = engine.CreateScope();
                    scope.SetVariable("model", model);

                    var qf = new QueryFunctions(model.db);
                    scope.SetVariable("q", qf);
                    code.Execute(scope);

                    model.db.SubmitChanges();

                    ms.Position = 0;

                    using (var sr = new StreamReader(ms))
                    {
                        var s = sr.ReadToEnd();
                        return s;
                    }
                }
                catch (Exception ex)
                {
                    var err = engine.GetService<ExceptionOperations>().FormatException(ex);
                    throw new Exception(err);
                }
            }
        }
Example #15
0
 public static string RunScript(CMSDataContext db, string script)
 {
     var engine = Python.CreateEngine();
     var ms = new MemoryStream();
     var sw = new StreamWriter(ms);
     engine.Runtime.IO.SetOutput(ms, sw);
     engine.Runtime.IO.SetErrorOutput(ms, sw);
     var sc = engine.CreateScriptSourceFromString(script);
     var code = sc.Compile();
     var scope = engine.CreateScope();
     var pe = new PythonEvents(db);
     scope.SetVariable("model", pe);
     var qf = new QueryFunctions(db);
     scope.SetVariable("q", qf);
     code.Execute(scope);
     ms.Position = 0;
     var sr = new StreamReader(ms);
     var s = sr.ReadToEnd();
     return s;
 }
Example #16
0
        public static string VitalStats(CMSDataContext Db)
        {
            var qf = new QueryFunctions(Db);
            var script = Db.Content("VitalStats");
            if (script == null)
                return "no VitalStats script";

            if (!script.Body.Contains("class VitalStats"))
                return RunScript(Db, script.Body);

            var engine = Python.CreateEngine();
            var sc = engine.CreateScriptSourceFromString(script.Body);

            try
            {
                var code = sc.Compile();
                var scope = engine.CreateScope();
                code.Execute(scope);

                dynamic VitalStats = scope.GetVariable("VitalStats");
                dynamic m = VitalStats();
                return m.Run(qf);
            }
            catch (Exception ex)
            {
                return "VitalStats script error: " + ex.Message;
            }
        }
Example #17
0
        private static string ExecutePython(string scriptContent, PythonEvents model)
        {
            var engine = Python.CreateEngine();

            using (var ms = new MemoryStream())
            using (var sw = new StreamWriter(ms))
            {
                engine.Runtime.IO.SetOutput(ms, sw);
                engine.Runtime.IO.SetErrorOutput(ms, sw);

                try
                {
                    var sc = engine.CreateScriptSourceFromString(scriptContent);
                    var code = sc.Compile();

                    var scope = engine.CreateScope();
                    scope.SetVariable("model", model);

                    var qf = new QueryFunctions(model.db);
                    scope.SetVariable("q", qf);
                    code.Execute(scope);

                    model.db.SubmitChanges();

                    ms.Position = 0;

                    using (var sr = new StreamReader(ms))
                    {
                        var s = sr.ReadToEnd();
                        return s;
                    }
                }
                catch (Exception ex)
                {
                    var err = engine.GetService<ExceptionOperations>().FormatException(ex);
                    throw new Exception(err);
                }
            }
        }