jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/512535 )
Change subject: [IMPR] make date.FormatDate class a function (PEP20) ......................................................................
[IMPR] make date.FormatDate class a function (PEP20)
- deprecate date.FormatDate class which is a function encapsulated in a class - format_date function is to be used instead - reuse dayMnthFmts instead of concatening again - introduce full range checking for month and day value - replace FormatDate with format_date in pagegenerators.py
Change-Id: I36900f2931398c4adb967acec010903e27b01e71 --- M pywikibot/date.py M pywikibot/pagegenerators.py 2 files changed, 37 insertions(+), 5 deletions(-)
Approvals: Dvorapa: Looks good to me, but someone else must approve Mpaa: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/date.py b/pywikibot/date.py index 49a22ea..1cd5c5e 100644 --- a/pywikibot/date.py +++ b/pywikibot/date.py @@ -19,6 +19,7 @@ import re from string import digits as _decimalDigits # noqa: N812
+from pywikibot import Site from pywikibot.textlib import NON_LATIN_DIGITS from pywikibot.tools import first_lower, first_upper, deprecated, UnicodeType
@@ -2215,9 +2216,10 @@ return None, None
+@deprecated('date.format_date', since='20190526') class FormatDate(object):
- """Format a date.""" + """DEPRECATED. Format a date."""
def __init__(self, site): """Initializer.""" @@ -2225,7 +2227,36 @@
def __call__(self, m, d): """Return a formatted month and day.""" - return formats[dayMnthFmts[m - 1]][self.site.lang](d) + return format_date(m, d, self.site) + + +def format_date(month, day, lang=None, year=2000): + """Format a date localized to given lang. + + @param month: month in range of 1..12 + @type month: int + @param day: day of month in range of 1..31 + @type day: int + @param lang: a site object or language key. Defaults to current site. + @type lang: BaseSite or string + @param year: year for which the date is to be formatted. always 29 will be + given For February except the year is given. Default is leap year 2000. + @type year: int + @return: localized date like "January 11" + @rtype: str + @raises ValueError: Wrong day value; must be 1-28/29/30/31 + @raises IllegalMonthError: bad month number; must be 1-12 + """ + if not lang: + lang = Site().lang + elif hasattr(lang, 'lang'): + lang = lang.lang + max_day = calendar.monthrange(year, month)[1] + if not 1 <= day <= max_day: + raise ValueError( + 'Wrong day value {day}; must be 1-{max_day}' + .format(day=day, max_day=max_day)) + return formats[dayMnthFmts[month - 1]][lang](day)
def formatYear(lang, year): diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py index 325d905..011dfd9 100644 --- a/pywikibot/pagegenerators.py +++ b/pywikibot/pagegenerators.py @@ -2904,12 +2904,13 @@ """ if site is None: site = pywikibot.Site() - fd = date.FormatDate(site) - firstPage = pywikibot.Page(site, fd(start_month, 1)) + lang = site.lang + firstPage = pywikibot.Page(site, date.format_date(start_month, 1, lang)) pywikibot.output('Starting with %s' % firstPage.title(as_link=True)) for month in range(start_month, end_month + 1): for day in range(1, calendar.monthrange(year, month)[1] + 1): - yield pywikibot.Page(pywikibot.Link(fd(month, day), site)) + yield pywikibot.Page( + pywikibot.Link(date.format_date(month, day, lang), site))
def WikidataPageFromItemGenerator(gen, site):