From 7e4acd865ad48258c001eaedeee0b672bae37841 Mon Sep 17 00:00:00 2001 From: Fabrice Salvaire Date: Tue, 25 Dec 2018 01:07:58 +0100 Subject: [PATCH] improved Ast --- PythonicGcodeMachine/Gcode/Rs274/Ast.py | 26 ++++++++++++++++----- PythonicGcodeMachine/Gcode/Rs274/Machine.py | 10 ++++++++ examples/gcode/annotate-gcode.py | 3 ++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/PythonicGcodeMachine/Gcode/Rs274/Ast.py b/PythonicGcodeMachine/Gcode/Rs274/Ast.py index 963964c..7d49298 100644 --- a/PythonicGcodeMachine/Gcode/Rs274/Ast.py +++ b/PythonicGcodeMachine/Gcode/Rs274/Ast.py @@ -563,7 +563,12 @@ class Word(LineItem): def ansi_str(self): - if self._letter in 'GM': + if self._machine: + gm = self._machine.GM_LETTERS + else: + gm = 'GM' + + if self._letter in gm: return Line.ANSI_G(str(self)) else: return Line.ANSI_X(self._letter) + Line.ANSI_VALUE(str(self._value)) @@ -577,15 +582,24 @@ class Word(LineItem): ############################################## @property - def is_valid_gcode(self): + def is_gm_gcode(self): self._check_machine() - return str(self) in self._machine.config.gcodes + return self._machine.is_gm_word(self) + + ############################################## + + @property + def is_valid_gcode(self): + if self.is_gm_gcode: + return str(self) in self._machine.config.gcodes + else: + return True ############################################## @property def meaning(self): - if self.is_valid_gcode: + if self.is_gm_gcode: return self._machine.config.gcodes[str(self)].meaning else: return self._machine.config.letters[self.letter].meaning @@ -594,7 +608,7 @@ class Word(LineItem): @property def modal_group(self): - if self.is_valid_gcode: + if self.is_gm_gcode: return self._machine.config.modal_groups[str(self)] else: return None @@ -603,7 +617,7 @@ class Word(LineItem): @property def execution_order(self): - if self.is_valid_gcode: + if self.is_gm_gcode: return self._machine.config.execution_order[str(self)] else: return None diff --git a/PythonicGcodeMachine/Gcode/Rs274/Machine.py b/PythonicGcodeMachine/Gcode/Rs274/Machine.py index 96e6939..1864685 100644 --- a/PythonicGcodeMachine/Gcode/Rs274/Machine.py +++ b/PythonicGcodeMachine/Gcode/Rs274/Machine.py @@ -40,6 +40,8 @@ class GcodeMachine: PARSER_CLS = GcodeParser + GM_LETTERS = 'GM' + ############################################## def __init__(self): @@ -85,3 +87,11 @@ class GcodeMachine: pass ############################################## + + def is_gm_letter(self, letter): + return letter in self.GM_LETTERS + + ############################################## + + def is_gm_word(self, word): + return self.is_gm_letter(word.letter) diff --git a/examples/gcode/annotate-gcode.py b/examples/gcode/annotate-gcode.py index ab393e7..8e933df 100644 --- a/examples/gcode/annotate-gcode.py +++ b/examples/gcode/annotate-gcode.py @@ -69,11 +69,12 @@ for line in program: # print(line.ansi_str()) # Fixme: pyterate print(str(line)) for word in line.iter_on_word(): - if word.is_valid_gcode: + if word.is_gm_gcode: margin = ' '*9 print(meaning_format.format(str(word), word.meaning)) print(margin + 'Modal group: {}'.format(word.modal_group.meaning)) print(margin + 'Execution order: {}'.format(word.execution_order.index)) + print(margin + 'Valid G-code: {}'.format(word.is_valid_gcode)) else: print(meaning_format.format(word.letter, word.meaning)) #o# -- GitLab