From 26cd7a8e5701d27d5b3efdfe603275081cc0bfd6 Mon Sep 17 00:00:00 2001 From: Peter Boin Date: Thu, 3 Aug 2017 18:25:25 +1000 Subject: [PATCH] line macros (%%) --- scripts/.gitignore | 4 ++++ scripts/pygcode-norm | 8 +++++--- src/pygcode/__init__.py | 2 +- src/pygcode/line.py | 15 +++++++++++++-- src/pygcode/machine.py | 8 ++++++-- tests/test_line.py | 26 ++++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 scripts/.gitignore diff --git a/scripts/.gitignore b/scripts/.gitignore new file mode 100644 index 0000000..b137628 --- /dev/null +++ b/scripts/.gitignore @@ -0,0 +1,4 @@ +# GCode files (dropped into this folder during development) +*.gcode +*.ngc +*.g diff --git a/scripts/pygcode-norm b/scripts/pygcode-norm index d9bd32f..c371547 100755 --- a/scripts/pygcode-norm +++ b/scripts/pygcode-norm @@ -203,7 +203,7 @@ omit_redundant_modes = utils.omit_redundant_modes if args.full: omit_redundant_modes = lambda gcode_iter: gcode_iter # bypass -def write(gcodes, modal_params=tuple(), comment=None): +def write(gcodes, modal_params=tuple(), comment=None, macro=None): """ Write to output, while enforcing the flags: args.singles @@ -234,6 +234,8 @@ def write(gcodes, modal_params=tuple(), comment=None): line_list.append(block_str) if comment: line_list.append(str(comment)) + if macro: + line_list.append(str(macro)) line_str = ' '.join(line_list) if line_str or not args.rm_blanks: print(line_str) @@ -309,7 +311,7 @@ for line_str in args.infile.readlines(): else: if args.full: - write(effective_gcodes, comment=line.comment) + write(effective_gcodes, comment=line.comment, macro=line.macro) else: - write(line.block.gcodes, modal_params=line.block.modal_params, comment=line.comment) + write(line.block.gcodes, modal_params=line.block.modal_params, comment=line.comment, macro=line.macro) machine.process_block(line.block) diff --git a/src/pygcode/__init__.py b/src/pygcode/__init__.py index afb4bd2..667c24d 100644 --- a/src/pygcode/__init__.py +++ b/src/pygcode/__init__.py @@ -6,7 +6,7 @@ # 1.x - Development Status :: 5 - Production/Stable # .y - developments on that version (pre-release) # *.dev* - development release (intended purely to test deployment) -__version__ = "0.1.1" +__version__ = "0.1.2" __title__ = "pygcode" __description__ = "Basic g-code parser, interpreter, and encoder library." diff --git a/src/pygcode/line.py b/src/pygcode/line.py index 881b5f1..b4f4aef 100644 --- a/src/pygcode/line.py +++ b/src/pygcode/line.py @@ -1,17 +1,28 @@ +import re + from .comment import split_line from .block import Block class Line(object): + + line_regex = re.compile(r'^(?P.*?)?(?P%.*%?)?\s*$') + def __init__(self, text=None): self._text = text # Initialize self.block = None self.comment = None + self.macro = None # Split line into block text, and comments if text is not None: - (block_str, comment) = split_line(text) + match = self.line_regex.search(text) + + block_and_comment = match.group('block_and_comment') + self.macro = match.group('macro') + + (block_str, comment) = split_line(block_and_comment) self.block = Block(block_str) if comment: self.comment = comment @@ -28,4 +39,4 @@ class Line(object): return self.block.gcodes def __str__(self): - return ' '.join([str(x) for x in [self.block, self.comment] if x]) + return ' '.join([str(x) for x in [self.block, self.comment, self.macro] if x]) diff --git a/src/pygcode/machine.py b/src/pygcode/machine.py index ac19ace..4cc2bed 100644 --- a/src/pygcode/machine.py +++ b/src/pygcode/machine.py @@ -1,3 +1,4 @@ +import re from copy import copy, deepcopy from collections import defaultdict @@ -290,9 +291,12 @@ class Mode(object): def __init__(self, set_default=True): self.modal_groups = defaultdict(lambda: None) - # Initialize + # Initialize (from multiline self.default_mode) if set_default: - self.set_mode(*Line(self.default_mode).block.gcodes) + gcodes = [] + for m in re.finditer(r'\s*(?P.*)\s*\n?', self.default_mode): + gcodes += Line(m.group('line')).block.gcodes + self.set_mode(*gcodes) def __copy__(self): obj = self.__class__(set_default=False) diff --git a/tests/test_line.py b/tests/test_line.py index 24337ca..1e938da 100644 --- a/tests/test_line.py +++ b/tests/test_line.py @@ -27,3 +27,29 @@ class LineCommentTests(unittest.TestCase): line = Line('G02 X10.75 (x coord) Y47.44 (y coord) I-0.11 J-1.26 F70 (eol)') self.assertEqual(line.comment.text, 'x coord. y coord. eol') self.assertEqual(len(line.block.words), 6) + + def test_line_macros(self): + # (blank) + line = Line('') + self.assertIsNone(line.macro) + + # (no macro) + line = Line('G02 X10.75 Y47.44 I-0.11 J-1.26 F70 (blah blah)') + self.assertIsNone(line.macro) + + # % + line = Line('%') + self.assertEqual(str(line.macro), '%') + + # %% + line = Line('%%') + self.assertEqual(str(line.macro), '%%') + + # % blah blah % + line = Line('% blah blah %') + self.assertEqual(str(line.macro), '% blah blah %') + + # Combined at end of line (not sure if this is legit) + line = Line('G02 X10.75 Y2 ; abc %something%') + self.assertEqual(line.comment.text.strip(), 'abc') + self.assertEqual(line.macro, '%something%')