jenkins-bot has submitted this change and it was merged.
Change subject: [IMPROV] Deprecate nocapitalize and case
......................................................................
[IMPROV] Deprecate nocapitalize and case
Instead of a static list in the Family file the API should be queried.
All scripts use the Namespace object and APISite itself does check the
Namespace object of the main namespace. The Family class does use a
property which buffers the current state but reads from it will issue a
deprecation warning.
This also deprecates case() as the namespace case should be used if
possible (which is now always usable).
Change-Id: If33ee82d5d9bf657ee36139c409f347b622d3b71
---
M pywikibot/family.py
M pywikibot/page.py
M pywikibot/site.py
M pywikibot/textlib.py
M pywikibot/tools/__init__.py
M scripts/category.py
M scripts/cosmetic_changes.py
M scripts/image.py
M scripts/template.py
9 files changed, 55 insertions(+), 34 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/family.py b/pywikibot/family.py
index 435e5b0..01a6ac7 100644
--- a/pywikibot/family.py
+++ b/pywikibot/family.py
@@ -26,7 +26,7 @@
import pywikibot
from pywikibot import config2 as config
-from pywikibot.tools import deprecated, deprecate_arg
+from pywikibot.tools import deprecated, deprecate_arg, issue_deprecation_warning
from pywikibot.exceptions import Error, UnknownFamily, FamilyMaintenanceWarning
logger = logging.getLogger("pywiki.wiki.family")
@@ -701,8 +701,8 @@
# 'en': "Disambiguation"
self.disambcatname = {}
- # On most wikis page names must start with a capital letter, but some
- # languages don't use this.
+ # DEPRECATED, stores the code of the site which have a case sensitive
+ # main namespace. Use the Namespace given from the Site instead
self.nocapitalize = []
# attop is a list of languages that prefer to have the interwiki
@@ -853,6 +853,20 @@
_families = {}
+ def __getattribute__(self, name):
+ """
+ Check if attribute is deprecated and warn accordingly.
+
+ This is necessary as subclasses could prevent that message by using a
+ class variable. Only penalize getting it because it must be set so that
+ the backwards compatibility is still available.
+ """
+ if name == 'nocapitalize':
+ issue_deprecation_warning('nocapitalize',
+ "APISite.siteinfo['case'] or "
+ "Namespace.case ==
'case-sensitive'", 2)
+ return super(Family, self).__getattribute__(name)
+
@staticmethod
@deprecate_arg('fatal', None)
def load(fam=None):
diff --git a/pywikibot/page.py b/pywikibot/page.py
index 1f4c866..f088d82 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -4537,12 +4537,7 @@
raise pywikibot.InvalidTitle("The link does not contain a page "
"title")
- if hasattr(self._site.namespaces[self._namespace], 'case'):
- case = self._site.namespaces[self._namespace].case
- else:
- case = self._site.case()
-
- if case == 'first-letter':
+ if self._site.namespaces[self._namespace].case == 'first-letter':
t = t[:1].upper() + t[1:]
self._title = t
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 2c36ffa..87cd7f1 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -367,8 +367,8 @@
@classmethod
def builtin_namespaces(cls, use_image_name=False):
"""Return a dict of the builtin namespaces."""
- return dict([(i, cls(i, use_image_name=use_image_name))
- for i in range(-2, 16)])
+ return dict((i, cls(i, use_image_name=use_image_name,
case='first-letter'))
+ for i in range(-2, 16))
@staticmethod
def normalize_name(name):
@@ -535,7 +535,6 @@
raise UnknownSite(u"Language '%s' does not exist in family
%s"
% (self.__code, self.__family.name))
- self.nocapitalize = self.code in self.family.nocapitalize
self._username = [normalize_username(user), normalize_username(sysop)]
self.use_hard_category_redirects = (
@@ -544,6 +543,11 @@
# following are for use with lock_page and unlock_page methods
self._pagemutex = threading.Lock()
self._locked_pages = []
+
+ @property
+ @deprecated("APISite.siteinfo['case'] or Namespace.case ==
'case-sensitive'")
+ def nocapitalize(self):
+ return self.siteinfo['case'] == 'case-sensitive'
@property
def throttle(self):
@@ -919,7 +923,7 @@
name2 = name2.strip()
# If the namespace has a case definition it's overriding the site's
# case definition
- if (ns1_obj.case if hasattr(ns1_obj, 'case') else self.case()) ==
'first-letter':
+ if ns1_obj.case == 'first-letter':
name1 = name1[:1].upper() + name1[1:]
name2 = name2[:1].upper() + name2[1:]
return name1 == name2
@@ -2155,6 +2159,9 @@
if is_mw114:
canonical_name = nsdata.pop('canonical')
+ if 'case' not in nsdata:
+ nsdata['case'] = self.siteinfo['case']
+
namespace = Namespace(ns, canonical_name, custom_name,
use_image_name=not is_mw114,
**nsdata)
@@ -2205,8 +2212,11 @@
"""Site information dict."""
return self._siteinfo
+ @deprecated('use siteinfo or Namespace instance')
def case(self):
"""Return this site's capitalization rule."""
+ # This is the global setting via $wgCapitalLinks, it is used whenever
+ # the namespaces don't propagate the namespace specific value.
return self.siteinfo['case']
def dbName(self):
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 372e16e..69c67d8 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -779,7 +779,7 @@
# title might contain regex special characters
title = re.escape(title)
# title might not be capitalized correctly on the wiki
- if title[0].isalpha() and not site.nocapitalize:
+ if title[0].isalpha() and site.namespaces[14].case == 'first-letter':
title = "[%s%s]" % (title[0].upper(), title[0].lower()) + title[1:]
# spaces and underscores in page titles are interchangeable and collapsible
title = title.replace(r"\ ", "[ _]+").replace(r"\_",
"[ _]+")
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index 1c879f2..051349f 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -798,6 +798,16 @@
return outer_wrapper
+def issue_deprecation_warning(name, instead, depth):
+ """Issue a deprecation warning."""
+ if instead:
+ warn(u'{0} is deprecated, use {1} instead.'.format(name, instead),
+ DeprecationWarning, depth + 1)
+ else:
+ warn(u'{0} is deprecated.'.format(name), _NotImplementedWarning,
+ depth + 1)
+
+
@add_full_name
def deprecated(*args, **kwargs):
"""Decorator to output a deprecation warning.
@@ -825,12 +835,7 @@
"""
name = obj.__full_name__
depth = get_wrapper_depth(wrapper) + 1
- if instead:
- warn(u"%s is deprecated, use %s instead." % (name, instead),
- DeprecationWarning, depth)
- else:
- warn(u"%s is deprecated." % name,
- _NotImplementedWarning, depth)
+ issue_deprecation_warning(name, instead, depth)
return obj(*args, **kwargs)
if not __debug__:
@@ -1042,7 +1047,7 @@
@rtype: callable
"""
def call(*a, **kw):
- warn(warn_message, DeprecationWarning, 2)
+ issue_deprecation_warning(old_name, new_name, 2)
return target(*a, **kw)
if target_module is None:
target_module = target.__module__
@@ -1057,11 +1062,8 @@
if class_name:
target_module += class_name + '.'
source_module += class_name + '.'
- warn_message = ('{source}{old} is deprecated, use {target}{new} '
- 'instead.').format(new=target.__name__,
- old=old_name or target.__name__,
- target=target_module,
- source=source_module)
+ old_name = source_module + (old_name or target.__name__)
+ new_name = target_module + target.__name__
if not __debug__:
return target
diff --git a/scripts/category.py b/scripts/category.py
index 4e2fcc2..1db584f 100755
--- a/scripts/category.py
+++ b/scripts/category.py
@@ -351,7 +351,7 @@
for cat in cats:
pywikibot.output(u"* %s" % cat.title())
newcat = self.newcat
- if not self.current_page.site.nocapitalize:
+ if self.namespaces[14].case == 'first-letter':
newcat = newcat[:1].upper() + newcat[1:]
catpl = pywikibot.Category(self.current_page.site, newcat)
if catpl in cats:
diff --git a/scripts/cosmetic_changes.py b/scripts/cosmetic_changes.py
index 04a19e6..68622b2 100755
--- a/scripts/cosmetic_changes.py
+++ b/scripts/cosmetic_changes.py
@@ -522,8 +522,9 @@
else:
# Try to capitalize the first letter of the title.
# Not useful for languages that don't capitalize nouns.
- # TODO: Determine which languages this is suitable for
- # perhaps using self.site.nocapitalize
+ # TODO: Add a configuration variable for each site,
+ # which determines if the link target is written in
+ # uppercase
if self.site.sitename() == 'wikipedia:de':
titleWithSection = (titleWithSection[0].upper() +
titleWithSection[1:])
@@ -653,7 +654,7 @@
new = ''
else:
new = '{{%s}}' % new
- if not self.site.nocapitalize:
+ if self.namespaces[10].case == 'first-letter':
old = '[' + old[0].upper() + old[0].lower() + ']' +
old[1:]
text = textlib.replaceExcept(
text,
diff --git a/scripts/image.py b/scripts/image.py
index fb26ad2..08efc95 100644
--- a/scripts/image.py
+++ b/scripts/image.py
@@ -137,7 +137,7 @@
replacements = []
- if not self.site.nocapitalize:
+ if self.site.namespaces[6].case == 'first-letter':
case = re.escape(self.old_image[0].upper() +
self.old_image[0].lower())
escaped = '[' + case + ']' + re.escape(self.old_image[1:])
diff --git a/scripts/template.py b/scripts/template.py
index 25aad55..b1054e4 100755
--- a/scripts/template.py
+++ b/scripts/template.py
@@ -173,11 +173,10 @@
# regular expression to find the original template.
# {{vfd}} does the same thing as {{Vfd}}, so both will be found.
# The old syntax, {{msg:vfd}}, will also be found.
- # TODO: check site.nocapitalize()
templatePatterns = []
for template in self.templates:
templatePattern = template.title(withNamespace=False)
- if not pywikibot.Site().nocapitalize:
+ if mysite.namespaces[10].case == 'first-letter':
templatePattern = '[%s%s]%s' % (templatePattern[0].upper(),
templatePattern[0].lower(),
templatePattern[1:])
@@ -250,7 +249,7 @@
site = pywikibot.Site()
for old, new in self.templates.items():
namespaces = list(site.namespace(10, all=True))
- if not site.nocapitalize:
+ if site.namespaces[10].case == 'first-letter':
pattern = '[' + \
re.escape(old[0].upper()) + \
re.escape(old[0].lower()) + \
--
To view, visit
https://gerrit.wikimedia.org/r/190619
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: If33ee82d5d9bf657ee36139c409f347b622d3b71
Gerrit-PatchSet: 8
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>