added pattern replacer

This commit is contained in:
reven
2021-11-26 13:32:30 +13:00
parent fc09f75c05
commit 63d06b75aa
10 changed files with 135 additions and 3 deletions

Binary file not shown.

View File

@@ -82,6 +82,16 @@
"Pattern-Help": "A regular expression, using the C# specifciation for regular expressions."
}
},
"PatternReplacer": {
"Description": "Lets you make replacements in the filename. Can use regular expressions for replacements, or simple string raplacements.\n\nOutput 1: Replacement done\nOutput 2: No replacement done",
"Fields": {
"Replacements": "Replacements",
"ReplacementsKey": "Pattern",
"ReplacementsValue": "Value",
"UseWorkingFileName": "Use Working Filename",
"UseWorkingFileName-Help": "If current working filename should be used, or if false, the original filename of the incoming file will be used."
}
},
"Renamer": {
"Description": "Renames the working file.\nVariables can be used by entering the key '{' inside the Pattern field.",
"Fields": {

View File

@@ -0,0 +1,76 @@
namespace FileFlows.BasicNodes.Functions
{
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
public class PatternReplacer : Node
{
public override int Inputs => 1;
public override int Outputs => 2;
public override FlowElementType Type => FlowElementType.Logic;
public override string Icon => "fas fa-exchange-alt";
internal bool UnitTest = false;
[KeyValue(1)]
[Required]
public List<KeyValuePair<string, string>> Replacements{ get; set; }
[Boolean(2)]
public bool UseWorkingFileName { get; set; }
public override int Execute(NodeParameters args)
{
if (Replacements?.Any() != true)
return 2; // no replacements
string filename = new FileInfo(UseWorkingFileName ? args.WorkingFile : args.FileName).Name;
string updated = filename;
try
{
foreach(var replacement in Replacements)
{
try
{
// this might not be a regex, but try it first
updated = Regex.Replace(updated, replacement.Key, replacement.Value, RegexOptions.IgnoreCase);
}
catch (Exception ex) { }
updated = updated.Replace(replacement.Key, replacement.Value);
}
if (updated == filename)
{
args.Logger?.ILog("No replacements found in file: " + filename);
return 2; // did not match
}
args.Logger?.ILog($"Pattern replacement: '{filename}' to '{updated}'");
string dest = Path.Combine(new FileInfo(args.WorkingFile)?.DirectoryName ?? string.Empty, updated);
args.Logger?.ILog($"New filename: " + dest);
if (UnitTest == false)
{
if (args.MoveFile(dest) == false)
{
args.Logger?.ELog("Failed to move file");
return -1;
}
}
else
{
args.SetWorkingFile(dest);
}
return 1;
}
catch (Exception ex)
{
args.Logger?.ELog("Pattern error: " + ex.Message);
return -1;
}
}
}
}

View File

@@ -0,0 +1,46 @@
#if(DEBUG)
namespace BasicNodes.Tests
{
using FileFlows.BasicNodes.Functions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class PatternReplacerTests
{
[TestMethod]
public void PatternReplacer_Basic()
{
PatternReplacer node = new PatternReplacer();
node.Replacements = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Seinfeld", "Batman")
};
node.UnitTest = true;
var args = new FileFlows.Plugin.NodeParameters(@"c:\test\Seinfeld.mkv");
var result = node.Execute(args);
Assert.AreEqual(1, result);
Assert.AreEqual(@"c:\test\Batman.mkv", args.WorkingFile);
}
[TestMethod]
public void PatternReplacer_Regex()
{
PatternReplacer node = new PatternReplacer();
node.Replacements = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>(@"s([\d]+)e([\d]+)", "$1x$2"),
new KeyValuePair<string, string>(@"0([1-9]+x[\d]+)", "$1"),
};
node.UnitTest = true;
var args = new FileFlows.Plugin.NodeParameters(@"c:\test\Seinfeld S03E06.mkv");
var result = node.Execute(args);
Assert.AreEqual(1, result);
Assert.AreEqual(@"c:\test\Seinfeld 3x06.mkv", args.WorkingFile);
}
}
}
#endif

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,17 +1,17 @@
[
{
"Name": "BasicNodes",
"Version": "0.0.1.15",
"Version": "0.0.1.16",
"Package": "https://github.com/revenz/FileFlowsPlugins/blob/master/Builds/BasicNodes.zip?raw=true"
},
{
"Name": "MetaNodes",
"Version": "0.0.1.15",
"Version": "0.0.1.16",
"Package": "https://github.com/revenz/FileFlowsPlugins/blob/master/Builds/MetaNodes.zip?raw=true"
},
{
"Name": "VideoNodes",
"Version": "0.0.1.15",
"Version": "0.0.1.16",
"Package": "https://github.com/revenz/FileFlowsPlugins/blob/master/Builds/VideoNodes.zip?raw=true"
}
]