<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Controls.WpfPropertyGrid.Attributes;
using System.Xml.Serialization;
using Hawk.Core.Utils;
using Hawk.Core.Utils.Logs;
using Hawk.Core.Utils.Plugins;
using IronPython.Hosting;

namespace Hawk.ETL.Managements
{
    public class ProcessTask : TaskBase, IDictionarySerializable
    {
        #region Constructors and Destructors

        public ProcessTask()
        {
            ProcessToDo = new FreeDocument();
        }

        #endregion

        #region Properties

        /// <summary>
        ///     要实现的算法和对应的配置
        /// </summary>
        [XmlIgnore]
        [Browsable(false)]
        public FreeDocument ProcessToDo { get; set; }

        #endregion

        public virtual FreeDocument DictSerialize(Scenario scenario = Scenario.Database)
        {
            var docu = ProcessToDo.DictSerialize(scenario);
            docu.SetValue("CreateTime", CreateTime);
            docu.SetValue("Name", Name);
            docu.SetValue("Description", Description);
            docu.SetValue("ScriptPath", ScriptPath);
            return docu;
        }

        public virtual void DictDeserialize(IDictionary<string, object> docu, Scenario scenario = Scenario.Database)
        {
            ProcessToDo.DictDeserialize(docu, scenario);
            Name = docu.Set("Name", Name);
            Description = docu.Set("Description", Description);
            CreateTime = docu.Set("CreateTime", CreateTime);
            ScriptPath = docu.Set("ScriptPath", ScriptPath);
        }

        #region Constants and Fields

        #endregion

        #region Public Methods

        public Project Project { get; set; }

        public void EvalScript( )
        {
            var script = ScriptPath;
            var path = Project.SavePath;
            var folder = new DirectoryInfo(path).Parent?.FullName;
            if (folder != null)
                script = folder +"\\"+ script;

            if (!File.Exists(script))
                return;

            var engine = Python.CreateEngine();
            var scope = engine.CreateScope();

            try
            {
                var source = engine.CreateScriptSourceFromFile(script);
                var compiledCode = source.Compile();
                foreach (var process in ProcessManager.CurrentProcessCollections)
                {
                    scope.SetVariable(process.Name, process);
                }
                dynamic d;

                d = compiledCode.Execute(scope);
            }
            catch (Exception ex)
            {
                XLogSys.Print.Error(ex);
            }
        }

        public virtual void Load()
        {
            if (
                (ProcessManager.CurrentProcessCollections.FirstOrDefault(d => d.Name == this.Name) == null).SafeCheck("不能重复加载该任务") ==
                false)
                return;
            ControlExtended.SafeInvoke(() =>
            {
                var processname = ProcessToDo["Type"].ToString();
                if (string.IsNullOrEmpty(processname))
                    return;
                var process = ProcessManager.GetOneInstance(processname, newOne: true);
                ProcessToDo.DictCopyTo(process as IDictionarySerializable);
                process.Init();
                EvalScript();
            }, LogType.Important, $"加载{Name}任务", true);
        }

        [DisplayName("脚本路径")]
        [PropertyOrder(6)]
        public string ScriptPath { get; set; }

        #endregion
    }
}<commit_msg>今天周日加班<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Controls.WpfPropertyGrid.Attributes;
using System.Xml.Serialization;
using Hawk.Core.Utils;
using Hawk.Core.Utils.Logs;
using Hawk.Core.Utils.Plugins;
using IronPython.Hosting;
using Microsoft.Scripting;

namespace Hawk.ETL.Managements
{
    public class ProcessTask : TaskBase, IDictionarySerializable
    {
        #region Constructors and Destructors

        public ProcessTask()
        {
            ProcessToDo = new FreeDocument();
        }

        #endregion

        #region Properties

        /// <summary>
        ///     要实现的算法和对应的配置
        /// </summary>
        [XmlIgnore]
        [Browsable(false)]
        public FreeDocument ProcessToDo { get; set; }

        #endregion

        public virtual FreeDocument DictSerialize(Scenario scenario = Scenario.Database)
        {
            var docu = ProcessToDo.DictSerialize(scenario);
            docu.SetValue("CreateTime", CreateTime);
            docu.SetValue("Name", Name);
            docu.SetValue("Description", Description);
            docu.SetValue("ScriptPath", ScriptPath);
            return docu;
        }

        public virtual void DictDeserialize(IDictionary<string, object> docu, Scenario scenario = Scenario.Database)
        {
            ProcessToDo.DictDeserialize(docu, scenario);
            Name = docu.Set("Name", Name);
            Description = docu.Set("Description", Description);
            CreateTime = docu.Set("CreateTime", CreateTime);
            ScriptPath = docu.Set("ScriptPath", ScriptPath);
        }

        #region Constants and Fields

        #endregion

        #region Public Methods

        public Project Project { get; set; }

        public void EvalScript( )
        {
            var script = ScriptPath;
            var path = Project.SavePath;
            var folder = new DirectoryInfo(path).Parent?.FullName;
            if (folder != null)
                script = folder +"\\"+ script;

            if (!File.Exists(script))
                return;

            var engine = Python.CreateEngine();
            var scope = engine.CreateScope();

            try
            {
                var source = engine.CreateScriptSourceFromFile(script);
                var compiledCode = source.Compile();
                foreach (var process in ProcessManager.CurrentProcessCollections)
                {
                    scope.SetVariable(process.Name, process);
                }
                dynamic d;

                d = compiledCode.Execute(scope);
            }
            catch (Exception ex)
            {
                var syntax = ex as SyntaxErrorException;
                if (syntax != null)
                {
                    XLogSys.Print.ErrorFormat("编译错误：{0}，位置在{1}行,从{2}到{3}",ex.Message,syntax.Line, syntax.RawSpan.Start,syntax.RawSpan.End);
                    return;
                }
                XLogSys.Print.Error(ex);
            }
        }

        public virtual void Load()
        {
            if (
                (ProcessManager.CurrentProcessCollections.FirstOrDefault(d => d.Name == this.Name) == null).SafeCheck("不能重复加载该任务") ==
                false)
                return;
            ControlExtended.SafeInvoke(() =>
            {
                var processname = ProcessToDo["Type"].ToString();
                if (string.IsNullOrEmpty(processname))
                    return;
                var process = ProcessManager.GetOneInstance(processname, newOne: true);
                ProcessToDo.DictCopyTo(process as IDictionarySerializable);
                process.Init();
                EvalScript();
            }, LogType.Important, $"加载{Name}任务", true);
        }

        [DisplayName("脚本路径")]
        [PropertyOrder(6)]
        public string ScriptPath { get; set; }

        #endregion
    }
}
{'fraction_non_alphanumeric': 0.09478127398311588, 'fraction_numerical': 0.0007674597083653108, 'mean_word_length': 1.5942269409422694, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2472867', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1951, 'n_words': 405}