Commit 35b4d3be authored by Fabrice Salvaire's avatar Fabrice Salvaire

Improved confi

parent f223aead
...@@ -612,10 +612,23 @@ class Word(LineItem): ...@@ -612,10 +612,23 @@ class Word(LineItem):
############################################## ##############################################
@property
def _gcode_info(self):
return self._machine.config.gcodes[str(self)]
##############################################
@property
def gcode_info(self):
if self.is_gm_gcode:
return self._gcode_info
##############################################
@property @property
def meaning(self): def meaning(self):
if self.is_gm_gcode: if self.is_gm_gcode:
return self._machine.config.gcodes[str(self)].meaning return self._gcode_info.meaning
else: else:
return self._machine.config.letters[self.letter].meaning return self._machine.config.letters[self.letter].meaning
...@@ -624,7 +637,7 @@ class Word(LineItem): ...@@ -624,7 +637,7 @@ class Word(LineItem):
@property @property
def modal_group(self): def modal_group(self):
if self.is_gm_gcode: if self.is_gm_gcode:
return self._machine.config.modal_groups[str(self)] return self._gcode_info.modal_group
else: else:
return None return None
...@@ -633,7 +646,7 @@ class Word(LineItem): ...@@ -633,7 +646,7 @@ class Word(LineItem):
@property @property
def execution_order(self): def execution_order(self):
if self.is_gm_gcode: if self.is_gm_gcode:
return self._machine.config.execution_order[str(self)] return self._gcode_info.execution_order
else: else:
return None return None
......
...@@ -35,12 +35,12 @@ __all__ = [ ...@@ -35,12 +35,12 @@ __all__ = [
'ExecutionGroup', 'ExecutionGroup',
'ExecutionOrder', 'ExecutionOrder',
'Gcode', 'Gcode',
'Gcodes', 'GcodeSet',
'Letters', 'Letters',
'ModalGroup', 'ModalGroup',
'ModalGroups', 'ModalGroups',
'Parameter', 'Parameter',
'Parameters', 'ParameterSet',
] ]
#################################################################################################### ####################################################################################################
...@@ -49,6 +49,13 @@ import yaml ...@@ -49,6 +49,13 @@ import yaml
#################################################################################################### ####################################################################################################
def ensure_list(obj):
if not isinstance(obj, list):
obj = [obj]
return obj
####################################################################################################
class YamlMixin: class YamlMixin:
def _load_yaml(self, yaml_path): def _load_yaml(self, yaml_path):
with open(yaml_path, 'r') as fh: with open(yaml_path, 'r') as fh:
...@@ -151,7 +158,7 @@ class Parameter(MeaningMixin): ...@@ -151,7 +158,7 @@ class Parameter(MeaningMixin):
#################################################################################################### ####################################################################################################
class Parameters(YamlMixin, RstMixin): class ParameterSet(YamlMixin, RstMixin):
"""Class for the table of parameters.""" """Class for the table of parameters."""
...@@ -244,11 +251,15 @@ class Gcode(MeaningMixin): ...@@ -244,11 +251,15 @@ class Gcode(MeaningMixin):
############################################## ##############################################
def __init__(self, gcode, meaning): def __init__(self, gcode, meaning, modal_group=None, execution_order=None):
MeaningMixin.__init__(self, meaning) MeaningMixin.__init__(self, meaning)
self._gcode = str(gcode) self._gcode = str(gcode)
# Those are set later due to the initialisation process
self._modal_group = modal_group
self._execution_order = execution_order
############################################## ##############################################
@property @property
...@@ -256,9 +267,21 @@ class Gcode(MeaningMixin): ...@@ -256,9 +267,21 @@ class Gcode(MeaningMixin):
"""G-code (table key)""" """G-code (table key)"""
return self._gcode return self._gcode
@property
def modal_group(self):
return self._modal_group
@property
def execution_order(self):
return self._execution_order
@property
def execution_order_index(self):
return self._execution_order.index
#################################################################################################### ####################################################################################################
class Gcodes(YamlMixin, RstMixin): class GcodeSet(YamlMixin, RstMixin):
"""Class for the table of G-codes.""" """Class for the table of G-codes."""
...@@ -309,11 +332,12 @@ class ExecutionGroup(MeaningMixin): ...@@ -309,11 +332,12 @@ class ExecutionGroup(MeaningMixin):
############################################## ##############################################
def __init__(self, index, gcodes, meaning): def __init__(self, index, gcodes, raw_gcodes, meaning):
MeaningMixin.__init__(self, meaning) MeaningMixin.__init__(self, meaning)
self._index = int(index) self._index = int(index)
self._gcodes = list(gcodes) self._gcodes = list(gcodes)
self._raw_gcodes = list(raw_gcodes)
############################################## ##############################################
...@@ -327,6 +351,11 @@ class ExecutionGroup(MeaningMixin): ...@@ -327,6 +351,11 @@ class ExecutionGroup(MeaningMixin):
"""G-Codes list""" """G-Codes list"""
return self._gcodes return self._gcodes
@property
def raw_gcodes(self):
"""Raw G-Codes list"""
return self._raw_gcodes
#################################################################################################### ####################################################################################################
class ExecutionOrder(YamlMixin, RstMixin): class ExecutionOrder(YamlMixin, RstMixin):
...@@ -335,29 +364,41 @@ class ExecutionOrder(YamlMixin, RstMixin): ...@@ -335,29 +364,41 @@ class ExecutionOrder(YamlMixin, RstMixin):
############################################## ##############################################
def __init__(self, yaml_path): def __init__(self, yaml_path, gcode_set):
data = self._load_yaml(yaml_path) data = self._load_yaml(yaml_path)
self._order = [] self._order = []
self._gcode_map = {}
count = 1 count = 1
for index, d in data.items(): for index, d in data.items():
if index != count: if index != count:
raise ValueError('Unexpected index {} versus {}'.format(index, count)) raise ValueError('Unexpected index {} versus {}'.format(index, count))
count += 1 count += 1
gcodes = d['gcodes']
if not isinstance(gcodes, list): raw_gcodes = ensure_list(d['gcodes'])
gcodes = [gcodes]
group = ExecutionGroup(index, gcodes, d['meaning']) gcodes = []
self._order.append(group) for gcode in raw_gcodes:
for gcode in gcodes:
if '-' in gcode: if '-' in gcode:
start, stop = [int(code[1:]) for code in gcode.split('-')] start, stop = [int(code[1:]) for code in gcode.split('-')]
letter = gcode[0] letter = gcode[0]
for i in range(start, stop+1): for i in range(start, stop+1):
self._gcode_map['{}{}'.format(letter, i)] = group _gcode = '{}{}'.format(letter, i)
gcodes.append(gcode_set[_gcode])
else: else:
self._gcode_map[gcode] = group try:
gcode = gcode_set[gcode]
except KeyError:
if gcode != 'COMMENT':
raise ValueError('Invalid G-code {}'.format(gcode))
gcodes.append(gcode)
group = ExecutionGroup(index, gcodes, raw_gcodes, d['meaning'])
self._order.append(group)
for gcode in gcodes:
if isinstance(gcode, Gcode):
gcode._execution_order = group
############################################## ##############################################
...@@ -368,10 +409,7 @@ class ExecutionOrder(YamlMixin, RstMixin): ...@@ -368,10 +409,7 @@ class ExecutionOrder(YamlMixin, RstMixin):
return iter(self._order) return iter(self._order)
def __getitem__(self, index): def __getitem__(self, index):
if isinstance(index, int): return self._order[index]
return self._order[index]
else:
return self._gcode_map[index]
############################################## ##############################################
...@@ -415,19 +453,18 @@ class ModalGroups(YamlMixin, RstMixin): ...@@ -415,19 +453,18 @@ class ModalGroups(YamlMixin, RstMixin):
############################################## ##############################################
def __init__(self, yaml_path): def __init__(self, yaml_path, gcode_set):
data = self._load_yaml(yaml_path) data = self._load_yaml(yaml_path)
self._groups = {} self._groups = {}
self._gcode_map = {}
for index, d in data.items(): for index, d in data.items():
gcodes = d['gcodes'] gcodes = ensure_list(d['gcodes'])
if not isinstance(gcodes, list): gcodes = [gcode_set[gcode] for gcode in gcodes]
gcodes = [gcodes] group = ModalGroup(index, gcodes, d['meaning'])
group = ExecutionGroup(index, gcodes, d['meaning'])
self._groups[index] = group self._groups[index] = group
for gcode in gcodes: for gcode in gcodes:
self._gcode_map[gcode] = group gcode._modal_group = group
############################################## ##############################################
...@@ -438,10 +475,7 @@ class ModalGroups(YamlMixin, RstMixin): ...@@ -438,10 +475,7 @@ class ModalGroups(YamlMixin, RstMixin):
return iter(self._groups.values()) return iter(self._groups.values())
def __getitem__(self, index): def __getitem__(self, index):
if isinstance(index, int): return self._groups[index]
return self._groups[index]
else:
return self._gcode_map[index]
############################################## ##############################################
...@@ -484,45 +518,53 @@ class Config: ...@@ -484,45 +518,53 @@ class Config:
"""Each argument is a path to the corresponding YAML file. Files are loaded on demand (lazy loading). """Each argument is a path to the corresponding YAML file. Files are loaded on demand (lazy loading).
""" """
self._execution_order = str(execution_order) # self._letters = str(letters)
self._gcodes = str(gcodes) # self._parameters = str(parameters)
self._letters = str(letters)
self._modal_groups = str(modal_groups) # self._gcodes = str(gcodes)
self._parameters = str(parameters) # self._modal_groups = str(modal_groups)
# self._execution_order = str(execution_order)
self._letters = Letters(letters)
self._parameters = ParameterSet(parameters)
self._gcodes = GcodeSet(gcodes)
self._execution_order = ExecutionOrder(execution_order, self._gcodes)
self._modal_groups = ModalGroups(modal_groups, self._gcodes)
############################################## ##############################################
@property @property
def execution_order(self): def execution_order(self):
""":class:`ExecutionOrder` instance""" """:class:`ExecutionOrder` instance"""
if isinstance(self._execution_order, str): # if isinstance(self._execution_order, str):
self._execution_order = ExecutionOrder(self._execution_order) # self._execution_order = ExecutionOrder(self._execution_order)
return self._execution_order return self._execution_order
@property @property
def gcodes(self): def gcodes(self):
""":class:`Gcodes` instance""" """:class:`GcodeSet` instance"""
if isinstance(self._gcodes, str): # if isinstance(self._gcodes, str):
self._gcodes = Gcodes(self._gcodes) # self._gcodes = GcodeSet(self._gcodes)
return self._gcodes return self._gcodes
@property @property
def letters(self): def letters(self):
""":class:`Letters` instance""" """:class:`Letters` instance"""
if isinstance(self._letters, str): # if isinstance(self._letters, str):
self._letters = Letters(self._letters) # self._letters = Letters(self._letters)
return self._letters return self._letters
@property @property
def modal_groups(self): def modal_groups(self):
""":class:`ModalGroups` instance""" """:class:`ModalGroups` instance"""
if isinstance(self._modal_groups, str): # if isinstance(self._modal_groups, str):
self._modal_groups = ModalGroups(self._modal_groups) # self._modal_groups = ModalGroups(self._modal_groups)
return self._modal_groups return self._modal_groups
@property @property
def parameters(self): def parameters(self):
""":class:`Parameters` instance""" """:class:`ParameterSet` instance"""
if isinstance(self._parameters, str): # if isinstance(self._parameters, str):
self._parameters = Parameters(self._parameters) # self._parameters = ParameterSet(self._parameters)
return self._parameters return self._parameters
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment