From 0497698203124c7798ab87e73c966a6b782a240c Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Tue, 19 Jan 2021 18:04:13 +0100 Subject: [PATCH] pylint --- src/rougail/annotator/property.py | 2 +- src/rougail/annotator/service.py | 3 +- src/rougail/tiramisureflector.py | 163 +++++++++++++++++------------- 3 files changed, 94 insertions(+), 74 deletions(-) diff --git a/src/rougail/annotator/property.py b/src/rougail/annotator/property.py index 44957850..da0f3c5d 100644 --- a/src/rougail/annotator/property.py +++ b/src/rougail/annotator/property.py @@ -44,7 +44,7 @@ class PropertyAnnotator: f'variable "{variable.name}" in {xmlfiles}') raise DictConsistencyError(msg, 50) if properties: - variable.properties = frozenset(properties) + variable.properties = set(properties) def convert_services(self) -> None: """convert services diff --git a/src/rougail/annotator/service.py b/src/rougail/annotator/service.py index dbd61d12..04b2e06a 100644 --- a/src/rougail/annotator/service.py +++ b/src/rougail/annotator/service.py @@ -10,7 +10,8 @@ from ..error import DictConsistencyError # that shall not be present in the exported (flatened) XML ERASED_ATTRIBUTES = ('redefine', 'exists', 'fallback', 'optional', 'remove_check', 'namespace', 'remove_condition', 'path', 'instance_mode', 'index', 'is_in_leadership', - 'level', 'remove_fill', 'xmlfiles', 'type', 'reflector_name', 'reflector_object',) + 'level', 'remove_fill', 'xmlfiles', 'type', 'reflector_name', + 'reflector_object',) KEY_TYPE = {'variable': 'symlink', diff --git a/src/rougail/tiramisureflector.py b/src/rougail/tiramisureflector.py index 5cccd190..36463ce4 100644 --- a/src/rougail/tiramisureflector.py +++ b/src/rougail/tiramisureflector.py @@ -3,6 +3,7 @@ flattened XML specific """ from .config import Config from .annotator import ERASED_ATTRIBUTES, CONVERT_OPTION +#from .objspace import UnRedefinable, Redefinable, Atom FUNC_TO_DICT = [] @@ -45,7 +46,7 @@ class TiramisuReflector: baseelt = BaseElt() self.set_name(baseelt) basefamily = Family(baseelt, - self, + self.text, ) for elt in self.reorder_family(space): self.iter_family(basefamily, @@ -101,7 +102,7 @@ class TiramisuReflector: """ self.set_name(elt) family = Family(elt, - self, + self.text, ) parent_family.add(family) for children in self.get_children(elt): @@ -130,7 +131,7 @@ class TiramisuReflector: ): """Populate variable """ - if family.is_leader: + if family.is_leadership: is_leader = elt.name == family.elt.variable[0].name is_follower = not is_leader else: @@ -138,7 +139,7 @@ class TiramisuReflector: is_follower = False self.set_name(elt) family.add(Variable(elt, - self, + self.text, is_follower, is_leader, )) @@ -146,6 +147,8 @@ class TiramisuReflector: def set_name(self, elt, ): + """Set name + """ elt.reflector_name = f'option_{self.index}' self.index += 1 @@ -167,14 +170,14 @@ class Common: """Common function for variable and family """ def __init__(self, - storage, - is_leader, + elt, + text, ): + self.elt = elt self.option_name = None self.attrib = {} self.informations = {} - self.storage = storage - self.is_leader = is_leader + self.text = text self.elt.reflector_object = self def populate_properties(self, child): @@ -192,6 +195,14 @@ class Common: self.attrib['properties'] += ', ' self.attrib['properties'] += prop + def properties_to_string(self): + """Change properties to string + """ + if not self.attrib['properties']: + self.attrib['properties'] = '' + else: + self.attrib['properties'] = "'" + "', '".join(sorted(self.attrib['properties'])) + "'" + def get_attrib(self): """Get attributes """ @@ -216,7 +227,7 @@ class Common: for key, value in self.informations.items(): if isinstance(value, str): value = '"' + value.replace('"', '\"') + '"' - self.storage.text.append(f'{self.option_name}.impl_set_information("{key}", {value})') + self.text.append(f'{self.option_name}.impl_set_information("{key}", {value})') def get_attributes(self, space): # pylint: disable=R0201 """Get attributes @@ -248,14 +259,12 @@ class Variable(Common): """ def __init__(self, elt, - storage, + text, is_follower, is_leader, ): - self.elt = elt - super().__init__(storage, - is_leader, - ) + super().__init__(elt, text) + self.is_leader = is_leader self.is_follower = is_follower convert_option = CONVERT_OPTION[elt.type] self.object_type = convert_option['opttype'] @@ -275,7 +284,7 @@ class Variable(Common): self.parse_children() attrib = self.get_attrib() self.option_name = self.elt.reflector_name - self.storage.text.append(f'{self.option_name} = {self.object_type}({attrib})') + self.text.append(f'{self.option_name} = {self.object_type}({attrib})') self.populate_informations() return self.option_name @@ -303,44 +312,55 @@ class Variable(Common): if self.attrib['multi'] == 'submulti' and self.is_follower: self.attrib['default_multi'] = [] choices = [] - if 'properties' in self.attrib: - if self.attrib['properties']: - self.attrib['properties'] = "'" + \ - "', '".join(sorted(list(self.attrib['properties']))) + "'" - else: - self.attrib['properties'] = '' + self.properties_to_string() for tag, children in self.get_children(self.elt): for child in children: - if tag == 'property': - self.populate_properties(child) - elif tag == 'value': - if child.type == 'calculation': - self.attrib['default'] = self.calculation_value(child, []) - else: - self.populate_value(child) - elif tag == 'check': - validator = self.calculation_value(child, ['ParamSelfOption()']) - self.attrib['validators'].append(validator) - elif tag == 'choice': - if child.type == 'calculation': - value = child.name.reflector_object.get() - choices = f"Calculation(func.calc_value, Params((ParamOption({value}))))" - else: - choices.append(child.name) + self.parse_child(tag, + child, + choices, + ) if choices: - if isinstance(choices, list): - self.attrib['values'] = str(tuple(choices)) - else: - self.attrib['values'] = choices - if self.attrib['default'] == []: + self.attrib['values'] = str(tuple(choices)) + if not self.attrib['default']: del self.attrib['default'] elif not self.attrib['multi'] and isinstance(self.attrib['default'], list): self.attrib['default'] = self.attrib['default'][-1] - if self.attrib['validators'] == []: + if not self.attrib['validators']: del self.attrib['validators'] else: self.attrib['validators'] = '[' + ', '.join(self.attrib['validators']) + ']' + def parse_child(self, + tag, + child, + choices, + ) -> None: + """Parse child + """ + if tag == 'property': + self.populate_properties(child) + elif tag == 'value': + self.populate_value(child) + elif tag == 'check': + validator = self.calculation_value(child, ['ParamSelfOption()']) + self.attrib['validators'].append(validator) + elif tag == 'choice': + self.calculate_choice(child, + choices, + ) + + def calculate_choice(self, + child, + choices: list, + ) -> None: + """Calculating choice + """ + if child.type == 'calculation': + value = child.name.reflector_object.get() + self.attrib['values'] = f"Calculation(func.calc_value, Params((ParamOption({value}))))" + else: + choices.append(child.name) + def calculation_value(self, child, args, @@ -393,23 +413,25 @@ class Variable(Common): ): """Populate variable's values """ - value = child.name - if self.attrib['multi'] == 'submulti': - self.attrib['default_multi'].append(value) - elif self.is_follower: - self.attrib['default_multi'] = value - elif self.attrib['multi']: - self.attrib['default'].append(value) - if not self.is_leader: - self.attrib['default_multi'] = value - elif isinstance(value, (int, float)) or value is None: - self.attrib['default'].append(value) + if child.type == 'calculation': + self.attrib['default'] = self.calculation_value(child, []) else: - self.attrib['default'].append("'" + value + "'") + value = child.name + if self.attrib['multi'] == 'submulti': + self.attrib['default_multi'].append(value) + elif self.is_follower: + self.attrib['default_multi'] = value + elif self.attrib['multi']: + self.attrib['default'].append(value) + if not self.is_leader: + self.attrib['default_multi'] = value + elif isinstance(value, (int, float)) or value is None: + self.attrib['default'].append(value) + else: + self.attrib['default'].append("'" + value + "'") - def build_param(self, - param, - ): + @staticmethod + def build_param(param) -> str: """build variable parameters """ option_name = param['option'].reflector_object.get() @@ -425,12 +447,10 @@ class Family(Common): """ def __init__(self, elt, - storage, + text, ): - self.elt = elt - super().__init__(storage, - elt.__class__.__name__ == 'Leadership', - ) + super().__init__(elt, text) + self.is_leadership = self.elt.__class__.__name__ == 'Leadership' self.children = [] def add(self, child): @@ -448,7 +468,7 @@ class Family(Common): object_name = self.get_object_name() attrib = self.get_attrib() + \ ', children=[' + ', '.join([child.get() for child in self.children]) + ']' - self.storage.text.append(f'{self.option_name} = {object_name}({attrib})') + self.text.append(f'{self.option_name} = {object_name}({attrib})') self.populate_informations() return self.option_name @@ -470,17 +490,16 @@ class Family(Common): """parse current children """ if 'properties' in self.attrib: - self.attrib['properties'] = "'" + \ - "', '".join(sorted(list(self.attrib['properties']))) + "'" - if hasattr(self.elt, 'property'): - for child in self.elt.property: - self.populate_properties(child) + self.properties_to_string() + if hasattr(self.elt, 'property'): + for child in self.elt.property: + self.populate_properties(child) def get_object_name(self): """Get family object's name """ if 'suffixes' in self.attrib: return 'ConvertDynOptionDescription' - if not self.is_leader: - return 'OptionDescription' - return 'Leadership' + if self.is_leadership: + return 'Leadership' + return 'OptionDescription'