Collin Funk wrote:
> Then only set it to the cache or
> default in GLImport.__init__(). But that section of code is already
> sort of difficult to follow...

I am applying these two refactorings. Hope it makes this piece of code
easier to understand.

>From 27c5b31a42ccd5353ccff09d46dbaa0d105f0f0b Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sat, 13 Apr 2024 00:10:44 +0200
Subject: [PATCH 1/2] gnulib-tool.py: Optimize.

* pygnulib/GLConfig.py (GLConfig.update, GLConfig.update_key): Avoid
useless cloning of dictionaries.
---
 ChangeLog            |  6 ++++++
 pygnulib/GLConfig.py | 24 +++++++++++++++---------
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b25819a4aa..212a78b305 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-04-12  Bruno Haible  <br...@clisp.org>
+
+	gnulib-tool.py: Optimize.
+	* pygnulib/GLConfig.py (GLConfig.update, GLConfig.update_key): Avoid
+	useless cloning of dictionaries.
+
 2024-04-12  Bruno Haible  <br...@clisp.org>
 
 	gnulib-tool.py: Implement --no-conditional-dependencies correctly.
diff --git a/pygnulib/GLConfig.py b/pygnulib/GLConfig.py
index e938b30a5e..515047d3f0 100644
--- a/pygnulib/GLConfig.py
+++ b/pygnulib/GLConfig.py
@@ -275,21 +275,27 @@ class GLConfig:
         if type(dictionary) is not GLConfig:
             raise TypeError('dictionary must be a GLConfig, not %s'
                             % type(dictionary).__name__)
-        dictionary = dict(dictionary.table)
+        dictionary = dictionary.table
         result = dict()
         for key in dictionary:
             src = self.table[key]
             dest = dictionary[key]
-            result[key] = src
-            if src != dest:
+            # Merge src and dest into a single value.
+            if src == dest:
+                value = src
+            else:
                 if self.isdefault(key, src):
-                    result[key] = dest
+                    value = dest
                 else:  # if not self.isdefault(key, src)
-                    if not self.isdefault(key, dest):
+                    if self.isdefault(key, dest):
+                        value = src
+                    else:  # if not self.isdefault(key, dest)
                         if key in ['modules', 'avoids', 'tests']:
-                            dest = sorted(set(src + dest))
-                        result[key] = dest
-        self.table = dict(result)
+                            value = sorted(set(src + dest))
+                        else:
+                            value = dest
+            result[key] = value
+        self.table = result
 
     def update_key(self, dictionary: GLConfig, key: str) -> None:
         '''Update the given key using value from the given dictionary.'''
@@ -297,7 +303,7 @@ class GLConfig:
             if type(dictionary) is not GLConfig:
                 raise TypeError('dictionary must be a GLConfig, not %s'
                                 % type(dictionary).__name__)
-            dictionary = dict(dictionary.table)
+            dictionary = dictionary.table
             self.table[key] = dictionary[key]
         else:  # if key not in self.table
             raise KeyError('GLConfig does not contain key: %s' % repr(key))
-- 
2.34.1

>From 48f615b71b7299e7416ca46db076b7e61820a2f2 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sat, 13 Apr 2024 00:24:52 +0200
Subject: [PATCH 2/2] gnulib-tool.py: Refactor.

* pygnulib/GLConfig.py (GLConfig.update, GLConfig.update_key): Improve
variable names and comments.
* pygnulib/GLImport.py (GLImport.__init__): Improve comments.
---
 ChangeLog            |  7 +++++++
 pygnulib/GLConfig.py | 24 ++++++++++++------------
 pygnulib/GLImport.py | 30 +++++++++++++++++++++++-------
 3 files changed, 42 insertions(+), 19 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 212a78b305..4d5725f843 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-04-12  Bruno Haible  <br...@clisp.org>
+
+	gnulib-tool.py: Refactor.
+	* pygnulib/GLConfig.py (GLConfig.update, GLConfig.update_key): Improve
+	variable names and comments.
+	* pygnulib/GLImport.py (GLImport.__init__): Improve comments.
+
 2024-04-12  Bruno Haible  <br...@clisp.org>
 
 	gnulib-tool.py: Optimize.
diff --git a/pygnulib/GLConfig.py b/pygnulib/GLConfig.py
index 515047d3f0..ab1cb218be 100644
--- a/pygnulib/GLConfig.py
+++ b/pygnulib/GLConfig.py
@@ -270,12 +270,12 @@ class GLConfig:
         table = copy.deepcopy(self)
         return table
 
