Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 85339f5

Browse files
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
1 parent 337d310 commit 85339f5

3 files changed

Lines changed: 31 additions & 17 deletions

File tree

Lib/calendar.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -571,19 +571,11 @@ def __init__(self, firstweekday=0, locale=None):
571571

572572
def formatweekday(self, day, width):
573573
with different_locale(self.locale):
574-
if width >= 9:
575-
names = day_name
576-
else:
577-
names = day_abbr
578-
name = names[day]
579-
return name[:width].center(width)
574+
return super().formatweekday(day, width)
580575

581576
def formatmonthname(self, theyear, themonth, width, withyear=True):
582577
with different_locale(self.locale):
583-
s = month_name[themonth]
584-
if withyear:
585-
s = "%s %r" % (s, theyear)
586-
return s.center(width)
578+
return super().formatmonthname(theyear, themonth, width, withyear)
587579

588580

589581
class LocaleHTMLCalendar(HTMLCalendar):
@@ -601,16 +593,11 @@ def __init__(self, firstweekday=0, locale=None):
601593

602594
def formatweekday(self, day):
603595
with different_locale(self.locale):
604-
s = day_abbr[day]
605-
return '<th class="%s">%s</th>' % (self.cssclasses[day], s)
596+
return super().formatweekday(day)
606597

607598
def formatmonthname(self, theyear, themonth, withyear=True):
608599
with different_locale(self.locale):
609-
s = month_name[themonth]
610-
if withyear:
611-
s = '%s %s' % (s, theyear)
612-
return '<tr><th colspan="7" class="month">%s</th></tr>' % s
613-
600+
return super().formatmonthname(theyear, themonth, withyear)
614601

615602
# Support for old module level interface
616603
c = TextCalendar()

Lib/test/test_calendar.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,30 @@ def test_locale_calendars(self):
564564
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
565565
self.assertEqual(old_october, new_october)
566566

567+
def test_locale_html_calendar_custom_css_class_month_name(self):
568+
try:
569+
cal = calendar.LocaleHTMLCalendar(locale='')
570+
local_month = cal.formatmonthname(2010, 10, 10)
571+
except locale.Error:
572+
# cannot set the system default locale -- skip rest of test
573+
raise unittest.SkipTest('cannot set the system default locale')
574+
self.assertIn('class="month"', local_month)
575+
cal.cssclass_month_head = "text-center month"
576+
local_month = cal.formatmonthname(2010, 10, 10)
577+
self.assertIn('class="text-center month"', local_month)
578+
579+
def test_locale_html_calendar_custom_css_class_weekday(self):
580+
try:
581+
cal = calendar.LocaleHTMLCalendar(locale='')
582+
local_weekday = cal.formatweekday(6)
583+
except locale.Error:
584+
# cannot set the system default locale -- skip rest of test
585+
raise unittest.SkipTest('cannot set the system default locale')
586+
self.assertIn('class="sun"', local_weekday)
587+
cal.cssclasses_weekday_head = ["mon2", "tue2", "wed2", "thu2", "fri2", "sat2", "sun2"]
588+
local_weekday = cal.formatweekday(6)
589+
self.assertIn('class="sun2"', local_weekday)
590+
567591
def test_itermonthdays3(self):
568592
# ensure itermonthdays3 doesn't overflow after datetime.MAXYEAR
569593
list(calendar.Calendar().itermonthdays3(datetime.MAXYEAR, 12))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
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.
2+
Patch by Srinivas Reddy Thatiparthy
3+

0 commit comments

Comments
 (0)