line macros (%%)

This commit is contained in:
Peter Boin 2017-08-03 18:25:25 +10:00
parent 4a2f7adbdf
commit 26cd7a8e57
6 changed files with 55 additions and 8 deletions

4
scripts/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
# GCode files (dropped into this folder during development)
*.gcode
*.ngc
*.g

View File

@ -203,7 +203,7 @@ omit_redundant_modes = utils.omit_redundant_modes
if args.full: if args.full:
omit_redundant_modes = lambda gcode_iter: gcode_iter # bypass 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: Write to output, while enforcing the flags:
args.singles args.singles
@ -234,6 +234,8 @@ def write(gcodes, modal_params=tuple(), comment=None):
line_list.append(block_str) line_list.append(block_str)
if comment: if comment:
line_list.append(str(comment)) line_list.append(str(comment))
if macro:
line_list.append(str(macro))
line_str = ' '.join(line_list) line_str = ' '.join(line_list)
if line_str or not args.rm_blanks: if line_str or not args.rm_blanks:
print(line_str) print(line_str)
@ -309,7 +311,7 @@ for line_str in args.infile.readlines():
else: else:
if args.full: if args.full:
write(effective_gcodes, comment=line.comment) write(effective_gcodes, comment=line.comment, macro=line.macro)
else: 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) machine.process_block(line.block)

View File

@ -6,7 +6,7 @@
# 1.x - Development Status :: 5 - Production/Stable # 1.x - Development Status :: 5 - Production/Stable
# <any above>.y - developments on that version (pre-release) # <any above>.y - developments on that version (pre-release)
# <any above>*.dev* - development release (intended purely to test deployment) # <any above>*.dev* - development release (intended purely to test deployment)
__version__ = "0.1.1" __version__ = "0.1.2"
__title__ = "pygcode" __title__ = "pygcode"
__description__ = "Basic g-code parser, interpreter, and encoder library." __description__ = "Basic g-code parser, interpreter, and encoder library."

View File

@ -1,17 +1,28 @@
import re
from .comment import split_line from .comment import split_line
from .block import Block from .block import Block
class Line(object): class Line(object):
line_regex = re.compile(r'^(?P<block_and_comment>.*?)?(?P<macro>%.*%?)?\s*$')
def __init__(self, text=None): def __init__(self, text=None):
self._text = text self._text = text
# Initialize # Initialize
self.block = None self.block = None
self.comment = None self.comment = None
self.macro = None
# Split line into block text, and comments # Split line into block text, and comments
if text is not None: 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) self.block = Block(block_str)
if comment: if comment:
self.comment = comment self.comment = comment
@ -28,4 +39,4 @@ class Line(object):
return self.block.gcodes return self.block.gcodes
def __str__(self): 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])

View File

@ -1,3 +1,4 @@
import re
from copy import copy, deepcopy from copy import copy, deepcopy
from collections import defaultdict from collections import defaultdict
@ -290,9 +291,12 @@ class Mode(object):
def __init__(self, set_default=True): def __init__(self, set_default=True):
self.modal_groups = defaultdict(lambda: None) self.modal_groups = defaultdict(lambda: None)
# Initialize # Initialize (from multiline self.default_mode)
if set_default: if set_default:
self.set_mode(*Line(self.default_mode).block.gcodes) gcodes = []
for m in re.finditer(r'\s*(?P<line>.*)\s*\n?', self.default_mode):
gcodes += Line(m.group('line')).block.gcodes
self.set_mode(*gcodes)
def __copy__(self): def __copy__(self):
obj = self.__class__(set_default=False) obj = self.__class__(set_default=False)

View File

@ -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)') 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(line.comment.text, 'x coord. y coord. eol')
self.assertEqual(len(line.block.words), 6) 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%')