Xqt has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1287456?usp=email )
Change subject: maintenance: addwikis also updates WikimediaFamily.known_codes
......................................................................
maintenance: addwikis also updates WikimediaFamily.known_codes
Bug: T426319
Change-Id: Id37e9b2a43e025be4427751da5bcb5b90ffe4bb5
---
M scripts/maintenance/addwikis.py
1 file changed, 70 insertions(+), 20 deletions(-)
Approvals:
Xqt: Verified; Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/maintenance/addwikis.py b/scripts/maintenance/addwikis.py
index 10bd7a7..802c18f 100755
--- a/scripts/maintenance/addwikis.py
+++ b/scripts/maintenance/addwikis.py
@@ -29,7 +29,9 @@
instead of the new list.
.. version-changed:: 11.3
wikinews is no longer supported by this script because all wikinews
- sites are closed.
+ sites are closed. Also update
+ :attr:`family.WikimediaFamily.known_codes`.
+
"""
from __future__ import annotations
@@ -45,7 +47,7 @@
# supported families by this script
-families_list = [
+FAMILIES_LIST = [
'wikibooks',
'wikipedia',
'wikiquote',
@@ -56,12 +58,65 @@
]
-def update_family(family: str, wikis: set) -> None:
+def combine_codes(codes: set[str], /, *, attr='codes', style='{}') -> str:
+ """Combine codes and return a formatted Python assignment block.
+
+ .. version-added:: 11.3
+ """
+ new = sorted(codes)
+ opening, closing = style
+ text = f' {attr} = {opening}\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 += f' {closing}'
+ return text
+
+
+def update_known_codes(families: list[str]) -> set[str]:
+ """Collect all codes from all families and update Family.known_codes.
+
+ .. version-added:: 11.3
+ """
+ pywikibot.info()
+ family = Family.load(families[0])
+ if not isinstance(family, pywikibot.family.WikimediaFamily):
+ pywikibot.info(f'{family} is not a WikimediaFamily')
+ return
+
+ original: set[str] = set(family.known_codes)
+ new_codes: set[str] = set(family.codes)
+
+ for family_name in families[1:]:
+ new_codes.update(Family.load(family_name).codes)
+
+ new_codes.discard('mul')
+
+ if new_codes <= original:
+ pywikibot.info('No codes to add to known_codes list.')
+ return
+
+ pywikibot.info('Updating known_codes list:\n')
+ text = combine_codes(original | new_codes, attr='known_codes', style='[]')
+ filepath = Path('pywikibot/family.py')
+ old_family_text = filepath.read_text(encoding='utf8')
+ new_family_text = re.sub(r'(?ms)^ {4}known_codes = \[.+?\]',
+ text, old_family_text, count=1)
+ pywikibot.showDiff(old_family_text, new_family_text)
+ filepath.write_text(new_family_text, encoding='utf8')
+
+
+def update_family(family_name: str, wikis: set) -> None:
"""Update codes set in family file."""
joined_wikis = "', '".join(wikis)
- pywikibot.info(f"Adding '{joined_wikis}' to {family} family...\n")
+ pywikibot.info(f"Adding '{joined_wikis}' to {family_name} family...\n")
- original = Family.load(family).codes
+ family = Family.load(family_name)
+ original = family.codes
new_codes = set()
for wiki in list(wikis):
if wiki in original:
@@ -70,25 +125,18 @@
else:
new_codes.add(wiki)
+ # cleanup cache
+ family._families.clear()
+ module = type(family).__module__
+ del sys.modules[module]
+
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 updated 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 += ' }'
-
- # update codes
- filepath = Path(f'pywikibot/families/{family}_family.py')
+ text = combine_codes(original | new_codes)
+ filepath = Path(f'pywikibot/families/{module}.py')
old_family_text = filepath.read_text(encoding='utf8')
new_family_text = re.sub(r'(?ms)^ {4}codes = \{.+?\}',
text, old_family_text, count=1)
@@ -125,12 +173,14 @@
additional_text='No wiki is specified to be added.')
for family, codes in wikis.items():
- if family not in families_list:
+ if family not in FAMILIES_LIST:
pywikibot.bot.suggest_help(
additional_text=f'Script cannot be used for {family} family.')
else:
update_family(family, codes)
+ update_known_codes(FAMILIES_LIST)
+
if __name__ == '__main__':
main()
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1287456?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: Id37e9b2a43e025be4427751da5bcb5b90ffe4bb5
Gerrit-Change-Number: 1287456
Gerrit-PatchSet: 3
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]