jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] Enable short creation of a site if family name is equal to site code

Remove assert statement in Family.load because it is tested lated and
a meaningful warning is printed in such case.

Change-Id: Ie8d36c367715ca45c5bf3572b8f88160f5249622
---
M pywikibot/__init__.py
M pywikibot/data/sparql.py
M pywikibot/family.py
M pywikibot/page/_wikibase.py
M pywikibot/site/_apisite.py
M pywikibot/specialbots/_upload.py
M scripts/checkimages.py
M scripts/data_ingestion.py
M scripts/harvest_template.py
M scripts/illustrate_wikidata.py
M scripts/maintenance/sorting_order.py
M tests/i18n_tests.py
M tests/page_tests.py
M tests/site_tests.py
M tests/wikibase_edit_tests.py
M tests/wikibase_tests.py
16 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index b94b610..aead197 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -1113,7 +1113,7 @@

Override default family::

- site = pywikibot.Site(family='wikisource')
+ site = pywikibot.Site(fam='wikisource')

Setting a specific site::

@@ -1140,6 +1140,10 @@
**Never create a site object via interface class directly.**
Always use this factory method.

+ .. versionchanged:: 7.3
+ Short creation if site code is equal to family name like
+ `Site('commons')`, `Site('meta')` or `Site('wikidata')`.
+
:param code: language code (override config.mylang)
code may also be a sitename like 'wikipedia:test'
:param fam: family name or object (override config.family)
@@ -1167,6 +1171,9 @@
'should be provided')
fam, _, code = code.partition(':')
else:
+ if not fam: # try code as family
+ with suppress(exceptions.UnknownFamilyError):
+ fam = Family.load(code)
# Fallback to config defaults
code = code or _config.mylang
fam = fam or _config.family
diff --git a/pywikibot/data/sparql.py b/pywikibot/data/sparql.py
index c3a44ca..d08d323 100644
--- a/pywikibot/data/sparql.py
+++ b/pywikibot/data/sparql.py
@@ -49,7 +49,7 @@
"""
# default to Wikidata
if not repo and not endpoint:
- repo = Site('wikidata', 'wikidata')
+ repo = Site('wikidata')

if repo:
try:
diff --git a/pywikibot/family.py b/pywikibot/family.py
index 08b02cb..871a36c 100644
--- a/pywikibot/family.py
+++ b/pywikibot/family.py
@@ -573,10 +573,6 @@
if fam is None:
fam = config.family

- assert all(x in NAME_CHARACTERS for x in 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]

diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py
index 3042b44..e6fda16 100644
--- a/pywikibot/page/_wikibase.py
+++ b/pywikibot/page/_wikibase.py
@@ -1304,7 +1304,7 @@
'wikibase-form': lambda value, site: LexemeForm(site, value['id']),
'wikibase-sense': lambda value, site: LexemeSense(site, value['id']),
'commonsMedia': lambda value, site:
- FilePage(pywikibot.Site('commons', 'commons'), value), # T90492
+ FilePage(pywikibot.Site('commons'), value), # T90492
'globe-coordinate': pywikibot.Coordinate.fromWikibase,
'geo-shape': pywikibot.WbGeoShape.fromWikibase,
'tabular-data': pywikibot.WbTabularData.fromWikibase,
@@ -1882,7 +1882,7 @@
Basic usage sample:

>>> import pywikibot
- >>> repo = pywikibot.Site('wikidata:wikidata')
+ >>> repo = pywikibot.Site('wikidata')
>>> L2 = pywikibot.LexemePage(repo, 'L2') # create a Lexeme page
>>> list(L2.claims.keys()) # access the claims
['P5831', 'P5402']
diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py
index ce2c893..98c2c81 100644
--- a/pywikibot/site/_apisite.py
+++ b/pywikibot/site/_apisite.py
@@ -195,7 +195,7 @@
"""
# TODO this only works for some WMF sites
if not site:
- site = pywikibot.Site('meta', 'meta')
+ site = pywikibot.Site('meta')
req = site._request(expiry=datetime.timedelta(days=10),
parameters={'action': 'sitematrix'})
data = req.submit()
diff --git a/pywikibot/specialbots/_upload.py b/pywikibot/specialbots/_upload.py
index 66a81bb..ddffd20 100644
--- a/pywikibot/specialbots/_upload.py
+++ b/pywikibot/specialbots/_upload.py
@@ -113,7 +113,7 @@
self.force_if_shared = force_if_shared

if config.upload_to_commons:
- default_site = pywikibot.Site('commons:commons')
+ default_site = pywikibot.Site('commons')
else:
default_site = pywikibot.Site()
self.target_site = target_site or default_site
diff --git a/scripts/checkimages.py b/scripts/checkimages.py
index 0c9430e..6bea64c 100755
--- a/scripts/checkimages.py
+++ b/scripts/checkimages.py
@@ -831,7 +831,7 @@
except NoPageError:
return False # Image deleted, no hash found. Skip the image.

- site = pywikibot.Site('commons', 'commons')
+ site = pywikibot.Site('commons')
commons_image_with_this_hash = next(
iter(site.allimages(sha1=hash_found, total=1)), None)
if commons_image_with_this_hash:
diff --git a/scripts/data_ingestion.py b/scripts/data_ingestion.py
index 28c75ae..c567c05 100755
--- a/scripts/data_ingestion.py
+++ b/scripts/data_ingestion.py
@@ -139,7 +139,7 @@
self.contents = None

