jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/674317 )

Change subject: [IMPR] collect family files from zip folder
......................................................................

[IMPR] collect family files from zip folder

if the given folder_path of register_family_file funtion is not a
directory it probably resides in a zip folder. In that case check
the current path and its parents whether it is a zip file. If no
zipfile is found raise a NotADirectoryError. Otherwise collect all
files inside the folder and filter the family files by the suffix.

Bug: T278076
Change-Id: Ifb4a75c8f81a7d727b149ed6354e8896ffeb4e0c
---
M pywikibot/config2.py
1 file changed, 34 insertions(+), 5 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/config2.py b/pywikibot/config2.py
index d55dbf5..f514ce1 100644
--- a/pywikibot/config2.py
+++ b/pywikibot/config2.py
@@ -45,6 +45,7 @@
 from textwrap import fill
 from typing import Optional, Union
 from warnings import warn
+from zipfile import is_zipfile, ZipFile

 from pywikibot.__metadata__ import __version__ as pwb_version
 from pywikibot.backports import Dict, List, removesuffix, Tuple
@@ -397,13 +398,41 @@
     family_files[family_name] = file_path


-def register_families_folder(folder_path):
-    """Register all family class files contained in a directory."""
+def register_families_folder(folder_path: str):
+    """Register all family class files contained in a directory.
+
+    @param folder_path: The path of a folder containing family files.
+        The families may also be inside a zip archive structure.
+    @raises NotADirectoryError: folder_path is not a directory
+    """
     suffix = '_family.py'
-    for file_name in os.listdir(folder_path):
+    if os.path.isdir(folder_path):
+        for file_name in os.listdir(folder_path):
+            if file_name.endswith(suffix):
+                family_name = removesuffix(file_name, suffix)
+                family_files[family_name] = os.path.join(folder_path,
+                                                         file_name)
+        return
+
+    # probably there is a zip file chain (T278076)
+    # find the parent zip folder
+    path = Path(folder_path)
+    if not is_zipfile(path):
+        for path in path.parents:
+            if is_zipfile(path):
+                break
+        else:
+            raise NotADirectoryError('20', 'Not a directory', folder_path)
+
+    # read the family files from zip folder
+    # assume that all files ending with suffix reside in family folder
+    zip_file = ZipFile(path)
+    for file_name in zip_file.namelist():
         if file_name.endswith(suffix):
-            family_name = removesuffix(file_name, suffix)
-            family_files[family_name] = os.path.join(folder_path, file_name)
+            file_path = Path(file_name)
+            family_name = removesuffix(file_path.name, suffix)
+            family_files[family_name] = os.path.join(folder_path,
+                                                     file_path.name)


 # Get the names of all known families, and initialize with empty dictionaries.

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/674317
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ifb4a75c8f81a7d727b149ed6354e8896ffeb4e0c
Gerrit-Change-Number: 674317
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: WolfgangFahl <[email protected]>
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to