edtlib: expose str_as_token() API

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 <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2022-07-08 11:18:10 -07:00 committed by Kumar Gala
parent 19bb6b330c
commit 4ef8c8e447

View file

@ -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)