From 4ef8c8e44711f68798ff51b8b283003c68e31fcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Bol=C3=ADvar?= Date: Fri, 8 Jul 2022 11:18:10 -0700 Subject: [PATCH] edtlib: expose str_as_token() API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some callers need to be able to convert strings to tokens in the same way edtlib does. Make this possible by exposing the internal helper function used to do that under a suitable name. Signed-off-by: Martí Bolívar --- .../dts/python-devicetree/src/devicetree/edtlib.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/scripts/dts/python-devicetree/src/devicetree/edtlib.py b/scripts/dts/python-devicetree/src/devicetree/edtlib.py index a2ef70e5aa9..9e7dc1baaac 100644 --- a/scripts/dts/python-devicetree/src/devicetree/edtlib.py +++ b/scripts/dts/python-devicetree/src/devicetree/edtlib.py @@ -1552,7 +1552,7 @@ class PinCtrl: @property def name_as_token(self): "See the class docstring" - return _val_as_token(self.name) if self.name is not None else None + return str_as_token(self.name) if self.name is not None else None def __repr__(self): fields = [] @@ -1645,7 +1645,7 @@ class Property: @property def val_as_token(self): "See the class docstring" - return _val_as_token(self.val) + return str_as_token(self.val) @property def enum_index(self): @@ -2929,7 +2929,12 @@ _LOG = logging.getLogger(__name__) _NOT_ALPHANUM_OR_UNDERSCORE = re.compile(r'\W', re.ASCII) -def _val_as_token(val): +def str_as_token(val): + """Return a canonical representation of a string as a C token. + + This converts special characters in 'val' to underscores, and + returns the result.""" + return re.sub(_NOT_ALPHANUM_OR_UNDERSCORE, '_', val)