jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[maintenance] Add new addwikis script to add wikis to a family file

Change-Id: I8ce9dc7c374a281286c8b2e1890f570413e6a6e1
---
M docs/scripts/maintenance.rst
M docs/scripts_ref/scripts.maintenance.rst
M scripts/README.rst
A scripts/maintenance/addwikis.py
4 files changed, 120 insertions(+), 0 deletions(-)

diff --git a/docs/scripts/maintenance.rst b/docs/scripts/maintenance.rst
index 2eefdcd..da7df5d 100644
--- a/docs/scripts/maintenance.rst
+++ b/docs/scripts/maintenance.rst
@@ -2,6 +2,13 @@
Utility scripts
***************

+addwikis script
+===============
+
+.. automodule:: scripts.maintenance.addwikis
+ :no-members:
+ :noindex:
+
cache script
============

diff --git a/docs/scripts_ref/scripts.maintenance.rst b/docs/scripts_ref/scripts.maintenance.rst
index af4359d..b24603d 100644
--- a/docs/scripts_ref/scripts.maintenance.rst
+++ b/docs/scripts_ref/scripts.maintenance.rst
@@ -9,6 +9,11 @@
Maintenance submodules
======================

+scripts.maintenance.addwikis
+----------------------------
+
+.. automodule:: scripts.maintenance.addwikis
+
scripts.maintenance.cache
-------------------------

diff --git a/scripts/README.rst b/scripts/README.rst
index 162933a..3815f29 100644
--- a/scripts/README.rst
+++ b/scripts/README.rst
@@ -166,6 +166,8 @@
+------------------------+---------------------------------------------------------+
| maintenance | Framework helper scripts |
+========================+=========================================================+
+| addwikis.py | Script to add wikis to a family file. |
++------------------------+---------------------------------------------------------+
| cache.py | Script for showing and deleting API cache. |
+------------------------+---------------------------------------------------------+
| colors.py | Utility to show pywikibot colors. |
diff --git a/scripts/maintenance/addwikis.py b/scripts/maintenance/addwikis.py
new file mode 100644
index 0000000..0fa7b0d
--- /dev/null
+++ b/scripts/maintenance/addwikis.py
@@ -0,0 +1,106 @@
+#!/usr/bin/env python3
+"""Script that adds new wikis to the codes set in Wikimedia family files.
+
+Usage:
+
+ python pwb.py addwiki [-family:<family>] {<wiki>}
+
+
+.. versionadded:: 9.2
+"""
+#
+# (C) Pywikibot team, 2024
+#
+# Distributed under the terms of the MIT license.
+#
+from __future__ import annotations
+
+import re
+import sys
+from pathlib import Path
+
+import pywikibot
+from pywikibot.family import Family
+
+
+# supported families by this script
+families_list = [
+ 'wikibooks',
+ 'wikinews',
+ 'wikipedia',
+ 'wikiquote',
+ 'wikisource',
+ 'wikiversity',
+ 'wikivoyage',
+ 'wiktionary',
+]
+
+
+def update_family(family, wikis):
+ """Update codes set in family file."""
+ joined_wikis = "', '".join(wikis)
+ pywikibot.info(f"Adding '{joined_wikis}' to {family} family...\n")
+
+ original = Family.load(family).codes
+ new_codes = set()
+ for wiki in list(wikis):
+ if wiki in original:
+ pywikibot.warning(f'{wiki!r} is alread in Family.codes; ignoring.')
+ else:
+ new_codes.add(wiki)
+
+ if not new_codes:
+ pywikibot.info('No wikis to add.')
+ return
+
+ # combine new codes set
+ new = sorted(original | new_codes)
+ pywikibot.info("The lists don't match, the new list is:\n")
+ text = ' codes = {\n'
+ line = ' ' * 7
+ for code in new:
+ if len(line) + len(code) >= 76:
+ text += line + '\n'
+ line = ' ' * 7
+ line += f" '{code}',"
+ text += line + '\n'
+ text += ' }'
+ pywikibot.info(text)
+
+ # update codes
+ filepath = Path(f'pywikibot/families/{family}_family.py')
+ family_text = filepath.read_text(encoding='utf8')
+ family_text = re.sub(r'(?ms)^ {4}codes = \{.+?\}',
+ text, family_text, count=1)
+ filepath.write_text(family_text, encoding='utf8')
+
+
+def main(*args: str) -> None:
+ """Script entry point to handle args."""
+ if not args:
+ args = sys.argv[1:]
+ sys.argv = [sys.argv[0]]
+
+ family = 'wikipedia'
+ wikis = []
+ for arg in args:
+ if arg.startswith('-family'):
+ family = arg.split(':')[1]
+ elif arg == 'help':
+ pywikibot.show_help()
+ return
+ else:
+ wikis.append(arg)
+
+ if not wikis:
+ pywikibot.bot.suggest_help(
+ additional_text='No wiki is specified to be added.')
+ elif family not in families_list:
+ pywikibot.bot.suggest_help(
+ additional_text=f'Script cannot be used for {family} family.')
+ else:
+ update_family(family, wikis)
+
+
+if __name__ == '__main__':
+ main()

To view, visit change 1048367. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I8ce9dc7c374a281286c8b2e1890f570413e6a6e1
Gerrit-Change-Number: 1048367
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: D3r1ck01 <dalangi-ctr@wikimedia.org>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot