* pygnulib/GLModuleSystem.py (GLModule.shell_id_chars): Remove class
variable.
(GLModule.shell_id_pattern): New class variable.
(GLModule.getShellId): New function.
(GLModule.getShellFunc, GLModule.getShellVar)
(GLModule.getConditionalName): Use it.
---
 ChangeLog                  | 10 ++++++++
 pygnulib/GLModuleSystem.py | 48 ++++++++------------------------------
 2 files changed, 20 insertions(+), 38 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a405680df3..9be0407793 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2024-05-28  Collin Funk  <collin.fu...@gmail.com>
+
+       gnulib-tool.py: Simplify creation of module shell ids.
+       * pygnulib/GLModuleSystem.py (GLModule.shell_id_chars): Remove class
+       variable.
+       (GLModule.shell_id_pattern): New class variable.
+       (GLModule.getShellId): New function.
+       (GLModule.getShellFunc, GLModule.getShellVar)
+       (GLModule.getConditionalName): Use it.
+
 2024-05-28  Bruno Haible  <br...@clisp.org>
 
        pthread-once: Work around Cygwin 3.5.3 bug.
diff --git a/pygnulib/GLModuleSystem.py b/pygnulib/GLModuleSystem.py
index 872961ba91..63d4f4a73e 100644
--- a/pygnulib/GLModuleSystem.py
+++ b/pygnulib/GLModuleSystem.py
@@ -166,8 +166,8 @@ class GLModule:
                    + r'Makefile\.am|Include|Link|License|Maintainer):$',
                    re.M)
 
-    # List of characters allowed in shell identifiers.
-    shell_id_chars: ClassVar[str] = 
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
+    # Regular expression matching module names that can be used as shell ids.
+    shell_id_pattern: ClassVar[re.Pattern] = re.compile(r'^\w*$')
 
     cache: dict[str, Any]
     content: str
@@ -378,56 +378,28 @@ def getLinkDirectiveRecursively(self) -> str:
                               for line in section.splitlines() })
         return lines_to_multiline(directives)
 
+    def getShellId(self) -> str:
+        if re.match(self.shell_id_pattern, self.name):
+            return self.name
+        return 
hashlib.md5(f'{self.name}\n'.encode(ENCS['default'])).hexdigest()
+
     def getShellFunc(self) -> str:
         '''Computes the shell function name that will contain the m4 macros
         for the module.'''
         macro_prefix = self.config['macro_prefix']
-        valid_shell_id = True
-        for char in self.name:
-            if char not in GLModule.shell_id_chars:
-                valid_shell_id = False
-                break
-        if valid_shell_id:
-            identifier = self.name
-        else:
-            hash_input = '%s\n' % self.name
-            identifier = 
hashlib.md5(hash_input.encode(ENCS['default'])).hexdigest()
-        result = 'func_%s_gnulib_m4code_%s' % (macro_prefix, identifier)
-        return result
+        return 'func_%s_gnulib_m4code_%s' % (macro_prefix, self.getShellId())
 
     def getShellVar(self) -> str:
         '''Compute the shell variable name the will be set to true once the
         m4 macros for the module have been executed.'''
         macro_prefix = self.config['macro_prefix']
-        valid_shell_id = True
-        for char in self.name:
-            if char not in GLModule.shell_id_chars:
-                valid_shell_id = False
-                break
-        if valid_shell_id:
-            identifier = self.name
-        else:
-            hash_input = '%s\n' % self.name
-            identifier = 
hashlib.md5(hash_input.encode(ENCS['default'])).hexdigest()
-        result = '%s_gnulib_enabled_%s' % (macro_prefix, identifier)
-        return result
+        return '%s_gnulib_enabled_%s' % (macro_prefix, self.getShellId())
 
     def getConditionalName(self) -> str:
         '''Return the automake conditional name.
         GLConfig: macro_prefix.'''
         macro_prefix = self.config['macro_prefix']
-        valid_shell_id = True
-        for char in self.name:
-            if char not in GLModule.shell_id_chars:
-                valid_shell_id = False
-                break
-        if valid_shell_id:
-            identifier = self.name
-        else:
-            hash_input = '%s\n' % self.name
-            identifier = 
hashlib.md5(hash_input.encode(ENCS['default'])).hexdigest()
-        result = '%s_GNULIB_ENABLED_%s' % (macro_prefix, identifier)
-        return result
+        return '%s_GNULIB_ENABLED_%s' % (macro_prefix, self.getShellId())
 
     def getDescription(self) -> str:
         '''Return description of the module.'''
-- 
2.45.1


Reply via email to