Re: gnulib-tool.py: Add a new GLFileTable class.

2024-04-25 Thread Bruno Haible
Collin Funk wrote:
> for instance variables I
> rather put them outside __init__. It also serves as a decent place to
> add comments that don't belong in doc strings.

Yes. For instance variables that are used in several methods it would seem
odd to declare them in __init__. To me, __init__ is the place where they
are initialized, not declared.

Bruno






Re: gnulib-tool.py: Add a new GLFileTable class.

2024-04-25 Thread Collin Funk
On 4/25/24 1:11 AM, Bruno Haible wrote:
> Interesting syntax. This makes the class easier to understand. You're welcome
> to do the same thing with the other classes (except for GLError.message, which
> is private to a single method).

I agree. I originally wanted to add it to GLModuleTable when I was
looking at it previously. I imagine that section of code is probably
the most difficult for someone new to understand compared to the rest
of gnulib-tool.py.

Before I made the changes to GLModule.__hash__ the types were
something like this:

self.dependers: dict[str, list[str]] # 'module-name' : ['dep1', 'dep2']
self.conditionals: dict[str, str | bool] # key is 'module1---module2'

Which doesn't really tell us much without looking more into the class.
After my changes to GLModule.__hash__ it is a bit better:

self.dependers: defaultdict[GLModule, set[GLModule]]
self.conditionals: dict[tuple[GLModule, GLModule], str | bool]

This syntax can also be used on assignments in __init__ or for normal
variables like this:

# type hinted 'self.dependers = []'.
self.dependers: defaultdict[GLModule, set[GLModule]] = []

Which helps mypy and other type checkers who cannot infer the type
otherwise. I felt it was a bit cluttered so for instance variables I
rather put them outside __init__. It also serves as a decent place to
add comments that don't belong in doc strings.

I'll probably add them once I get around to making variables private
as we discussed previously.

> We are not going to lower the version dependency:
>   - No one asked for it in the beta testing process since last week.
>   - Python 3.6 (and even 3.7) is already end-of-life [1].

Sounds good. In any case it is always good to double check quickly
before modernizing like this. I run all the tests with Python 3.7
before submitting patches for the same reason.

Collin



Re: gnulib-tool.py: Add a new GLFileTable class.

2024-04-25 Thread Bruno Haible
Collin Funk wrote:
> I've applied the following patch adding a GLFileTable class as
> discussed here:
> 
> https://lists.gnu.org/archive/html/bug-gnulib/2024-04/msg00357.html

Thanks! Looks good.

> +class GLFileTable:
> +'''The GLFileTable class stores file information for the duration of the
> +operation being executed.'''
> +
> +all_files: list[str]
> +old_files: list[tuple[str, str]]
> +new_files: list[tuple[str, str]]
> +added_files: list[str]
> +removed_files: list[str]

Interesting syntax. This makes the class easier to understand. You're welcome
to do the same thing with the other classes (except for GLError.message, which
is private to a single method).

> This syntax was introduced in Python 3.6 which is okay for our
> dependency on Python 3.7. For older versions they can be removed or be
> placed in comments [1]. I've gone with it since I imagine there will
> be bigger fish to fry by lowering the version dependency.

We are not going to lower the version dependency:
  - No one asked for it in the beta testing process since last week.
  - Python 3.6 (and even 3.7) is already end-of-life [1].

Bruno

[1] https://en.wikipedia.org/wiki/History_of_Python#Table_of_versions






gnulib-tool.py: Add a new GLFileTable class.

2024-04-24 Thread Collin Funk
I've applied the following patch adding a GLFileTable class as
discussed here:

https://lists.gnu.org/archive/html/bug-gnulib/2024-04/msg00357.html

For the most part this just changes a dictionary in GLImport to an
actual class. So field initialization is done in GLFileTable.__init__()
and uses are changed like this:

filetable['all'] -> filetable.all_files
filetable['old'] -> filetable.old_files

This is beneficial since the fields can be individually typed. Since
old_files is a list[tuple[str, str]] where the tuple is:

 (rewritten-file-name, old-file-name)
 # for example, using gl_M4_BASE([glm4])
 ('glm4/gnulib-cache.m4', 'm4/gnulib-cache.m4')

