Source code for scripts.maintenance.make_i18n_dict
#!/usr/bin/env python3"""Generate an i18n file from a given script.Run IDLE at topmost level:>>> import pwb>>> from scripts.maintenance.make_i18n_dict import i18nBot>>> bot = i18nBot('<scriptname>', '<msg dict>')>>> bot.run().. hint:: Import from ``pywikibot-scripts`` if scripts are installed as a site-package.If you have more than one message dictionary, give all these names to the bot:>>> bot = i18nBot('<scriptname>', '<msg dict1>', '<msg dict2>', '<msg dict3>')If you want to rename the message index use keyword arguments. This may bemixed with preleading positional arguments:>>> bot = i18nBot('<scriptname>', '<msg dict1>', the_other_msg='<msg dict2>')If you have the messages as instance constants you may call the bot as follows:>>> bot = i18nBot('<scriptname>.<class name>', '<msg dict1>', '<msg dict2>')It's also possible to make json files too by using to_json method afterinstantiating the bot. It also calls ``bot.run()`` to create the dictionaries:>>> bot.to_json()"""## (C) Pywikibot team, 2013-2024## Distributed under the terms of the MIT license.#from__future__importannotationsimportcodecsimportjsonimportosfromimportlibimportimport_modulefrompywikibotimportconfig
[docs]classi18nBot:# noqa: N801"""I18n bot."""def__init__(self,script,*args,**kwargs):"""Initializer."""modules=script.split('.')self.scriptname=modules[0]self.script=import_module('scripts.'+self.scriptname)forminmodules:self.script=getattr(self.script,m)self.messages={}# setup the message dictformsginargs:ifhasattr(self.script,msg):self.messages[msg]=msgelse:print(f'message {msg} not found')fornew,oldinkwargs.items():self.messages[old]=new.replace('_','-')self.dict={}
[docs]defprint_all(self):"""Pretty print the dict as a file content to screen."""ifnotself.dict:print('No messages found, read them first.\n''Use "run" or "to_json" methods')returnkeys=list(self.dict.keys())keys.remove('qqq')keys.sort()keys.insert(0,'qqq')if'en'inkeys:keys.remove('en')keys.insert(0,'en')print('msg = {')forcodeinkeys:print(f" '{code}': {{")formsginsorted(self.messages.values()):label=f'{self.scriptname}-{msg}'iflabelinself.dict[code]:print(f" '{label}': '{self.dict[code][label]}',")print(' },')print('};')
[docs]defread(self,oldmsg,newmsg=None):"""Read a single message from source script."""msg=getattr(self.script,oldmsg)keys=list(msg.keys())keys.append('qqq')ifnewmsgisNone:newmsg=oldmsgforcodeinkeys:label=f'{self.scriptname}-{newmsg}'ifcode=='qqq':ifcodenotinself.dict:self.dict[code]={}self.dict[code][label]=(f'Edit summary for message {newmsg} 'f'of {self.scriptname} report')elifcode!='commons':ifcodenotinself.dict:self.dict[code]={}self.dict[code][label]=msg[code]if'en'notinkeys:print('WARNING: "en" key missing for message '+newmsg)
[docs]defrun(self,quiet=False):"""Run the bot, read the messages from source and print the dict. :param quiet: print the result if False :type quiet: bool """foriteminself.messages.items():self.read(*item)ifnotquiet:self.print_all()
[docs]defto_json(self,quiet=True):"""Run the bot and create json files. :param quiet: Print the result if False :type quiet: bool """indent=4ifnotself.dict:self.run(quiet)json_dir=os.path.join(config.base_dir,'scripts/i18n',self.scriptname)ifnotos.path.exists(json_dir):os.makedirs(json_dir)forlanginself.dict:file_name=os.path.join(json_dir,f'{lang}.json')ifos.path.isfile(file_name):withcodecs.open(file_name,'r','utf-8')asjson_file:new_dict=json.load(json_file)else:new_dict={}new_dict['@metadata']=new_dict.get('@metadata',{'authors':[]})withcodecs.open(file_name,'w','utf-8')asjson_file:new_dict.update(self.dict[lang])s=json.dumps(new_dict,ensure_ascii=False,sort_keys=True,indent=indent,separators=(',',': '))s=s.replace(' '*indent,'\t')json_file.write(s)