jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1150058?usp=email )

Change subject: IMPR: use pathlib methods in generate_user_file
......................................................................

IMPR: use pathlib methods in generate_user_file

Bug: T395187
Change-Id: I887581593a257ae6b85f467b38df1315e7056ba6
---
M pywikibot/scripts/generate_user_files.py
1 file changed, 29 insertions(+), 28 deletions(-)

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




diff --git a/pywikibot/scripts/generate_user_files.py 
b/pywikibot/scripts/generate_user_files.py
index 022471a..bd4d6fb 100755
--- a/pywikibot/scripts/generate_user_files.py
+++ b/pywikibot/scripts/generate_user_files.py
@@ -8,13 +8,12 @@
    Also EXTERNAL EDITOR SETTINGS section can be copied.
 """
 #
-# (C) Pywikibot team, 2010-2024
+# (C) Pywikibot team, 2010-2025
 #
 # Distributed under the terms of the MIT license.
 #
 from __future__ import annotations

-import codecs
 import os
 import re
 import sys
@@ -243,8 +242,7 @@
     data = []

     config_path = Path(__file__).resolve().parents[1].joinpath('config.py')
-    with codecs.open(config_path, 'r', 'utf-8') as config_f:
-        config_file = config_f.read()
+    config_file = config_path.read_text(encoding='utf-8')

     result = re.findall(
         '^(?P<section># #{5,} (?P<head>[A-Z][A-Z_ ]+[A-Z]) #{5,}\r?\n'
@@ -356,8 +354,8 @@

     Create a user-password.py if necessary.
     """
-    _fnc = os.path.join(base_dir, USER_BASENAME)
-    _fncpass = os.path.join(base_dir, PASS_BASENAME)
+    f_user = Path(base_dir, USER_BASENAME)
+    f_pass = Path(base_dir, PASS_BASENAME)

     userlist = []
     if force and not config.verbose_output:
@@ -376,7 +374,7 @@
     # BotPassword (username, BotPassword name, BotPassword pass)
     msg: str | None = fill(
         f'See {__url__}/BotPasswords to know how to get codes. '
-        f'Please note that plain text in {_fncpass} and anyone with read'
+        f'Please note that plain text in {f_pass} and anyone with read'
         ' access to that directory will be able read the file.'
     )
     botpasswords = []
@@ -421,26 +419,32 @@

     try:
         # Finally save user-config.py
-        with codecs.open(_fnc, 'w', 'utf-8') as f:
-            f.write(config_content.format(
+        f_user.write_text(
+            config_content.format(
                 main_family=main_family,
                 main_code=main_code,
                 usernames=usernames,
                 config_text=config_text,
                 botpasswords='password_file = ' + (f'"{PASS_BASENAME}"'
-                                                   if botpasswords
-                                                   else 'None')))
-        pywikibot.info(f"'{_fnc}' written.")
+                                                   if botpasswords else 'None')
+            ),
+            encoding='utf-8'
+        )
+        pywikibot.info(f"'{f_user}' written.")
     except BaseException:
-        if os.path.exists(_fnc):
-            os.remove(_fnc)
+        f_user.unlink(missing_ok=True)
         raise

-    save_botpasswords(botpasswords, _fncpass)
+    save_botpasswords(botpasswords, f_pass)
 

-def save_botpasswords(botpasswords, _fncpass):
-    """Write botpasswords to file."""
+def save_botpasswords(botpasswords: str, path: Path):
+    """Write botpasswords to file.
+
+    :param botpasswords: botpasswords for password file
+    :param path: file path for password file
+    :raises OSError: OSError during writing the file
+    """
     if botpasswords:
         # Save user-password.py if necessary
         # user-config.py is already created at this point
@@ -448,19 +452,16 @@
         from pywikibot.tools import file_mode_checker
         try:
             # First create an empty file with good permissions, before writing
-            # in it
-            with codecs.open(_fncpass, 'w', 'utf-8') as f:
-                f.write('')
-                file_mode_checker(_fncpass,
+            # the content
+            for content in ('',
+                            PASSFILE_CONFIG.format(botpasswords=botpasswords)):
+                path.write_text(content, encoding='utf-8')
+                file_mode_checker(path,
                                   mode=config.private_files_permission,
-                                  quiet=True)
-            with codecs.open(_fncpass, 'w', 'utf-8') as f:
-                f.write(PASSFILE_CONFIG.format(botpasswords=botpasswords))
-                file_mode_checker(_fncpass,
-                                  mode=config.private_files_permission)
-                pywikibot.info(f"'{_fncpass}' written.")
+                                  quiet=not content)
+            pywikibot.info(f"'{path}' written.")
         except OSError:
-            os.remove(_fncpass)
+            path.unlink(missing_ok=True)
             raise



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

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I887581593a257ae6b85f467b38df1315e7056ba6
Gerrit-Change-Number: 1150058
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to