mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-28 00:21:48 +03:00
Unify the _format_parameter
function among the ConfigFile
subclasses
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
This commit is contained in:
71
scripts/config.py
Normal file → Executable file
71
scripts/config.py
Normal file → Executable file
@ -19,7 +19,7 @@ Basic usage, to read the Mbed TLS configuration:
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from abc import ABCMeta, abstractmethod
|
from abc import ABCMeta
|
||||||
|
|
||||||
class Setting:
|
class Setting:
|
||||||
"""Representation of one Mbed TLS mbedtls_config.h setting.
|
"""Representation of one Mbed TLS mbedtls_config.h setting.
|
||||||
@ -475,9 +475,29 @@ class ConfigFile(metaclass=ABCMeta):
|
|||||||
yield setting
|
yield setting
|
||||||
self.current_section = None
|
self.current_section = None
|
||||||
|
|
||||||
@abstractmethod
|
#pylint: disable=no-self-use
|
||||||
def _format_template(self, setting, name, indent, middle):
|
def _format_template(self, setting, indent, middle):
|
||||||
pass
|
"""Build a line for mbedtls_config.h for the given setting.
|
||||||
|
|
||||||
|
The line has the form "<indent>#define <name> <value>"
|
||||||
|
where <middle> is "#define <name> ".
|
||||||
|
"""
|
||||||
|
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):
|
def write_to_stream(self, settings, output):
|
||||||
"""Write the whole configuration to output."""
|
"""Write the whole configuration to output."""
|
||||||
@ -485,8 +505,8 @@ class ConfigFile(metaclass=ABCMeta):
|
|||||||
if isinstance(template, str):
|
if isinstance(template, str):
|
||||||
line = template
|
line = template
|
||||||
else:
|
else:
|
||||||
name, _, _ = template
|
name, indent, middle = template
|
||||||
line = self._format_template(settings[name], *template)
|
line = self._format_template(settings[name], indent, middle)
|
||||||
output.write(line + '\n')
|
output.write(line + '\n')
|
||||||
|
|
||||||
def write(self, settings, filename=None):
|
def write(self, settings, filename=None):
|
||||||
@ -519,29 +539,6 @@ class MbedtlsConfigFile(ConfigFile):
|
|||||||
super().__init__(self.default_path, 'Mbed TLS', filename)
|
super().__init__(self.default_path, 'Mbed TLS', filename)
|
||||||
self.current_section = 'header'
|
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 "<indent>#define <name> <value>"
|
|
||||||
where <middle> is "#define <name> ".
|
|
||||||
"""
|
|
||||||
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):
|
class CryptoConfigFile(ConfigFile):
|
||||||
"""Representation of an Crypto configuration file."""
|
"""Representation of an Crypto configuration file."""
|
||||||
|
|
||||||
@ -556,22 +553,6 @@ class CryptoConfigFile(ConfigFile):
|
|||||||
def __init__(self, filename=None):
|
def __init__(self, filename=None):
|
||||||
super().__init__(self.default_path, 'Crypto', filename)
|
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 "<indent>#define <name> <value>"
|
|
||||||
where <middle> is "#define <name> ".
|
|
||||||
"""
|
|
||||||
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):
|
class MbedtlsConfig(Config):
|
||||||
"""Representation of the Mbed TLS configuration.
|
"""Representation of the Mbed TLS configuration.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user