Skip to content

Commit

Permalink
Merge "[IMPR] Add a new Site property codes"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Nov 29, 2024
2 parents db149a8 + 4f46379 commit d65d4a8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
24 changes: 19 additions & 5 deletions pywikibot/site/_basesite.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(self, code: str, fam=None, user=None) -> None:
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
Expand Down Expand Up @@ -231,13 +231,27 @@ def __hash__(self):
"""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):
Expand Down
4 changes: 2 additions & 2 deletions pywikibot/titletranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def translate(
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
link = pywikibot.Link(newname,
Expand All @@ -82,7 +82,7 @@ def translate(
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 != page.site.lang:
Expand Down
5 changes: 3 additions & 2 deletions tests/dry_api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ def version(self):
def protocol(self):
return 'http'

def languages(self):
return ['mock']
@property
def codes(self):
return {'mock'}

def user(self):
return self._user
Expand Down
12 changes: 6 additions & 6 deletions tests/site_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ def test_constructors(self):
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
Expand All @@ -118,7 +118,7 @@ def test_language_methods(self):
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):
Expand Down

0 comments on commit d65d4a8

Please sign in to comment.