From 8eb3b99f276b5d8e2c78d9e40023a62a794b5302 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Tue, 26 Jan 2021 13:42:48 +0100 Subject: [PATCH] reorganize modes --- src/rougail/annotator/family.py | 86 +++++++++++++-------------------- 1 file changed, 34 insertions(+), 52 deletions(-) diff --git a/src/rougail/annotator/family.py b/src/rougail/annotator/family.py index d393cd8a..0034787b 100644 --- a/src/rougail/annotator/family.py +++ b/src/rougail/annotator/family.py @@ -38,13 +38,23 @@ class FamilyAnnotator: self.objectspace = objectspace if not hasattr(self.objectspace.space, 'variables'): return - self.family_names() self.remove_empty_families() - self.change_variable_mode() - self.change_family_mode() + self.family_names() + self.change_modes() self.dynamic_families() self.convert_help() + def remove_empty_families(self) -> None: + """Remove all families without any variable + """ + for families in self.objectspace.space.variables.values(): + removed_families = [] + for family_name, family in families.family.items(): + if not hasattr(family, 'variable') or len(family.variable) == 0: + removed_families.append(family_name) + for family_name in removed_families: + del families.family[family_name] + def family_names(self) -> None: """Set doc, path, ... to family """ @@ -59,23 +69,14 @@ class FamilyAnnotator: setattr(family, key, None) family.name = normalize_family(family.name) - def remove_empty_families(self) -> None: - """Remove all families without any variable - """ - for families in self.objectspace.space.variables.values(): - removed_families = [] - for family_name, family in families.family.items(): - if not hasattr(family, 'variable') or len(family.variable) == 0: - removed_families.append(family_name) - for family_name in removed_families: - del families.family[family_name] - - def change_variable_mode(self): + def change_modes(self): """change the mode of variables """ - for variables in self.objectspace.space.variables.values(): - for family in variables.family.values(): + for families in self.objectspace.space.variables.values(): + for family in families.family.values(): family_mode = family.mode + # default is high level + new_family_mode = modes_level[-1] for variable in family.variable.values(): if not isinstance(variable, self.objectspace.leadership): func = self._change_variabe_mode @@ -84,19 +85,28 @@ class FamilyAnnotator: func(variable, family_mode, ) + if modes[new_family_mode] > modes[variable.mode]: + new_family_mode = variable.mode + # set the lower variable mode to family + family.mode = new_family_mode def _change_variabe_mode(self, variable, family_mode: str, + is_follower=False, ) -> None: # auto_save or auto_freeze variable is set to 'basic' mode # if its mode is not defined by the user if 'mode' not in vars(variable) and \ (variable.auto_save is True or variable.auto_freeze is True): variable.mode = modes_level[0] - self._annotate_variable(variable, - family_mode, - ) + # mandatory variable without value is a basic variable + if variable.mandatory is True and not hasattr(variable, 'default'): + variable.mode = modes_level[0] + # none basic variable in high level family has to be in high level + if modes[variable.mode] < modes[family_mode] and \ + (not is_follower or variable.mode != modes_level[0]): + variable.mode = family_mode def _change_variable_mode_leader(self, leadership, @@ -113,10 +123,10 @@ class FamilyAnnotator: xmlfiles = self.objectspace.display_xmlfiles(leadership.xmlfiles) msg = f'leader/followers "{follower.name}" could not be auto_freeze in {xmlfiles}' raise DictConsistencyError(_(msg), 30) - self._annotate_variable(follower, - family_mode, - is_follower, - ) + self._change_variabe_mode(follower, + family_mode, + is_follower, + ) if leader_mode is None: leader_mode = leadership.variable[0].mode leadership.variable[0].mode = None @@ -127,34 +137,6 @@ class FamilyAnnotator: is_follower = True leadership.mode = leader_mode - def _annotate_variable(self, - variable, - family_mode: str, - is_follower=False, - ) -> None: - # mandatory variable without value is a basic variable - if variable.mandatory is True and not hasattr(variable, 'default'): - variable.mode = modes_level[0] - # none basic variable in high level family has to be in high level - if modes[variable.mode] < modes[family_mode] and \ - (not is_follower or variable.mode != modes_level[0]): - variable.mode = family_mode - - def change_family_mode(self): - """change mode of a family - """ - for families in self.objectspace.space.variables.values(): - for family in families.family.values(): - # default is high level - mode = modes_level[-1] - # get de lower sub variable mode - for variable in family.variable.values(): - variable_mode = variable.mode - if modes[mode] > modes[variable_mode]: - mode = variable_mode - # set the lower variable mode to family - family.mode = mode - def dynamic_families(self): """link dynamic families to object """