jenkins-bot merged this change.

View Change

Approvals: Framawiki: Looks good to me, approved jenkins-bot: Verified
[IMPR] Simplify titletranslate.translate()

- assert statement ensures that page is given if site is None
- predefine hints as a tuple which could be iterated by the for statement.
The if could be omitted then.
- use partition() instead of if .. then .. else to get codes and newname
- use isdigit() instead of try .. except to calculate the codes and keep
the code flat.

Change-Id: I61a219ccbe66142151304c8477e9d808664ad31c
---
M pywikibot/titletranslate.py
1 file changed, 35 insertions(+), 45 deletions(-)

diff --git a/pywikibot/titletranslate.py b/pywikibot/titletranslate.py
index 893ae22..073393f 100644
--- a/pywikibot/titletranslate.py
+++ b/pywikibot/titletranslate.py
@@ -19,7 +19,7 @@


@deprecated_args(family=None)
-def translate(page=None, hints=None, auto=True, removebrackets=False,
+def translate(page=None, hints=(), auto=True, removebrackets=False,
site=None):
"""
Return a list of links to pages on other sites based on hints.
@@ -30,58 +30,48 @@
brackets and the text between them is removed from the page title.
If 'auto' is true, known year and date page titles are autotranslated
to all known target languages and inserted into the list.
-
"""
result = set()

assert page or site

- if site is None and page:
+ if site is None:
site = page.site

- if hints:
- for h in hints:
- if ':' not in h:
- # argument given as -hint:xy where xy is a language code
- codes = h
- newname = ''
- else:
- codes, newname = h.split(':', 1)
- if newname == '':
- # if given as -hint:xy or -hint:xy:, assume that there should
- # be a page in language xy with the same title as the page
- # we're currently working on ...
- if page is None:
- continue
- newname = page.title(withNamespace=False)
- # ... unless we do want brackets
- if removebrackets:
- newname = re.sub(re.compile(r"\W*?\(.*?\)\W*?",
- re.UNICODE), u" ", newname)
- try:
- number = int(codes)
- codes = site.family.languages_by_size[:number]
- except ValueError:
- if codes == 'all':
- codes = site.family.languages_by_size
- elif codes in site.family.language_groups:
- codes = site.family.language_groups[codes]
- else:
- codes = codes.split(',')
+ for h in hints:
+ # argument may be given as -hint:xy where xy is a language code
+ codes, _, newname = h.partition(':')
+ if not newname:
+ # if given as -hint:xy or -hint:xy:, assume that there should
+ # be a page in language xy with the same title as the page
+ # we're currently working on ...
+ if page is None:
+ continue
+ newname = page.title(withNamespace=False)
+ # ... unless we do want brackets
+ if removebrackets:
+ newname = re.sub(re.compile(r'\W*?\(.*?\)\W*?',
+ re.UNICODE), ' ', newname)
+ if codes.isdigit():
+ codes = site.family.languages_by_size[:int(codes)]
+ elif codes == 'all':
+ codes = site.family.languages_by_size
+ elif codes in site.family.language_groups:
+ codes = site.family.language_groups[codes]
+ else:
+ codes = codes.split(',')

- for newcode in codes:
-
- if newcode in site.languages():
- if newcode != site.code:
- ns = page.namespace() if page else 0
- x = pywikibot.Link(newname,
- site.getSite(code=newcode),
- defaultNamespace=ns)
- result.add(x)
- else:
- if config.verbose_output:
- pywikibot.output(u"Ignoring unknown language code %s"
- % newcode)
+ for newcode in codes:
+ if newcode in site.languages():
+ if newcode != site.code:
+ ns = page.namespace() if page else 0
+ x = pywikibot.Link(newname,
+ site.getSite(code=newcode),
+ defaultNamespace=ns)
+ result.add(x)
+ elif config.verbose_output:
+ pywikibot.output('Ignoring unknown language code {}'
+ .format(newcode))

# Autotranslate dates into all other languages, the rest will come from
# existing interwiki links.

To view, visit change 430886. To unsubscribe, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I61a219ccbe66142151304c8477e9d808664ad31c
Gerrit-Change-Number: 430886
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Framawiki <framawiki@tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444@gmail.com>
Gerrit-Reviewer: jenkins-bot <>