jenkins-bot has submitted this change and it was merged.
Change subject: [IMPR] Rename the message index by using keyword arguments. ......................................................................
[IMPR] Rename the message index by using keyword arguments.
- bugfix for generic 'qqq' edit summary message - Add doc strings to methods - add script to flake8-docstrings-mandatory
Change-Id: I2becaee8126f5413bed5025ddfd41c6ac25558cd --- M scripts/maintenance/make_i18n_dict.py M tox.ini 2 files changed, 31 insertions(+), 18 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/maintenance/make_i18n_dict.py b/scripts/maintenance/make_i18n_dict.py index bb6155f..de37f2c 100755 --- a/scripts/maintenance/make_i18n_dict.py +++ b/scripts/maintenance/make_i18n_dict.py @@ -14,17 +14,20 @@ 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 be +mixed with preleading positonal argumens: +>>> 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 instance>', '<msg dict1>', '<msg dict2>')
-It's also possible to make json files too by using to_json method: ->>> from scripts.maintenance.make_i18n_dict import i18nBot ->>> bot = i18nBot('disambredir', 'msg') +It's also possible to make json files too by using to_json method after +instantiating the bot. It also calls bot.run() to create the dictionaries.
bot.to_json()
""" # -# (C) xqt, 2013-2014 -# (C) Pywikibot team, 2013-2014 +# (C) xqt, 2013-2015 +# (C) Pywikibot team, 2013-2015 # # Distributed under the terms of the MIT license. # @@ -44,20 +47,24 @@
"""I18n bot."""
- def __init__(self, script, *args): + def __init__(self, script, *args, **kwargs): + """Constructor.""" modules = script.split('.') self.scriptname = modules[0] self.script = __import__('scripts.' + self.scriptname) for m in modules: self.script = getattr(self.script, m) - self.messages = list() + self.messages = {} + # setup the message dict for msg in args: if hasattr(self.script, msg): - self.messages.append(msg) - self.messages.sort() - self.dict = dict() + self.messages[msg] = msg + for new, old in kwargs.items(): + self.messages[old] = new.replace('_', '-') + self.dict = {}
def print_all(self): + """Pretty print the dict as a file content to screen.""" keys = list(self.dict.keys()) keys.remove('qqq') keys.sort() @@ -70,7 +77,7 @@ print("msg = {") for code in keys: print(" '%s': {" % code) - for msg in self.messages: + for msg in sorted(self.messages.values()): label = "%s-%s" % (self.scriptname, msg) if label in self.dict[code]: print(" '%s': u'%s'," % (label, @@ -78,31 +85,36 @@ print(" },") print("};")
- def read(self, item): - msg = getattr(self.script, item) + def read(self, oldmsg, newmsg=None): + """Read a single message from source script.""" + msg = getattr(self.script, oldmsg) keys = list(msg.keys()) keys.append('qqq') + if newmsg is None: + newmsg = oldmsg for code in keys: - label = "%s-%s" % (self.scriptname, item) + label = "%s-%s" % (self.scriptname, newmsg) if code == 'qqq': if code not in self.dict: self.dict[code] = {} self.dict[code][label] = ( u'Edit summary for message %s of %s report' - % (self.scriptname, item)) + % (newmsg, self.scriptname)) elif code != 'commons': if code not in self.dict: self.dict[code] = {} self.dict[code][label] = msg[code] if 'en' not in keys: - print('WARNING: "en" key missing for message %s' % item) + print('WARNING: "en" key missing for message %s' % newmsg)
def run(self): - for msg in self.messages: - self.read(msg) + """Run the bot, read the messages from source and print the dict.""" + for item in self.messages.items(): + self.read(*item) self.print_all()
def to_json(self): + """Run the bot and create json files.""" if not self.dict: self.run() json_dir = os.path.join( diff --git a/tox.ini b/tox.ini index 6a052c3..c641bd2 100644 --- a/tox.ini +++ b/tox.ini @@ -102,6 +102,7 @@ scripts/listpages.py \ scripts/login.py \ scripts/maintenance/__init__.py \ + scripts/maintenance/make_i18n_dict.py \ scripts/misspelling.py \ scripts/newitem.py \ scripts/noreferences.py \