if not site:
- site = pywikibot.Site('commons:commons')
+ site = pywikibot.Site('commons')

# default title
super().__init__(site, self.get_title('%(_filename)s.%(_ext)s'))
diff --git a/scripts/harvest_template.py b/scripts/harvest_template.py
index e647160..f6b778b 100755
--- a/scripts/harvest_template.py
+++ b/scripts/harvest_template.py
@@ -335,7 +335,7 @@
continue
claim.setTarget(match.group('url'))
elif claim.type == 'commonsMedia':
- commonssite = pywikibot.Site('commons', 'commons')
+ commonssite = pywikibot.Site('commons')
imagelink = pywikibot.Link(
value, source=commonssite, default_namespace=6)
image = pywikibot.FilePage(imagelink)
diff --git a/scripts/illustrate_wikidata.py b/scripts/illustrate_wikidata.py
index 6c8b587..35245ef 100755
--- a/scripts/illustrate_wikidata.py
+++ b/scripts/illustrate_wikidata.py
@@ -57,7 +57,7 @@
return

newclaim = pywikibot.Claim(self.repo, self.wdproperty)
- commonssite = pywikibot.Site('commons', 'commons')
+ commonssite = pywikibot.Site('commons')
imagelink = pywikibot.Link(imagename, source=commonssite,
default_namespace=6)
image = pywikibot.FilePage(imagelink)
diff --git a/scripts/maintenance/sorting_order.py b/scripts/maintenance/sorting_order.py
index 1d80e2e..06c373f 100755
--- a/scripts/maintenance/sorting_order.py
+++ b/scripts/maintenance/sorting_order.py
@@ -68,7 +68,7 @@

def main():
"""Main entry function."""
- site = pywikibot.Site('meta', 'meta')
+ site = pywikibot.Site('meta')
for list_name, page_name in pages.items():
page = pywikibot.Page(site, page_name, ns=site.namespaces.MEDIAWIKI)
update_family(list_name, page)
diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py
index 9fa1e94..4a17748 100755
--- a/tests/i18n_tests.py
+++ b/tests/i18n_tests.py
@@ -60,7 +60,7 @@
Test whether the localzation is found either with the Site object
or with the site code.
"""
- site = Site('commons', 'commons')
+ site = Site('commons')
for code in (site, 'commons'):
with self.subTest(code=code):
self.assertEqual(i18n.translate(code, self.xdict),
diff --git a/tests/page_tests.py b/tests/page_tests.py
index 5176308..c58d6b8 100755
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -1238,7 +1238,7 @@
def test_create_short_link(self):
"""Test create_short_link function."""
# Make sure test user is logged in on meta:meta (T244062)
- meta = pywikibot.Site('meta:meta')
+ meta = pywikibot.Site('meta')
if not meta.logged_in():
meta.login()
if not meta.user():
diff --git a/tests/site_tests.py b/tests/site_tests.py
index 2d5642e..2c0f0e3 100755
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -3346,7 +3346,7 @@
with self.assertRaises(KeyError):
site.family.hostname('wowwiki')
with self.assertRaises(UnknownSiteError):
- pywikibot.Site('wowwiki', 'wowwiki')
+ pywikibot.Site('wowwiki')
with self.assertRaises(UnknownSiteError):
pywikibot.Site('ceb', 'wowwiki')

diff --git a/tests/wikibase_edit_tests.py b/tests/wikibase_edit_tests.py
index 40cd4d3..589d12d 100755
--- a/tests/wikibase_edit_tests.py
+++ b/tests/wikibase_edit_tests.py
@@ -349,7 +349,7 @@

# set new claim
claim = pywikibot.page.Claim(testsite, 'P27199', datatype='geo-shape')
- commons_site = pywikibot.Site('commons', 'commons')
+ commons_site = pywikibot.Site('commons')
page = pywikibot.Page(commons_site, 'Data:Lyngby Hovedgade.map')
target = pywikibot.WbGeoShape(page)
claim.setTarget(target)
@@ -369,7 +369,7 @@
# set new claim
claim = pywikibot.page.Claim(
testsite, 'P30175', datatype='tabular-data')
- commons_site = pywikibot.Site('commons', 'commons')
+ commons_site = pywikibot.Site('commons')
page = pywikibot.Page(commons_site, 'Data:Bea.gov/GDP by state.tab')
target = pywikibot.WbGeoShape(page)
claim.setTarget(target)
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 4ced0de..96b7ad6 100755
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -640,7 +640,7 @@

def setUp(self):
"""Setup tests."""
- self.commons = pywikibot.Site('commons', 'commons')
+ self.commons = pywikibot.Site('commons')
self.page = Page(self.commons, 'Data:Lyngby Hovedgade.map')
super().setUp()

@@ -716,7 +716,7 @@

def setUp(self):
"""Setup tests."""
- self.commons = pywikibot.Site('commons', 'commons')
+ self.commons = pywikibot.Site('commons')
self.page = Page(self.commons, 'Data:Bea.gov/GDP by state.tab')
super().setUp()


To view, visit change 786171. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ie8d36c367715ca45c5bf3572b8f88160f5249622
Gerrit-Change-Number: 786171
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki@aol.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged