Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PatternRemover Enhancement #1158 and Second Typo #1645 #1676

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ dependency-reduced-pom.xml
/ugs-platform/ProbeModule/nbproject/
ugs-pendant/src/main/webapp/node/node_modules
ugs-pendant/src/main/webapp/node
/ugs-platform/ugs-platform-plugin-cloud-storage/nbproject/
/ugs-platform/ugs-platform-plugin-cloud-storage/nbproject/
all-checksum.txt
/lastModified/all-checksum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,77 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.universalgcodesender.i18n.Localization;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import java.util.regex.Matcher;
//
import com.willwinder.universalgcodesender.types.Macro;
import com.willwinder.universalgcodesender.utils.SettingsFactory;
//
/**
*
* @author wwinder
* @author wwinder, AndyCXL
*
*/
public class PatternRemover implements CommandProcessor {
final private Pattern p;

final private List<String> r = new ArrayList<>();
private static final Logger logger = Logger.getLogger(PatternRemover.class.getName());
//
public PatternRemover(String regexPattern) {
p = Pattern.compile(regexPattern);
// Check if 'remover or replacer' by detecting sed syntax
String[] s3 = regexPattern.split("/", 3);
switch(s3.length) {
case 2:
// Replacer s/regex match to p, blank r
p = Pattern.compile(s3[1].trim());
r.add("");
break;
case 3:
// Full s/regex/replace match, r as-is or with macro expansion
Pattern pm = Pattern.compile("%.+%");
Matcher mp = pm.matcher(s3[2].trim());
if (mp.matches()) {
// Get the user's macros for match searching
List<Macro> macros = SettingsFactory.loadSettings().getMacros();
for (Macro macro: macros) {
if (mp.group().equals("%"+macro.getName()+"%")) {
s3[2] = s3[2].replace(mp.group(), macro.getGcode());
break;
}
}
}
// s3[2] contains macro expansion or the orig string if no matches
p = Pattern.compile(s3[1].trim());
if (s3[2].contains("%")) {
r.add("");
} else {
r.add(s3[2].trim());
}
break;
default:
// No sed, or mal-formed, so Simple regex to p, blank r
p = Pattern.compile(regexPattern);
r.add("");
break;
}
}

@Override
public String getHelp() {
return Localization.getString("sender.help.patternRemover")
+ ": \"" + p.pattern() + "\"";
+ ": \"" + p.pattern() + "/" + r.get(0) + "\"";
}

@Override
public List<String> processCommand(String command, GcodeState state) {
List<String> ret = new ArrayList<>();
ret.add(p.matcher(command).replaceAll(""));
// Property p contains the grep string in either case of grep or sed
ret.add( p.matcher(command).replaceAll( r.get(0) ) );
if (!command.equals(ret.get(0))) {
logger.log(Level.INFO, "Replacer: \""+command+"\" to: \""+ret.get(0)+"\"");
}
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void divideZStepSize() {
}

public void divideABCStepSize() {
setStepSizeABC(divideSize(getStepSizeZ()));
setStepSizeABC(divideSize(getStepSizeABC()));
}

public void multiplyXYStepSize() {
Expand All @@ -146,7 +146,7 @@ public void multiplyZStepSize() {
}

public void multiplyABCStepSize() {
setStepSizeABC(multiplySize(getStepSizeZ()));
setStepSizeABC(multiplySize(getStepSizeABC()));
}

public void multiplyFeedRate() {
Expand Down
4 changes: 2 additions & 2 deletions ugs-core/src/resources/MessagesBundle_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ macro.substitution = Substitutions
parser.processor.arc.start-error = There is no starting point, unable to expand arc.
parser.processor.arc.multiple-commands = Multiple commands were found on one line while expanding an arc, unable to continue. The command splitter processor can be used to continue.
sender.help.commandSplitter = Split gcode command into multiple parts for better error reporting.
sender.help.patternRemover = Removes text matching a regular expression pattern
sender.help.patternRemover = Removes or replaces text matching a regular expression or sed "s/regex/replace" pattern
controller.exception.ignoreFutureErrors = Would you like to skip commands like this in the future?
settings.processors.header = Gcode Processor Configuration
settings.file.outOfDate.title = Out of date configuration
Expand All @@ -333,7 +333,7 @@ settings.file.generalError = An error has occurred while initializing firmware c
settings.processors.enabled = Enabled
settings.processors.add = Add
settings.processors.remove = Remove Selected
settings.processors.help = These filters are used to process gcode prior to sending it to the controller. Some of them must be enabled, others may be disabled. Most are configured by global settings in the sender settings menu. Custom filters can be added to remove specific patterns, for example "Td+" will remove any instance of "T1", "T12", etc.
settings.processors.help = These filters process gcode prior to sending to the controller. Some must be enabled, others may be disabled. Most are configured by global settings in the sender settings menu. Custom filters can be added to remove specific patterns, for example "Td+" will remove any instance of "T1", "T12", etc. Using sed, for example "s/Td+/T9" will Replace "T1" or "T2" with T9
settings.processors.loadError = Unable to load processor
settings.language = Language
settings.connectionDriver = Connection driver
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright (C) 2021 Will Winder, AndyCXL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.universalgcodesender.gcode.processors;

import com.willwinder.universalgcodesender.gcode.GcodeState;
import com.willwinder.universalgcodesender.model.Position;
import static com.willwinder.universalgcodesender.model.UnitUtils.Units.MM;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

/**
*
* @author AndyCXL
*/
public class PatternRemoverTest {
@Rule
public ExpectedException expectedEx = ExpectedException.none();

private static void removerHarness(
String command, String regex, Position start, List<String> expected) throws Exception {

PatternRemover instance = new PatternRemover(regex);

GcodeState state = new GcodeState();
state.currentPoint = start;
state.inAbsoluteMode = true;

List<String> result = instance.processCommand(command, state);
assertEquals(expected, result);
}

/**
* Commands without regex match should not be modified.
*/
@Test
public void testIgnoreNoRegexLines() throws Exception {
System.out.println("ignoreNoRegexLines");

PatternRemover instance = new PatternRemover("^[mM]6\\s*[tT]([0-9]+)$");

String command;

GcodeState state = new GcodeState();
state.currentPoint = new Position(0, 0, 0, MM);
state.inAbsoluteMode = true;

command = "G90NOR";
List<String> result = instance.processCommand(command, state);
System.out.println(">>"+command+" to \""+result.get(0)+"\"");
assertThat(result).containsExactly(command);

command = "G1X1NOR";
result = instance.processCommand(command, state);
System.out.println(">>"+command+" to \""+result.get(0)+"\"");
assertThat(result).containsExactly(command);
}

/**
* Commands without sed match should not be modified.
*/
@Test
public void testIgnoreNoSedLines() throws Exception {
System.out.println("ignoreNoSedLines");

PatternRemover instance = new PatternRemover("s/^[mM]6\\s*[tT]([0-9]+)$/M123");

String command;

GcodeState state = new GcodeState();
state.currentPoint = new Position(0, 0, 0, MM);
state.inAbsoluteMode = true;

command = "G90NOS";
List<String> result = instance.processCommand(command, state);
System.out.println(">>"+command+" to \""+result.get(0)+"\"");
assertThat(result).containsExactly(command);

command = "G1X1NOS";
result = instance.processCommand(command, state);
System.out.println(">>"+command+" to \""+result.get(0)+"\"");
assertThat(result).containsExactly(command);
}

/**
* Commands with regex match should be modified.
*/
@Test
public void testMatchRegexpLines() throws Exception {
System.out.println("matchRegexLines");

PatternRemover instance = new PatternRemover("^[mM]6\\s*[tT]([0-9]+)$");

String command;

GcodeState state = new GcodeState();
state.currentPoint = new Position(0, 0, 0, MM);
state.inAbsoluteMode = true;

command = "M6 T12";
List<String> result = instance.processCommand(command, state);
System.out.println(">>"+command+" to \""+result.get(0)+"\"");
assertThat(result).containsExactly("");

command = "M6T12";
result = instance.processCommand(command, state);
System.out.println(">>"+command+" to \""+result.get(0)+"\"");
assertThat(result).containsExactly("");
}

/**
* Commands with sed match should be modified.
*/
@Test
public void testMatchSedLines() throws Exception {
System.out.println("matchSedLines");

PatternRemover instance = new PatternRemover("s/M6T[0-9]+/M123SED");

String command;

GcodeState state = new GcodeState();
state.currentPoint = new Position(0, 0, 0, MM);
state.inAbsoluteMode = true;

command = "M6T12";
List<String> result = instance.processCommand(command, state);
System.out.println(">>"+command+" to \""+result.get(0)+"\"");
assertThat(result).containsExactly("M123SED");
}

/**
* Commands with sed match should be modified
*/
@Test
public void testMatchSedStartLines() throws Exception {
System.out.println("matchSedStartLines");

PatternRemover instance = new PatternRemover("s/^[mM]6\\s*[tT]([0-9]+)");

String command;

GcodeState state = new GcodeState();
state.currentPoint = new Position(0, 0, 0, MM);
state.inAbsoluteMode = true;

command = "M6T12S1000";
List<String> result = instance.processCommand(command, state);
System.out.println(">>"+command+" to \""+result.get(0)+"\"");
assertThat(result).containsExactly("S1000");
}

/**
* Commands with sed match should be modified.
*/
@Test
public void testMatchSedMacroLines() throws Exception {
System.out.println("matchSedMacroLines");

// Vanilla install contains 1 macro named "Macro #1" defined as "G91 X0 Y0;"
PatternRemover instance = new PatternRemover("s/M6\\s*T([0-9]+)/%Macro #1%");

String command;

GcodeState state = new GcodeState();
state.currentPoint = new Position(0, 0, 0, MM);
state.inAbsoluteMode = true;

command = "M6 T113";
List<String> result = instance.processCommand(command, state);
System.out.println(">>"+command+" to \""+result.get(0)+"\"");
assertThat(result).containsExactly("G91 X0 Y0;");
}
}
Loading