while all_files only contains 'm4/gnulib-cache.m4'.

I've also used this class in GLTestDir since there are obvious ways to
reduce code duplication there. Specifically the rewrite_filename
stuff in GLImport.prepare() and GLTestDir.execute().

Since I noticed some discussion about bootstrapping, I have focused a
bit more on Python version compatibility. Just so that I have *some*
idea of what will need to be changed if there is ever a need.

+class GLFileTable:
+'''The GLFileTable class stores file information for the duration of the
+operation being executed.'''
+
+all_files: list[str]
+old_files: list[tuple[str, str]]
+new_files: list[tuple[str, str]]
+added_files: list[str]
+removed_files: list[str]

This syntax was introduced in Python 3.6 which is okay for our
dependency on Python 3.7. For older versions they can be removed or be
placed in comments [1]. I've gone with it since I imagine there will
be bigger fish to fry by lowering the version dependency.

[1] https://peps.python.org/pep-0484/#type-comments

CollinFrom 962397de1dc64f44c5736be14efd9ffb95c93c65 Mon Sep 17 00:00:00 2001
From: Collin Funk 
Date: Wed, 24 Apr 2024 16:04:25 -0700
Subject: [PATCH] gnulib-tool.py: Add a new GLFileTable class.

* pygnulib/GLFileTable.py: New file. Define the GLFileTable class with
five attributes which can be individually typed.
* pygnulib/GLTestDir.py (GLTestDir.execute): Use the GLFileTable class.
* pygnulib/GLImport.py (GLImport.gnulib_comp, GLImport.prepare)
(GLImport.execute): Likewise. Update type hints and doc strings.
---
 ChangeLog   |   9 
 pygnulib/GLFileTable.py |  36 ++
 pygnulib/GLImport.py| 102 +++-
 pygnulib/GLTestDir.py   |  18 +++
 4 files changed, 103 insertions(+), 62 deletions(-)
 create mode 100644 pygnulib/GLFileTable.py

diff --git a/ChangeLog b/ChangeLog
index 676aaef0be..ee17881d2a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-04-24  Collin Funk  
+
+	gnulib-tool.py: Add a new GLFileTable class.
+	* pygnulib/GLFileTable.py: New file. Define the GLFileTable class with
+	five attributes which can be individually typed.
+	* pygnulib/GLTestDir.py (GLTestDir.execute): Use the GLFileTable class.
+	* pygnulib/GLImport.py (GLImport.gnulib_comp, GLImport.prepare)
+	(GLImport.execute): Likewise. Update type hints and doc strings.
+
 2024-04-24  Paul Eggert  
 
 	largefile: port to C++
diff --git a/pygnulib/GLFileTable.py b/pygnulib/GLFileTable.py
new file mode 100644
index 00..6180e0d318
--- /dev/null
+++ b/pygnulib/GLFileTable.py
@@ -0,0 +1,36 @@
+# Copyright (C) 2002-2024 Free Software Foundation, Inc.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+from __future__ import annotations
+
+
+class GLFileTable:
+'''The GLFileTable class stores file information for the duration of the
+operation being executed.'''
+
+all_files: list[str]
+old_files: list[tuple[str, str]]
+new_files: list[tuple[str, str]]
+added_files: list[str]
+removed_files: list[str]
+
+def __init__(self, all_files: list[str]) -> None:
+'''Create a GLFileTable with initialized fields.
+- all_files, a list of all files being operated on.'''
+self.all_files = all_files
+self.old_files = []
+self.new_files = []
+self.added_files = []
+self.removed_files = []
diff --git a/pygnulib/GLImport.py b/pygnulib/GLImport.py
index 0bab7ca150..cf1ebd10ec 100644
--- a/pygnulib/GLImport.py
+++ b/pygnulib/GLImport.py
@@ -46,6 +46,7 @@
 from .GLFileSystem import GLFileAssistant
 from .GLMakefileTable import GLMakefileTable
 from .GLEmiter import GLEmiter
+from .GLF