From 85339f5c220a5e79c47c3a33c93f1dca5c59c52e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Srinivas=20Reddy=20Thatiparthy=20=28=E0=B0=B6=E0=B1=8D?= =?UTF-8?q?=E0=B0=B0=E0=B1=80=E0=B0=A8=E0=B0=BF=E0=B0=B5=E0=B0=BE=E0=B0=B8?= =?UTF-8?q?=E0=B1=8D=20=20=E0=B0=B0=E0=B1=86=E0=B0=A1=E0=B1=8D=E0=B0=A1?= =?UTF-8?q?=E0=B0=BF=20=E0=B0=A4=E0=B0=BE=E0=B0=9F=E0=B0=BF=E0=B0=AA?= =?UTF-8?q?=E0=B0=B0=E0=B1=8D=E0=B0=A4=E0=B0=BF=29?= Date: Tue, 2 Jun 2020 17:03:09 +0530 Subject: [PATCH] bpo-35078: Allow customization of CSS class name of a month in calendar module (gh-10137) Refactor formatweekday(), formatmonthname() methods in LocaleHTMLCalendar and LocaleTextCalendar classes in calendar module to call the base class methods. This enables customizable CSS classes for LocaleHTMLCalendar and LocaleTextCalendar. Patch by Srinivas Reddy Thatiparthy --- Lib/calendar.py | 21 ++++------------ Lib/test/test_calendar.py | 24 +++++++++++++++++++ .../2018-10-27-09-37-03.bpo-35078.kweA3R.rst | 3 +++ 3 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-10-27-09-37-03.bpo-35078.kweA3R.rst diff --git a/Lib/calendar.py b/Lib/calendar.py index 7550d52c0a9..7311a017372 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -571,19 +571,11 @@ class LocaleTextCalendar(TextCalendar): def formatweekday(self, day, width): with different_locale(self.locale): - if width >= 9: - names = day_name - else: - names = day_abbr - name = names[day] - return name[:width].center(width) + return super().formatweekday(day, width) def formatmonthname(self, theyear, themonth, width, withyear=True): with different_locale(self.locale): - s = month_name[themonth] - if withyear: - s = "%s %r" % (s, theyear) - return s.center(width) + return super().formatmonthname(theyear, themonth, width, withyear) class LocaleHTMLCalendar(HTMLCalendar): @@ -601,16 +593,11 @@ class LocaleHTMLCalendar(HTMLCalendar): def formatweekday(self, day): with different_locale(self.locale): - s = day_abbr[day] - return '%s' % (self.cssclasses[day], s) + return super().formatweekday(day) def formatmonthname(self, theyear, themonth, withyear=True): with different_locale(self.locale): - s = month_name[themonth] - if withyear: - s = '%s %s' % (s, theyear) - return '%s' % s - + return super().formatmonthname(theyear, themonth, withyear) # Support for old module level interface c = TextCalendar() diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py index 6241d114d33..7c7ec1c931a 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -564,6 +564,30 @@ class CalendarTestCase(unittest.TestCase): new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10) self.assertEqual(old_october, new_october) + def test_locale_html_calendar_custom_css_class_month_name(self): + try: + cal = calendar.LocaleHTMLCalendar(locale='') + local_month = cal.formatmonthname(2010, 10, 10) + except locale.Error: + # cannot set the system default locale -- skip rest of test + raise unittest.SkipTest('cannot set the system default locale') + self.assertIn('class="month"', local_month) + cal.cssclass_month_head = "text-center month" + local_month = cal.formatmonthname(2010, 10, 10) + self.assertIn('class="text-center month"', local_month) + + def test_locale_html_calendar_custom_css_class_weekday(self): + try: + cal = calendar.LocaleHTMLCalendar(locale='') + local_weekday = cal.formatweekday(6) + except locale.Error: + # cannot set the system default locale -- skip rest of test + raise unittest.SkipTest('cannot set the system default locale') + self.assertIn('class="sun"', local_weekday) + cal.cssclasses_weekday_head = ["mon2", "tue2", "wed2", "thu2", "fri2", "sat2", "sun2"] + local_weekday = cal.formatweekday(6) + self.assertIn('class="sun2"', local_weekday) + def test_itermonthdays3(self): # ensure itermonthdays3 doesn't overflow after datetime.MAXYEAR list(calendar.Calendar().itermonthdays3(datetime.MAXYEAR, 12)) diff --git a/Misc/NEWS.d/next/Library/2018-10-27-09-37-03.bpo-35078.kweA3R.rst b/Misc/NEWS.d/next/Library/2018-10-27-09-37-03.bpo-35078.kweA3R.rst new file mode 100644 index 00000000000..123f9dabde9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-10-27-09-37-03.bpo-35078.kweA3R.rst @@ -0,0 +1,3 @@ +Refactor formatweekday, formatmonthname methods in LocaleHTMLCalendar and LocaleTextCalendar classes in calendar module to call the base class methods.This enables customizable CSS classes for LocaleHTMLCalendar. +Patch by Srinivas Reddy Thatiparthy +