#!/usr/bin/env python3"""Script that adds new wikis to the codes set in Wikimedia family files.Usage: python pwb.py addwikis [-family:<family>] {<wiki>}.. versionadded:: 9.2"""## (C) Pywikibot team, 2024## Distributed under the terms of the MIT license.#from__future__importannotationsimportreimportsysfrompathlibimportPathimportpywikibotfrompywikibot.familyimportFamily# supported families by this scriptfamilies_list=['wikibooks','wikinews','wikipedia','wikiquote','wikisource','wikiversity','wikivoyage','wiktionary',]
[docs]defupdate_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).codesnew_codes=set()forwikiinlist(wikis):ifwikiinoriginal:pywikibot.warning(f'{wiki!r} is alread in Family.codes; ignoring.')else:new_codes.add(wiki)ifnotnew_codes:pywikibot.info('No wikis to add.')return# combine new codes setnew=sorted(original|new_codes)pywikibot.info("The lists don't match, the new list is:\n")text=' codes = {\n'line=' '*7forcodeinnew:iflen(line)+len(code)>=76:text+=line+'\n'line=' '*7line+=f" '{code}',"text+=line+'\n'text+=' }'pywikibot.info(text)# update codesfilepath=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')
[docs]defmain(*args:str)->None:"""Script entry point to handle args."""ifnotargs:args=sys.argv[1:]sys.argv=[sys.argv[0]]family='wikipedia'wikis=[]forarginargs:ifarg.startswith('-family'):family=arg.split(':')[1]elifarg=='help':pywikibot.show_help()returnelse:wikis.append(arg)ifnotwikis:pywikibot.bot.suggest_help(additional_text='No wiki is specified to be added.')eliffamilynotinfamilies_list:pywikibot.bot.suggest_help(additional_text=f'Script cannot be used for {family} family.')else:update_family(family,wikis)