jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1094477?usp=email )
Change subject: [IMPR] Add a new Site property codes
......................................................................
[IMPR] Add a new Site property codes
- add Site.codes property to give a list of site codes
- deprecate Site.languages() which returns a list of site codes
- update languages() usages
Bug: T380606
Change-Id: I2c4ceb0b9fe98f58972fc79b4d2076be20f24d36
---
M pywikibot/site/_basesite.py
M pywikibot/titletranslate.py
M tests/dry_api_tests.py
M tests/site_tests.py
4 files changed, 30 insertions(+), 15 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/site/_basesite.py b/pywikibot/site/_basesite.py
index fb30296..86ee9f8 100644
--- a/pywikibot/site/_basesite.py
+++ b/pywikibot/site/_basesite.py
@@ -74,7 +74,7 @@
self.obsolete = True
pywikibot.log(f'Site {self} instantiated and marked "obsolete"'
' to prevent access')
- elif self.__code not in self.languages():
+ elif self.__code not in self.codes:
if self.__family.name in self.__family.langs \
and len(self.__family.langs) == 1:
self.__code = self.__family.name
@@ -231,13 +231,27 @@
"""Return hash value of instance."""
return hash(repr(self))
- def languages(self):
- """Return list of all valid language codes for this site's Family."""
- return list(self.family.langs.keys())
+ @deprecated('codes', since='9.6')
+ def languages(self) -> list[str]:
+ """Return list of all valid site codes for this site's Family.
+
+ .. deprecated:: 9.6
+ Use :meth:`codes` instead.
+ """
+ return sorted(self.codes)
+
+ @property
+ def codes(self) -> set[str]:
+ """Return set of all valid site codes for this site's Family.
+
+ .. versionadded:: 9.6
+ .. seealso:: :attr:`family.Family.codes`
+ """
+ return set(self.family.langs.keys())
def validLanguageLinks(self): # noqa: N802
"""Return list of language codes to be used in interwiki links."""
- return [lang for lang in self.languages()
+ return [lang for lang in sorted(self.codes)
if self.namespaces.lookup_normalized_name(lang) is None]
def _interwiki_urls(self, only_article_suffixes: bool = False):
diff --git a/pywikibot/titletranslate.py b/pywikibot/titletranslate.py
index e349c74..7214304 100644
--- a/pywikibot/titletranslate.py
+++ b/pywikibot/titletranslate.py
@@ -51,7 +51,7 @@
codes = site.family.language_groups.get(codes, codes.split(','))
for newcode in codes:
- if newcode in site.languages():
+ if newcode in site.codes:
if newcode != site.code:
ns = page.namespace() if page else 0
x = pywikibot.Link(newname,
@@ -71,7 +71,7 @@
pywikibot.info(f'TitleTranslate: {page.title()} was recognized as '
f'{dict_name} with value {value}')
for entry_lang, entry in date.formats[dict_name].items():
- if entry_lang not in site.languages():
+ if entry_lang not in site.codes:
continue
if entry_lang != sitelang:
newname = entry(value)
diff --git a/tests/dry_api_tests.py b/tests/dry_api_tests.py
index a49c66c..7674625 100755
--- a/tests/dry_api_tests.py
+++ b/tests/dry_api_tests.py
@@ -169,8 +169,9 @@
def protocol(self):
return 'http'
- def languages(self):
- return ['mock']
+ @property
+ def codes(self):
+ return {'mock'}
def user(self):
return self._user
diff --git a/tests/site_tests.py b/tests/site_tests.py
index 7a0505b..9d133ff 100755
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -104,12 +104,12 @@
pywikibot.site.APISite.fromDBName(dbname, site),
pywikibot.Site(sitename))
- def test_language_methods(self):
- """Test cases for languages() and related methods."""
+ def test_codes_property(self):
+ """Test cases for codes property and related methods."""
mysite = self.get_site()
- langs = mysite.languages()
- self.assertIsInstance(langs, list)
- self.assertIn(mysite.code, langs)
+ codes = mysite.codes
+ self.assertIsInstance(codes, set)
+ self.assertIn(mysite.code, codes)
self.assertIsInstance(mysite.obsolete, bool)
ipf = mysite.interwiki_putfirst()
if ipf: # no languages use this anymore, keep it for foreign families
@@ -118,7 +118,7 @@
self.assertIsNone(ipf)
for item in mysite.validLanguageLinks():
- self.assertIn(item, langs)
+ self.assertIn(item, codes)
self.assertIsNone(self.site.namespaces.lookup_name(item))
def test_namespace_methods(self):
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1094477?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I2c4ceb0b9fe98f58972fc79b4d2076be20f24d36
Gerrit-Change-Number: 1094477
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1093597?usp=email )
Change subject: [IMPR] return a boolean with item_is_in_list and item_has_label
......................................................................
[IMPR] return a boolean with item_is_in_list and item_has_label
Simplify the code: both functions are used with their boolean result,
it is not necessary to return the item found.
Change-Id: Ie7e38f8fa041e91b8cca7c7dec813b1236beb2ae
---
M scripts/create_isbn_edition.py
1 file changed, 9 insertions(+), 9 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/create_isbn_edition.py b/scripts/create_isbn_edition.py
index d803761..79d5d85 100755
--- a/scripts/create_isbn_edition.py
+++ b/scripts/create_isbn_edition.py
@@ -696,39 +696,39 @@
return main_languages
-def item_is_in_list(statement_list: list, itemlist: list[str]) -> str:
+def item_is_in_list(statement_list: list, itemlist: list[str]) -> bool:
"""Verify if statement list contains at least one item from the itemlist.
param statement_list: Statement list
param itemlist: List of values (string)
- return: Matching or empty string
+ return: Whether the item matches
"""
for seq in statement_list:
with suppress(AttributeError): # Ignore NoneType error
isinlist = seq.getTarget().getID()
if isinlist in itemlist:
- return isinlist
- return ''
+ return True
+ return False
-def item_has_label(item, label: str) -> str:
+def item_has_label(item, label: str) -> bool:
"""Verify if the item has a label.
:param item: Item
:param label: Item label
- :return: Matching string
+ :return: Whether the item has a label
"""
label = unidecode(label).casefold()
for lang in item.labels:
if unidecode(item.labels[lang]).casefold() == label:
- return item.labels[lang]
+ return True
for lang in item.aliases:
for seq in item.aliases[lang]:
if unidecode(seq).casefold() == label:
- return seq
+ return True
- return '' # Must return "False" when no label
+ return False
def is_in_value_list(statement_list: list, valuelist: list[str]) -> bool:
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1093597?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ie7e38f8fa041e91b8cca7c7dec813b1236beb2ae
Gerrit-Change-Number: 1093597
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Geertivp <geertivp(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1093371?usp=email )
Change subject: [IMPR] use search_entities method in get_item_list function
......................................................................
[IMPR] use search_entities method in get_item_list function
Change-Id: I7d033572a0595b73bd8f0f5892a939a2c13e46e9
---
M scripts/create_isbn_edition.py
1 file changed, 2 insertions(+), 22 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/create_isbn_edition.py b/scripts/create_isbn_edition.py
index d803761..c566fbc 100755
--- a/scripts/create_isbn_edition.py
+++ b/scripts/create_isbn_edition.py
@@ -781,32 +781,12 @@
:param instance_id: Instance ID
:return: Set of items
"""
- pywikibot.debug(f'Search label: {item_name.encode("utf-8")}')
- # TODO: try to use search_entities instead?
- params = {
- 'action': 'wbsearchentities',
- 'format': 'json',
- 'type': 'item',
- # All languages are searched, but labels are in native language
- 'strictlanguage': False,
- 'language': mainlang,
- 'uselang': mainlang, # (primary) Search language
- 'search': item_name, # Get item list from label
- 'limit': 20, # Should be reasonable value
- }
- request = api.Request(site=repo, parameters=params)
- result = request.submit()
- pywikibot.debug(result)
-
- if 'search' not in result:
- return set()
-
# Ignore accents and case
item_name_canon = unidecode(item_name).casefold()
item_list = set()
- # Loop though items
- for res in result['search']:
+ # Loop though items, total should be reasonable value
+ for res in repo.search_entities(item_name, mainlang, total=20):
item = get_item_page(res['id'])
# Matching instance
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1093371?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I7d033572a0595b73bd8f0f5892a939a2c13e46e9
Gerrit-Change-Number: 1093371
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Geertivp <geertivp(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1092834?usp=email )
Change subject: [IMPR] Show a warning if ignore_extension was set and the extension is invalid.
......................................................................
[IMPR] Show a warning if ignore_extension was set and the extension is invalid.
Change-Id: I23a58ed419faee25109b243ecc30f2b570dbe8c4
---
M pywikibot/page/_filepage.py
1 file changed, 14 insertions(+), 9 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/page/_filepage.py b/pywikibot/page/_filepage.py
index 703d178..3e3fd88 100644
--- a/pywikibot/page/_filepage.py
+++ b/pywikibot/page/_filepage.py
@@ -43,9 +43,15 @@
"""Initializer.
.. versionchanged:: 8.4
- check for valid extensions.
+ Check for valid extensions.
.. versionchanged:: 9.3
- *ignore_extension* parameter was added
+ Added the optional *ignore_extension* parameter.
+ .. versionchanged:: 9.6
+ Show a warning if *ignore_extension* was set and the
+ extension is invalid.
+ .. seealso::
+ :meth:`Site.file_extensions
+ <pywikibot.site._apisite.APISite.file_extensions>`
:param source: the source of the page
:type source: pywikibot.page.BaseLink (or subclass),
@@ -62,16 +68,15 @@
if self.namespace() != 6:
raise ValueError(f"'{self.title()}' is not in the file namespace!")
- if ignore_extension:
- return
-
title = self.title(with_ns=False, with_section=False)
_, sep, extension = title.rpartition('.')
if not sep or extension.lower() not in self.site.file_extensions:
- raise ValueError(
- f'{title!r} does not have a valid extension '
- f'({", ".join(self.site.file_extensions)}).'
- )
+ msg = (f'{title!r} does not have a valid extension\n'
+ f'({", ".join(self.site.file_extensions)}).')
+ if not ignore_extension:
+ raise ValueError(msg)
+
+ pywikibot.warning(msg)
def _load_file_revisions(self, imageinfo) -> None:
"""Save a file revision of FilePage (a FileInfo object) in local cache.
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1092834?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I23a58ed419faee25109b243ecc30f2b570dbe8c4
Gerrit-Change-Number: 1092834
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot