From e7742b360d4e9dd237a3bfb373589f73c2da1a5d Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 26 Jun 2024 18:04:09 +0200 Subject: [PATCH] Unify the `_format_parameter` function among the `ConfigFile` subclasses Signed-off-by: Gabor Mezei --- scripts/config.py | 71 +++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 45 deletions(-) mode change 100644 => 100755 scripts/config.py diff --git a/scripts/config.py b/scripts/config.py old mode 100644 new mode 100755 index fcc3fff686..cd6a4b4f4a --- a/scripts/config.py +++ b/scripts/config.py @@ -19,7 +19,7 @@ Basic usage, to read the Mbed TLS configuration: import os import re -from abc import ABCMeta, abstractmethod +from abc import ABCMeta class Setting: """Representation of one Mbed TLS mbedtls_config.h setting. @@ -475,9 +475,29 @@ class ConfigFile(metaclass=ABCMeta): yield setting self.current_section = None - @abstractmethod - def _format_template(self, setting, name, indent, middle): - pass + #pylint: disable=no-self-use + def _format_template(self, setting, indent, middle): + """Build a line for mbedtls_config.h for the given setting. + + The line has the form "#define " + where is "#define ". + """ + value = setting.value + if value is None: + value = '' + # Normally the whitespace to separate the symbol name from the + # value is part of middle, and there's no whitespace for a symbol + # with no value. But if a symbol has been changed from having a + # value to not having one, the whitespace is wrong, so fix it. + if value: + if middle[-1] not in '\t ': + middle += ' ' + else: + middle = middle.rstrip() + return ''.join([indent, + '' if setting.active else '//', + middle, + value]).rstrip() def write_to_stream(self, settings, output): """Write the whole configuration to output.""" @@ -485,8 +505,8 @@ class ConfigFile(metaclass=ABCMeta): if isinstance(template, str): line = template else: - name, _, _ = template - line = self._format_template(settings[name], *template) + name, indent, middle = template + line = self._format_template(settings[name], indent, middle) output.write(line + '\n') def write(self, settings, filename=None): @@ -519,29 +539,6 @@ class MbedtlsConfigFile(ConfigFile): super().__init__(self.default_path, 'Mbed TLS', filename) self.current_section = 'header' - def _format_template(self, setting, name, indent, middle): - """Build a line for mbedtls_config.h for the given setting. - - The line has the form "#define " - where is "#define ". - """ - value = setting.value - if value is None: - value = '' - # Normally the whitespace to separate the symbol name from the - # value is part of middle, and there's no whitespace for a symbol - # with no value. But if a symbol has been changed from having a - # value to not having one, the whitespace is wrong, so fix it. - if value: - if middle[-1] not in '\t ': - middle += ' ' - else: - middle = middle.rstrip() - return ''.join([indent, - '' if setting.active else '//', - middle, - value]).rstrip() - class CryptoConfigFile(ConfigFile): """Representation of an Crypto configuration file.""" @@ -556,22 +553,6 @@ class CryptoConfigFile(ConfigFile): def __init__(self, filename=None): super().__init__(self.default_path, 'Crypto', filename) - def _format_template(self, setting, name, indent, middle): - """Build a line for crypto_config.h for the given setting. - - The line has the form "#define " - where is "#define ". - """ - value = setting.value - if value is None: - value = '1' - if middle[-1] not in '\t ': - middle += ' ' - return ''.join([indent, - '' if setting.active else '//', - middle, - value]).rstrip() - class MbedtlsConfig(Config): """Representation of the Mbed TLS configuration.