From 08703a95930c2f2525cbf369e0c33f3dc203bf20 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Wed, 8 Sep 2021 13:28:48 +0200 Subject: [PATCH] Creato classe x parametrizzare gest TAGS --- MP.FileData/Controllers/FileController.cs | 49 +++++++++++++-- MP.FileData/SearchRules.cs | 73 +++++++++++++++++++++++ MP.FileData/TagsUtils.cs | 45 +++++++++++--- MP.Prog/Conf/.placeholder | 1 + MP.Prog/Conf/Rule01.json | 1 + 5 files changed, 157 insertions(+), 12 deletions(-) create mode 100644 MP.FileData/SearchRules.cs create mode 100644 MP.Prog/Conf/.placeholder create mode 100644 MP.Prog/Conf/Rule01.json diff --git a/MP.FileData/Controllers/FileController.cs b/MP.FileData/Controllers/FileController.cs index 4242dd44..d6a78f6c 100644 --- a/MP.FileData/Controllers/FileController.cs +++ b/MP.FileData/Controllers/FileController.cs @@ -104,10 +104,32 @@ namespace MP.FileData.Controllers } } + // fixme todo !!! fix da conf file + + // fare: lettura conf rule x recupero tag x singola macchina + //$"\\b{fileName}" + @".{0,2}\([\w\d\s.]+\)"; + + Dictionary confReplace = new Dictionary(); + confReplace.Add("(", " "); + confReplace.Add(")", " "); + // hard coded + salvataggio conf x creare json + SearchRules currRule = new SearchRules() + { + Name = "Commento Filename", + Mode = SearchMode.StringOnFile, + MaxChar2Search = 100, + ReplaceCR = true, + RegExPattern = "\\b{{fileName}}" + @".{0,2}\([\w\d\s.]+\)", + RegExRepFileName = true, + ExcludedTags = ".xls .xlsx", + OutReplace = confReplace, + OutExcludeFileName = true + }; + // salvo i NUOVI file if (fileNew != null && fileNew.Count > 0) { - FileInsert(idxMacchina, fileNew, 0); + FileInsert(idxMacchina, fileNew, 0, currRule); Log.Info($"CheckFileArchived S03 | insert {fileNew.Count} files"); } // aggiorno i file modificati @@ -234,8 +256,9 @@ namespace MP.FileData.Controllers /// /// /// - public bool FileInsert(string idxMacchina, List newFiles, int rev) + public bool FileInsert(string idxMacchina, List newFiles, int rev, SearchRules currRule) { + // fare: lettura conf x macchina Log.Info($"FileInsert S01 per macchina {idxMacchina}: {newFiles.Count} files da valutare"); bool answ = false; DateTime adesso = DateTime.Now; @@ -283,7 +306,7 @@ namespace MP.FileData.Controllers { code2search = item.FileStringContent; } - Tags = TagsUtils.searchProgComment(item.Name, code2search, excludeTags); + Tags = TagsUtils.searchProgComment(item.Name, code2search, currRule); foreach (var tag in Tags) { @@ -339,8 +362,26 @@ namespace MP.FileData.Controllers var newFileInfo = new FileInfo(currFile.Path); listUpdate.Add(newFileInfo); + // fixme todo !!! fix da conf file + Dictionary confReplace = new Dictionary(); + confReplace.Add("(", " "); + confReplace.Add(")", " "); + // hard coded + salvataggio conf x creare json + SearchRules currRule = new SearchRules() + { + Name = "Commento Filename", + Mode = SearchMode.StringOnFile, + MaxChar2Search = 100, + ReplaceCR = true, + RegExPattern = "\\b{{fileName}}" + @".{0,2}\([\w\d\s.]+\)", + RegExRepFileName = true, + ExcludedTags = ".xls .xlsx", + OutReplace = confReplace, + OutExcludeFileName = true + }; + // inserisco come REVISIONE - FileInsert(currFile.IdxMacchina, listUpdate, currFile.Rev + 1); + FileInsert(currFile.IdxMacchina, listUpdate, currFile.Rev + 1, currRule); // archivio vecchio file currFile.Active = false; diff --git a/MP.FileData/SearchRules.cs b/MP.FileData/SearchRules.cs new file mode 100644 index 00000000..41b7af27 --- /dev/null +++ b/MP.FileData/SearchRules.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MP.FileData +{ + public enum SearchMode + { + /// + /// Ricerca occorrenze di una RegExp dentro il contenuto del file + /// + StringOnFile, + + /// + /// Cerca da path relativo + nume file + /// + PathAndName + } + + public class SearchRules + { + #region Public Properties + + /// + /// Pattern esclusione Tags (stop-words) + /// + public string ExcludedTags { get; set; } = ""; + + /// + /// Quantità massima di caratteri da analizzare, 0 = tutti + /// + public int MaxChar2Search { get; set; } = 100; + + /// + /// Modalità ricerca + /// + public SearchMode Mode { get; set; } = SearchMode.StringOnFile; + + /// + /// Nome della regola di ricerca + /// + public string Name { get; set; } + + /// + /// Configurazione opzionale x rimozione filename da output finali + /// + public bool OutExcludeFileName { get; set; } = true; + + /// + /// Replace in uscita per "bonificare" il tag + /// + public Dictionary OutReplace { get; set; } = new Dictionary(); + + /// + /// Pattern in formato RegExp + /// + public string RegExPattern { get; set; } = ""; + + /// + /// Configurazione opzionale x sostituzione placeholder {{fileName}} in RegExp con il VERO nome file + /// + public bool RegExRepFileName { get; set; } = true; + + /// + /// Configurazione opzionale x sostituzione carriage return --> spazi + /// + public bool ReplaceCR { get; set; } = true; + + #endregion Public Properties + } +} \ No newline at end of file diff --git a/MP.FileData/TagsUtils.cs b/MP.FileData/TagsUtils.cs index 07f31807..54147e2e 100644 --- a/MP.FileData/TagsUtils.cs +++ b/MP.FileData/TagsUtils.cs @@ -31,16 +31,27 @@ namespace MP.FileData /// Cerca tag di commento inseriti nel formato /// NOME_PROG(commento) /// - /// es: O00123(172L D20.5) --> estrae 172L D20.5 + /// es: O00123(172L D20.5) --> estrae 172L, D20.5 /// - /// - /// + /// Nome del file (da cercare per contenuto) + /// Contenuto del file da analizzare + /// Parametri analisi /// - public static List searchProgComment(string fileName, string fileContent, List excludeTags) + public static List searchProgComment(string fileName, string fileContent, SearchRules currRule) { + // verifico se trimmare contenuto file + if (fileContent.Length > currRule.MaxChar2Search && currRule.MaxChar2Search > 0) + { + fileContent.Substring(0, currRule.MaxChar2Search); + } // bonifico: a capo --> spazi fileContent = fileContent.Replace(Environment.NewLine, " "); - string pattern = $"\\b{fileName}" + @".{0,2}\([\w\d\s.]+\)"; + //string pattern = $"\\b{fileName}" + @".{0,2}\([\w\d\s.]+\)"; + string pattern = currRule.RegExPattern; + if (currRule.RegExRepFileName) + { + pattern = pattern.Replace("{{fileName}}", fileName); + } List answ = new List(); if (fileContent.Length > 0) { @@ -50,16 +61,34 @@ namespace MP.FileData { for (int count = 0; count < matchTags.Count; count++) { + string currMatch = matchTags[count].Value; + // bonifica preliminare (se richiesta) + if (currRule.OutExcludeFileName) + { + currMatch.Replace(fileName, ""); + } + if (currRule.OutReplace.Count > 0) + { + foreach (var item in currRule.OutReplace) + { + currMatch = currMatch.Replace(item.Key, item.Value); + } + } +#if false // pulizia: elimino nome programma e parentesi string currMatch = matchTags[count].Value .Replace(fileName, "") .Replace("(", " ") .Replace(")", " ").Trim(); +#endif + // trim finale + currMatch = currMatch.Trim(); + // split con spazi var splitTags = currMatch.Split(" "); foreach (var item in splitTags) { - if (!excludeTags.Contains(item) && !string.IsNullOrEmpty(item)) + if (!currRule.ExcludedTags.Contains(item) && !string.IsNullOrEmpty(item)) { // esclusione valori ignorati answ.Add(item); @@ -69,13 +98,13 @@ namespace MP.FileData } else { - Log.Warn($"searchProgComment Attenzione Match non trovato | {fileName} | pattern: {pattern}"); + Log.Warn($"searchProgComment Attenzione Match non trovato | {fileName} | pattern: {currRule.RegExPattern}"); } } // se nullo --> segnalo! if (answ.Count == 0) { - Log.Warn($"searchProgComment Attenzione Tag non trovati | {fileName} | pattern: {pattern}{Environment.NewLine}{fileContent}"); + Log.Warn($"searchProgComment Attenzione Tag non trovati | {fileName} | pattern: {currRule.RegExPattern}{Environment.NewLine}{fileContent}"); } return answ; } diff --git a/MP.Prog/Conf/.placeholder b/MP.Prog/Conf/.placeholder new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/MP.Prog/Conf/.placeholder @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/MP.Prog/Conf/Rule01.json b/MP.Prog/Conf/Rule01.json new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/MP.Prog/Conf/Rule01.json @@ -0,0 +1 @@ + \ No newline at end of file