-    def update(self, dictionary: GLConfig) -> None:
-        '''Specify the dictionary whose keys will be used to update config.'''
-        if type(dictionary) is not GLConfig:
-            raise TypeError('dictionary must be a GLConfig, not %s'
-                            % type(dictionary).__name__)
-        dictionary = dictionary.table
+    def update(self, other_config: GLConfig) -> None:
+        '''Merges all non-default values from other_config into this config.'''
+        if type(other_config) is not GLConfig:
+            raise TypeError('other_config must be a GLConfig, not %s'
+                            % type(other_config).__name__)
+        dictionary = other_config.table
         result = dict()
         for key in dictionary:
             src = self.table[key]
@@ -297,13 +297,13 @@ class GLConfig:
             result[key] = value
         self.table = result
 
-    def update_key(self, dictionary: GLConfig, key: str) -> None:
-        '''Update the given key using value from the given dictionary.'''
+    def update_key(self, other_config: GLConfig, key: str) -> None:
+        '''Copies the value for key from other_config into this config.'''
         if key in self.table:
-            if type(dictionary) is not GLConfig:
-                raise TypeError('dictionary must be a GLConfig, not %s'
-                                % type(dictionary).__name__)
-            dictionary = dictionary.table
+            if type(other_config) is not GLConfig:
+                raise TypeError('other_config must be a GLConfig, not %s'
+                                % type(other_config).__name__)
+            dictionary = other_config.table
             self.table[key] = dictionary[key]
         else:  # if key not in self.table
             raise KeyError('GLConfig does not contain key: %s' % repr(key))
diff --git a/pygnulib/GLImport.py b/pygnulib/GLImport.py
index b6c9bbeea3..c931536ea9 100644
--- a/pygnulib/GLImport.py
+++ b/pygnulib/GLImport.py
@@ -84,24 +84,36 @@ class GLImport:
             raise TypeError('mode must be 0 <= mode <= 3, not %s'
                             % repr(mode))
 
-        # Initialize some values.
-        self.cache = GLConfig()
+        # config contains the configuration, as specified through command-line
+        # parameters.
+
+        # self.config records the configuration that we are building/deducing.
+        # Initialize it with the properties from config.
         self.config = config.copy()
+
+        # self.cache is the configuration extracted from some files on the
+        # file system: configure.{ac,in}, gnulib-cache.m4, gnulib-comp.m4.
+        self.cache = GLConfig()
         os.rmdir(self.cache['tempdir'])
 
-        # Get cached auxdir and libtool from configure.ac/in.
-        self.cache.setAuxDir('.')
+        # Read configure.{ac,in}.
         with codecs.open(self.config.getAutoconfFile(), 'rb', 'UTF-8') as file:
             data = file.read()
+
+        # Get cached auxdir and libtool from configure.{ac,in}.
+        self.cache.setAuxDir('.')
         pattern = re.compile(r'^AC_CONFIG_AUX_DIR\([\[ ]*([^]"$`\\)]+).*?$', re.MULTILINE)
         match = pattern.search(data)
         if match:
             self.cache.setAuxDir(match.group(1))
-        pattern = re.compile(r'A[CM]_PROG_LIBTOOL', re.M)
-        guessed_libtool = bool(pattern.findall(data))
         if self.config['auxdir'] == '':
             self.config.setAuxDir(self.cache['auxdir'])
 
+        # Get libtool guess from configure.{ac,in}.
+        # XXX This is not actually used.
+        pattern = re.compile(r'A[CM]_PROG_LIBTOOL', re.M)
+        guessed_libtool = bool(pattern.findall(data))
+
         # Guess autoconf version.
         pattern = re.compile(r'.*AC_PREREQ\((.*)\)', re.M)
         versions = cleaner(pattern.findall(data))
@@ -244,12 +256,16 @@ class GLImport:
             elif self.mode == MODES['update']:
                 modules = self.cache.getModules()
 
-            # Update configuration dictionary.
+            # Merge the configuration found on disk.
             self.config.update(self.cache)
+
+            # Merge with the configuration from the command-line parameters;
+            # they override the configuration found on disk.
             for key in config.keys():
                 value = config[key]
                 if not config.isdefault(key, value):
                     self.config.update_key(config, key)
+
             self.config.setModules(modules)
 
         # Determine whether --automake-subdir/--automake-subdir-tests are supported.
-- 
2.34.1

Reply via email to