jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/633805 )
Change subject: [bugfix] accept only valid names in generate_family_file.py
......................................................................
[bugfix] accept only valid names in generate_family_file.py
- check name validity in generate_family_file.py
- rephrase error and warning messages in family.py
Bug: T265328
Bug: T265353
Change-Id: I60bc76888a1ae338eaedbe31b3af28718043b18e
---
M generate_family_file.py
M pywikibot/family.py
2 files changed, 29 insertions(+), 5 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/generate_family_file.py b/generate_family_file.py
index 3c4e1e4..770adb8 100755
--- a/generate_family_file.py
+++ b/generate_family_file.py
@@ -8,11 +8,19 @@
#
import codecs
import os
+import string
import sys
from os import environ, getenv
from urllib.parse import urlparse
+# see pywikibot.family.py
+# Legal characters for Family name and Family langs keys
+NAME_CHARACTERS = string.ascii_letters + string.digits
+# nds_nl code alias requires "_"n
+# dash must be the last char to be reused as regex in update_linktrails
+CODE_CHARACTERS = string.ascii_lowercase + string.digits + '_-'
+
class FamilyFileGenerator:
@@ -30,6 +38,11 @@
url = input('Please insert URL to wiki: ')
if name is None:
name = input('Please insert a short name (eg: freeciv): ')
+
+ assert all(x in NAME_CHARACTERS for x in name), \
+ 'Name of family {} must be ASCII letters and digits' \
+ '[a-zA-Z0-9]'.format(name)
+
self.dointerwiki = dointerwiki
self.base_url = url
self.name = name
@@ -91,6 +104,12 @@
if wiki['prefix'] in do_langs
or wiki['url'] == w.iwpath]
+ for wiki in self.langs:
+ assert all(x in CODE_CHARACTERS for x in wiki['prefix']), \
+ 'Family {} code {} must be ASCII lowercase ' \
+ 'letters and digits [a-z0-9] or underscore/dash [_-]' \
+ .format(self.name, wiki['prefix'])
+
def getapis(self):
"""Load other language pages."""
print('Loading wikis... ')
diff --git a/pywikibot/family.py b/pywikibot/family.py
index 8a6be39..3e46804 100644
--- a/pywikibot/family.py
+++ b/pywikibot/family.py
@@ -691,7 +691,8 @@
fam = config.family
assert all(x in NAME_CHARACTERS for x in fam), \
- 'Name of family %s must be ASCII characters and digits' % fam
+ 'Name of family {} must be ASCII letters and digits [a-zA-Z0-9]' \
+ .format(fam)
if fam in Family._families:
return Family._families[fam]
@@ -720,14 +721,18 @@
if cls.name != fam:
warnings.warn('Family name {} does not match family module name {}'
.format(cls.name, fam), FamilyMaintenanceWarning)
- # Family 'name' and the 'langs' codes must be ascii, and the
- # codes must be lower-case due to the Site loading algorithm.
+ # Family 'name' and the 'langs' codes must be ascii letters and digits,
+ # and codes must be lower-case due to the Site loading algorithm;
+ # codes can accept also underscore/dash.
if not all(x in NAME_CHARACTERS for x in cls.name):
- warnings.warn('Family name {} contains non-ascii characters'
+ warnings.warn('Name of family {} must be ASCII letters '
+ 'and digits [a-zA-Z0-9]'
.format(cls.name), FamilyMaintenanceWarning)
for code in cls.langs.keys():
if not all(x in CODE_CHARACTERS for x in code):
- warnings.warn('Family {} code {} contains non-ascii characters'
+ warnings.warn('Family {} code {} must be ASCII lowercase '
+ 'letters and digits [a-z0-9] or '
+ 'underscore/dash [_-]'
.format(cls.name, code),
FamilyMaintenanceWarning)
Family._families[fam] = cls
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/633805
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I60bc76888a1ae338eaedbe31b3af28718043b18e
Gerrit-Change-Number: 633805
Gerrit-PatchSet: 2
Gerrit-Owner: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/633572 )
Change subject: [bugfix] do not encode str to bytes in imagetransfer.py
......................................................................
[bugfix] do not encode str to bytes in imagetransfer.py
Bug: T265257
Change-Id: I284ed2abf0334e1e415230cfb0b861dd7baa2dee
---
M scripts/imagetransfer.py
1 file changed, 1 insertion(+), 1 deletion(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/imagetransfer.py b/scripts/imagetransfer.py
index d251906..6e539d6 100755
--- a/scripts/imagetransfer.py
+++ b/scripts/imagetransfer.py
@@ -148,7 +148,7 @@
@return: the filename which was used to upload the image
"""
sourceSite = sourceImagePage.site
- url = sourceImagePage.get_file_url().encode('utf-8')
+ url = sourceImagePage.get_file_url()
pywikibot.output('URL should be: ' + url)
# localize the text that should be printed on image description page
try:
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/633572
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I284ed2abf0334e1e415230cfb0b861dd7baa2dee
Gerrit-Change-Number: 633572
Gerrit-PatchSet: 1
Gerrit-Owner: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/633292 )
Change subject: [IMPR] Add typing hints to i18n.py and plural.py
......................................................................
[IMPR] Add typing hints to i18n.py and plural.py
plural.py:
- add typing hints
- add a new plural_rule function which returns the PluralRule dict for
a given lang or the default dict.
i18n.py:
- add typing hints
- rename code parameter of :extract_plural to lang
- use plural_rule function
- flatten input function
Change-Id: I5ba39767dbc13f69df11b72189f126661d370410
---
M pywikibot/i18n.py
M pywikibot/plural.py
2 files changed, 41 insertions(+), 36 deletions(-)
Approvals:
Hazard-SJ: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py
index 287a86c..b9748f0 100644
--- a/pywikibot/i18n.py
+++ b/pywikibot/i18n.py
@@ -30,6 +30,7 @@
from collections import defaultdict
from contextlib import suppress
from textwrap import fill
+from typing import List, Optional
from warnings import warn
import pywikibot
@@ -37,7 +38,7 @@
from pywikibot import __url__
from pywikibot import config2 as config
from pywikibot.exceptions import Error
-from pywikibot.plural import plural_rules
+from pywikibot.plural import plural_rule
from pywikibot.tools import (
deprecated, deprecated_args, issue_deprecation_warning, PYTHON_VERSION)
@@ -273,6 +274,7 @@
'zh-min-nan': 'zh-min-nan',
'zh-tw': 'zh-classical',
'zh-yue': 'cdo'})
+
_GROUP_NAME_TO_FALLBACKS = {
'': [],
'aa': ['am'],
@@ -358,7 +360,7 @@
'cdo', 'zh', 'zh-hans', 'zh-tw', 'zh-cn', 'zh-classical', 'lzh']}
-def set_messages_package(package_name):
+def set_messages_package(package_name: str):
"""Set the package name where i18n messages are located."""
global _messages_package_name
global _messages_available
@@ -366,15 +368,13 @@
_messages_available = None
-def messages_available():
+def messages_available() -> bool:
"""
Return False if there are no i18n messages available.
To determine if messages are available, it looks for the package name
set using L{set_messages_package} for a message bundle called 'pywikibot'
containing messages.
-
- @rtype: bool
"""
global _messages_available
if _messages_available is not None:
@@ -393,7 +393,7 @@
return True
-def _altlang(lang):
+def _altlang(lang: str) -> List[str]:
"""Define fallback languages for particular languages.
If no translation is available to a specified language, translate() will
@@ -406,9 +406,7 @@
This code is used by other translating methods below.
@param lang: The language code
- @type lang: str
@return: language codes
- @rtype: list of str
"""
return _GROUP_NAME_TO_FALLBACKS[_LANG_TO_GROUP_NAME[lang]]
@@ -425,7 +423,7 @@
@cache
-def _get_translation(lang, twtitle):
+def _get_translation(lang: str, twtitle: str) -> Optional[str]:
"""
Return message of certain twtitle if exists.
@@ -443,15 +441,13 @@
return transdict.get(twtitle)
-def _extract_plural(code, message, parameters):
+def _extract_plural(lang: str, message: str, parameters: Mapping) -> str:
"""Check for the plural variants in message and replace them.
@param message: the message to be replaced
- @type message: str
@param parameters: plural parameters passed from other methods
@type parameters: Mapping of str to int
@return: The message with the plural instances replaced
- @rtype: str
"""
def static_plural_value(n):
return rule['plural']
@@ -497,11 +493,9 @@
return plural_entries[index]
assert isinstance(parameters, Mapping), \
- 'parameters is not Mapping but {0}'.format(type(parameters))
- try:
- rule = plural_rules[code]
- except KeyError:
- rule = plural_rules['_default']
+ 'parameters is not Mapping but {}'.format(type(parameters))
+
+ rule = plural_rule(lang)
plural_value = rule['plural']
if not callable(plural_value):
assert rule['nplurals'] == 1
@@ -635,9 +629,11 @@
@deprecated_args(code='source')
-def twtranslate(
- source, twtitle, parameters=None, fallback=True, only_plural=False
-):
+def twtranslate(source,
+ twtitle: str,
+ parameters: Optional[Mapping] = None,
+ fallback: bool = True,
+ only_plural: bool = False) -> Optional[str]:
r"""
Translate a message using JSON files in messages_package_name.
@@ -698,7 +694,6 @@
plural instances. If this is False it will apply the parameters also
to the resulting string. If this is True the placeholders must be
manually applied afterwards.
- @type only_plural: bool
@raise IndexError: If the language supports and requires more plurals than
defined for the given translation template.
"""
@@ -775,7 +770,8 @@
@deprecated('twtranslate', since='20151009', future_warning=True)
@deprecated_args(code='source')
-def twntranslate(source, twtitle, parameters=None):
+def twntranslate(source, twtitle: str,
+ parameters: Optional[Mapping] = None) -> Optional[str]:
"""DEPRECATED: Get translated string for the key."""
if parameters is not None:
parameters = _PluralMappingAlias(parameters)
@@ -783,7 +779,7 @@
@deprecated_args(code='source')
-def twhas_key(source, twtitle):
+def twhas_key(source, twtitle: str) -> bool:
"""
Check if a message has a translation in the specified language code.
@@ -803,7 +799,7 @@
return transdict is not None
-def twget_keys(twtitle):
+def twget_keys(twtitle: str) -> List[str]:
"""
Return all language codes for a special message.
@@ -827,7 +823,10 @@
if lang != 'qqq' and _get_translation(lang, twtitle)]
-def input(twtitle, parameters=None, password=False, fallback_prompt=None):
+def input(twtitle: str,
+ parameters: Optional[Mapping] = None,
+ password: bool = False,
+ fallback_prompt: Optional[str] = None) -> str:
"""
Ask the user a question, return the user's answer.
@@ -838,17 +837,14 @@
@param parameters: The values which will be applied to the translated text
@param password: Hides the user's input (for password entry)
@param fallback_prompt: The English prompt if i18n is not available.
- @rtype: str
"""
- if not messages_available():
- if not fallback_prompt:
- raise TranslationError(
- 'Unable to load messages package %s for bundle %s'
- % (_messages_package_name, twtitle))
- else:
- prompt = fallback_prompt
- else:
+ if messages_available():
code = config.userinterface_lang
-
prompt = twtranslate(code, twtitle, parameters)
+ elif fallback_prompt:
+ prompt = fallback_prompt
+ else:
+ raise TranslationError(
+ 'Unable to load messages package {} for bundle {}'
+ .format(_messages_package_name, twtitle))
return pywikibot.input(prompt, password)
diff --git a/pywikibot/plural.py b/pywikibot/plural.py
index 70bc201..5dea038 100644
--- a/pywikibot/plural.py
+++ b/pywikibot/plural.py
@@ -5,6 +5,10 @@
#
# Distributed under the terms of the MIT license.
#
+from typing import Callable, Dict, Union
+
+PluralRule = Dict[str, Union[int, Callable]]
+
plural_rules = {
'_default': {'nplurals': 2, 'plural': lambda n: (n != 1)},
'ar': {'nplurals': 6, 'plural': lambda n:
@@ -77,7 +81,7 @@
1 if (n % 100 == 2) else
2 if n % 100 in (3, 4) else
3},
-}
+} # type: Dict[str, PluralRule]
plural_rules.update(
dict.fromkeys(
@@ -99,3 +103,8 @@
0 if n % 10 == 1 and n % 100 != 11 else
1 if (2 <= (n % 10) <= 4) and (n % 100 < 10 or n % 100 >= 20)
else 2}))
+
+
+def plural_rule(lang: str) -> PluralRule:
+ """Return the plural rule for a given lang."""
+ return plural_rules.get(lang, plural_rules['_default'])
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/633292
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I5ba39767dbc13f69df11b72189f126661d370410
Gerrit-Change-Number: 633292
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Hazard-SJ <hazardsjwiki(a)gmail.com>
Gerrit-Reviewer: Isaacandy <isaac(a)iznd.xyz>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Siebrand <siebrand(a)kitano.nl>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged