annotate Lib/calendar.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents bfdf366a779a
children af41eca1959e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
1 """Calendar printing functions
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
2
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
3 Note when comparing these calendars to the ones printed by cal(1): By
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
4 default, these calendars have Monday as the first day of the week, and
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
5 Sunday as the last (the European convention). Use setfirstweekday() to
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
6 set the first day of the week (0=Monday, 6=Sunday)."""
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
43822
c4e2936d1307 Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 38084
diff changeset
8 import sys
c4e2936d1307 Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 38084
diff changeset
9 import datetime
c4e2936d1307 Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 38084
diff changeset
10 import locale as _locale
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
12 __all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
13 "firstweekday", "isleap", "leapdays", "weekday", "monthrange",
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
14 "monthcalendar", "prmonth", "month", "prcal", "calendar",
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
15 "timegm", "month_name", "month_abbr", "day_name", "day_abbr"]
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
16
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # Exception raised for bad input (with string parameter for details)
10819
5e03ea6927f2 No need to import gmtime, ctime, asctime.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
18 error = ValueError
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
20 # Exceptions raised for bad input
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
21 class IllegalMonthError(ValueError):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
22 def __init__(self, month):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
23 self.month = month
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
24 def __str__(self):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
25 return "bad month number %r; must be 1-12" % self.month
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
26
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
27
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
28 class IllegalWeekdayError(ValueError):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
29 def __init__(self, weekday):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
30 self.weekday = weekday
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
31 def __str__(self):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
32 return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self.weekday
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
33
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
34
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
35 # Constants for months referenced later
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
36 January = 1
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
37 February = 2
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
38
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
39 # Number of days per month (except for February in leap years)
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
40 mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
41
22472
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
42 # This module used to have hard-coded lists of day and month names, as
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
43 # English strings. The classes following emulate a read-only version of
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
44 # that, but supply localized names. Note that the values are computed
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
45 # fresh on each call, in case the user changes locale between calls.
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
46
23930
8eb2f31eea9e SF 570727 indexer() class no longer needed since lists now support slicing
Raymond Hettinger <python@rcn.com>
parents: 22472
diff changeset
47 class _localized_month:
33987
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
48
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
49 _months = [datetime.date(2001, i+1, 1).strftime for i in range(12)]
33987
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
50 _months.insert(0, lambda x: "")
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
51
22472
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
52 def __init__(self, format):
17906
9af806b286c6 Application of patch #401842 by Denis S. Otkidach to support
Barry Warsaw <barry@python.org>
parents: 16070
diff changeset
53 self.format = format
22472
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
54
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
55 def __getitem__(self, i):
33987
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
56 funcs = self._months[i]
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
57 if isinstance(i, slice):
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
58 return [f(self.format) for f in funcs]
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
59 else:
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
60 return funcs(self.format)
22472
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
61
22289
1873833c616d make _localized_name instances work more like the tuples they replaced. In
Skip Montanaro <skip@pobox.com>
parents: 17906
diff changeset
62 def __len__(self):
22472
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
63 return 13
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
64
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
65
23930
8eb2f31eea9e SF 570727 indexer() class no longer needed since lists now support slicing
Raymond Hettinger <python@rcn.com>
parents: 22472
diff changeset
66 class _localized_day:
33987
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
67
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
68 # January 1, 2001, was a Monday.
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
69 _days = [datetime.date(2001, 1, i+1).strftime for i in range(7)]
33987
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
70
22472
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
71 def __init__(self, format):
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
72 self.format = format
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
73
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
74 def __getitem__(self, i):
33987
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
75 funcs = self._days[i]
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
76 if isinstance(i, slice):
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
77 return [f(self.format) for f in funcs]
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
78 else:
489656dbdf5a SF bug 1065388: calendar day/month name lookup too slow
Tim Peters <tim.peters@gmail.com>
parents: 32290
diff changeset
79 return funcs(self.format)
22472
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
80
32290
71b3d2bbe63b There is no reason to have an underscore after self
Neal Norwitz <nnorwitz@gmail.com>
parents: 31515
diff changeset
81 def __len__(self):
22472
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
82 return 7
17906
9af806b286c6 Application of patch #401842 by Denis S. Otkidach to support
Barry Warsaw <barry@python.org>
parents: 16070
diff changeset
83
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
84
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
85 # Full and abbreviated names of weekdays
22472
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
86 day_name = _localized_day('%A')
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
87 day_abbr = _localized_day('%a')
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
88
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1315
diff changeset
89 # Full and abbreviated names of months (1-based arrays!!!)
22472
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
90 month_name = _localized_month('%B')
ad3825d04dec SF bug 533234: tm_isdst > 1 Passed to strftime.
Tim Peters <tim.peters@gmail.com>
parents: 22460
diff changeset
91 month_abbr = _localized_month('%b')
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
92
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
93 # Constants for weekdays
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
94 (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
95
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
96
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
97 def isleap(year):
65544
1f0ab729c632 Merged revisions 85725 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 59431
diff changeset
98 """Return True for leap years, False for non-leap years."""
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 15097
diff changeset
99 return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
100
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
101
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
102 def leapdays(y1, y2):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
103 """Return number of leap years in range [y1, y2).
15097
5fb8dc289812 Fixed leapdays(). From Patch #101841, by Denis S. Otkidach.
Guido van Rossum <guido@python.org>
parents: 14261
diff changeset
104 Assume y1 <= y2."""
5fb8dc289812 Fixed leapdays(). From Patch #101841, by Denis S. Otkidach.
Guido van Rossum <guido@python.org>
parents: 14261
diff changeset
105 y1 -= 1
5fb8dc289812 Fixed leapdays(). From Patch #101841, by Denis S. Otkidach.
Guido van Rossum <guido@python.org>
parents: 14261
diff changeset
106 y2 -= 1
26793
db9388f37d8c SF 658405: calendar.py to rely on the datetime module instead of the time
Raymond Hettinger <python@rcn.com>
parents: 25879
diff changeset
107 return (y2//4 - y1//4) - (y2//100 - y1//100) + (y2//400 - y1//400)
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1314
diff changeset
108
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
109
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 def weekday(year, month, day):
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
111 """Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
112 day (1-31)."""
26793
db9388f37d8c SF 658405: calendar.py to rely on the datetime module instead of the time
Raymond Hettinger <python@rcn.com>
parents: 25879
diff changeset
113 return datetime.date(year, month, day).weekday()
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
115
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 def monthrange(year, month):
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
117 """Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
118 year, month."""
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
119 if not 1 <= month <= 12:
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
120 raise IllegalMonthError(month)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
121 day1 = weekday(year, month, 1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
122 ndays = mdays[month] + (month == February and isleap(year))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
123 return day1, ndays
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
125
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
126 class Calendar(object):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
127 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
128 Base calendar class. This class doesn't do any formatting. It simply
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
129 provides data to subclasses.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
130 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
131
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
132 def __init__(self, firstweekday=0):
37392
fe97c0016d4b Make firstweekday a simple attribute instead
Walter Dörwald <walter@livinglogic.de>
parents: 37379
diff changeset
133 self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
134
37432
796acde70195 Turn firstweekday into a property.
Walter Dörwald <walter@livinglogic.de>
parents: 37405
diff changeset
135 def getfirstweekday(self):
37433
bdbe07d96b8e Always return firstweekday % 7 instead of complaining
Walter Dörwald <walter@livinglogic.de>
parents: 37432
diff changeset
136 return self._firstweekday % 7
37434
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
137
37432
796acde70195 Turn firstweekday into a property.
Walter Dörwald <walter@livinglogic.de>
parents: 37405
diff changeset
138 def setfirstweekday(self, firstweekday):
796acde70195 Turn firstweekday into a property.
Walter Dörwald <walter@livinglogic.de>
parents: 37405
diff changeset
139 self._firstweekday = firstweekday
37434
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
140
37432
796acde70195 Turn firstweekday into a property.
Walter Dörwald <walter@livinglogic.de>
parents: 37405
diff changeset
141 firstweekday = property(getfirstweekday, setfirstweekday)
796acde70195 Turn firstweekday into a property.
Walter Dörwald <walter@livinglogic.de>
parents: 37405
diff changeset
142
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
143 def iterweekdays(self):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
144 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
145 Return a iterator for one week of weekday numbers starting with the
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
146 configured first one.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
147 """
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
148 for i in range(self.firstweekday, self.firstweekday + 7):
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
149 yield i%7
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
150
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
151 def itermonthdates(self, year, month):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
152 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
153 Return an iterator for one month. The iterator will yield datetime.date
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
154 values and will always iterate through complete weeks, so it will yield
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
155 dates outside the specified month.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
156 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
157 date = datetime.date(year, month, 1)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
158 # Go back to the beginning of the week
37392
fe97c0016d4b Make firstweekday a simple attribute instead
Walter Dörwald <walter@livinglogic.de>
parents: 37379
diff changeset
159 days = (date.weekday() - self.firstweekday) % 7
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
160 date -= datetime.timedelta(days=days)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
161 oneday = datetime.timedelta(days=1)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
162 while True:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
163 yield date
79088
bfdf366a779a #15421: fix an OverflowError in Calendar.itermonthdates() after datetime.MAXYEAR. Patch by Cédric Krier.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
164 try:
bfdf366a779a #15421: fix an OverflowError in Calendar.itermonthdates() after datetime.MAXYEAR. Patch by Cédric Krier.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
165 date += oneday
bfdf366a779a #15421: fix an OverflowError in Calendar.itermonthdates() after datetime.MAXYEAR. Patch by Cédric Krier.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
166 except OverflowError:
bfdf366a779a #15421: fix an OverflowError in Calendar.itermonthdates() after datetime.MAXYEAR. Patch by Cédric Krier.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
167 # Adding one day could fail after datetime.MAXYEAR
bfdf366a779a #15421: fix an OverflowError in Calendar.itermonthdates() after datetime.MAXYEAR. Patch by Cédric Krier.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
168 break
37434
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
169 if date.month != month and date.weekday() == self.firstweekday:
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
170 break
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
171
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
172 def itermonthdays2(self, year, month):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
173 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
174 Like itermonthdates(), but will yield (day number, weekday number)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
175 tuples. For days outside the specified month the day number is 0.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
176 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
177 for date in self.itermonthdates(year, month):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
178 if date.month != month:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
179 yield (0, date.weekday())
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
180 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
181 yield (date.day, date.weekday())
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
182
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
183 def itermonthdays(self, year, month):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
184 """
45224
403f705d58c9 Backport r60653:
Walter Dörwald <walter@livinglogic.de>
parents: 43828
diff changeset
185 Like itermonthdates(), but will yield day numbers. For days outside
403f705d58c9 Backport r60653:
Walter Dörwald <walter@livinglogic.de>
parents: 43828
diff changeset
186 the specified month the day number is 0.
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
187 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
188 for date in self.itermonthdates(year, month):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
189 if date.month != month:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
190 yield 0
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
191 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
192 yield date.day
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
194 def monthdatescalendar(self, year, month):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
195 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
196 Return a matrix (list of lists) representing a month's calendar.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
197 Each row represents a week; week entries are datetime.date values.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
198 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
199 dates = list(self.itermonthdates(year, month))
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
200 return [ dates[i:i+7] for i in range(0, len(dates), 7) ]
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
201
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
202 def monthdays2calendar(self, year, month):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
203 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
204 Return a matrix representing a month's calendar.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
205 Each row represents a week; week entries are
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
206 (day number, weekday number) tuples. Day numbers outside this month
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
207 are zero.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
208 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
209 days = list(self.itermonthdays2(year, month))
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
210 return [ days[i:i+7] for i in range(0, len(days), 7) ]
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
211
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
212 def monthdayscalendar(self, year, month):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
213 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
214 Return a matrix representing a month's calendar.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
215 Each row represents a week; days outside this month are zero.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
216 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
217 days = list(self.itermonthdays(year, month))
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
218 return [ days[i:i+7] for i in range(0, len(days), 7) ]
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
219
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
220 def yeardatescalendar(self, year, width=3):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
221 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
222 Return the data for the specified year ready for formatting. The return
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
223 value is a list of month rows. Each month row contains upto width months.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
224 Each month contains between 4 and 6 weeks and each week contains 1-7
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
225 days. Days are datetime.date objects.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
226 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
227 months = [
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
228 self.monthdatescalendar(year, i)
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
229 for i in range(January, January+12)
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
230 ]
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
231 return [months[i:i+width] for i in range(0, len(months), width) ]
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
232
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
233 def yeardays2calendar(self, year, width=3):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
234 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
235 Return the data for the specified year ready for formatting (similar to
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
236 yeardatescalendar()). Entries in the week lists are
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
237 (day number, weekday number) tuples. Day numbers outside this month are
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
238 zero.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
239 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
240 months = [
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
241 self.monthdays2calendar(year, i)
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
242 for i in range(January, January+12)
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
243 ]
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
244 return [months[i:i+width] for i in range(0, len(months), width) ]
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
245
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
246 def yeardayscalendar(self, year, width=3):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
247 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
248 Return the data for the specified year ready for formatting (similar to
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
249 yeardatescalendar()). Entries in the week lists are day numbers.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
250 Day numbers outside this month are zero.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
251 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
252 months = [
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
253 self.monthdayscalendar(year, i)
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
254 for i in range(January, January+12)
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
255 ]
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
256 return [months[i:i+width] for i in range(0, len(months), width) ]
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
257
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
258
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
259 class TextCalendar(Calendar):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
260 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
261 Subclass of Calendar that outputs a calendar as a simple plain text
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
262 similar to the UNIX program cal.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
263 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
264
37532
1583d3fdddcd missing 'self' from TextCalendar.prweek.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37434
diff changeset
265 def prweek(self, theweek, width):
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
266 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
267 Print a single week (no newline).
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
268 """
43828
00e6165f4482 Backport r58942:
Walter Dörwald <walter@livinglogic.de>
parents: 43822
diff changeset
269 print self.formatweek(theweek, width),
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
270
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
271 def formatday(self, day, weekday, width):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
272 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
273 Returns a formatted day.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
274 """
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
275 if day == 0:
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
276 s = ''
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
277 else:
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
278 s = '%2i' % day # right-align single-digit days
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
279 return s.center(width)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
280
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
281 def formatweek(self, theweek, width):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
282 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
283 Returns a single week in a string (no newline).
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
284 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
285 return ' '.join(self.formatday(d, wd, width) for (d, wd) in theweek)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
286
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
287 def formatweekday(self, day, width):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
288 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
289 Returns a formatted week day name.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
290 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
291 if width >= 9:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
292 names = day_name
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
293 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
294 names = day_abbr
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
295 return names[day][:width].center(width)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
296
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
297 def formatweekheader(self, width):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
298 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
299 Return a header for a week.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
300 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
301 return ' '.join(self.formatweekday(i, width) for i in self.iterweekdays())
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
302
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
303 def formatmonthname(self, theyear, themonth, width, withyear=True):
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
304 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
305 Return a formatted month name.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
306 """
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
307 s = month_name[themonth]
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
308 if withyear:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
309 s = "%s %r" % (s, theyear)
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
310 return s.center(width)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
311
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
312 def prmonth(self, theyear, themonth, w=0, l=0):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
313 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
314 Print a month's calendar.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
315 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
316 print self.formatmonth(theyear, themonth, w, l),
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
317
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
318 def formatmonth(self, theyear, themonth, w=0, l=0):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
319 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
320 Return a month's calendar string (multi-line).
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
321 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
322 w = max(2, w)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
323 l = max(1, l)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
324 s = self.formatmonthname(theyear, themonth, 7 * (w + 1) - 1)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
325 s = s.rstrip()
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
326 s += '\n' * l
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
327 s += self.formatweekheader(w).rstrip()
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
328 s += '\n' * l
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
329 for week in self.monthdays2calendar(theyear, themonth):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
330 s += self.formatweek(week, w).rstrip()
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
331 s += '\n' * l
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
332 return s
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
334 def formatyear(self, theyear, w=2, l=1, c=6, m=3):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
335 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
336 Returns a year's calendar as a multi-line string.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
337 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
338 w = max(2, w)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
339 l = max(1, l)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
340 c = max(2, c)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
341 colwidth = (w + 1) * 7 - 1
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
342 v = []
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
343 a = v.append
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
344 a(repr(theyear).center(colwidth*m+c*(m-1)).rstrip())
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
345 a('\n'*l)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
346 header = self.formatweekheader(w)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
347 for (i, row) in enumerate(self.yeardays2calendar(theyear, m)):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
348 # months in this row
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
349 months = range(m*i+1, min(m*(i+1)+1, 13))
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
350 a('\n'*l)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
351 names = (self.formatmonthname(theyear, k, colwidth, False)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
352 for k in months)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
353 a(formatstring(names, colwidth, c).rstrip())
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
354 a('\n'*l)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
355 headers = (header for k in months)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
356 a(formatstring(headers, colwidth, c).rstrip())
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
357 a('\n'*l)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
358 # max number of weeks for this row
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
359 height = max(len(cal) for cal in row)
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
360 for j in range(height):
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
361 weeks = []
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
362 for cal in row:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
363 if j >= len(cal):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
364 weeks.append('')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
365 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
366 weeks.append(self.formatweek(cal[j], w))
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
367 a(formatstring(weeks, colwidth, c).rstrip())
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
368 a('\n' * l)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
369 return ''.join(v)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
370
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
371 def pryear(self, theyear, w=0, l=0, c=6, m=3):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
372 """Print a year's calendar."""
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
373 print self.formatyear(theyear, w, l, c, m)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
374
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
375
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
376 class HTMLCalendar(Calendar):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
377 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
378 This calendar returns complete HTML pages.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
379 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
380
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
381 # CSS classes for the day <td>s
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
382 cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
383
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
384 def formatday(self, day, weekday):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
385 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
386 Return a day as a table cell.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
387 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
388 if day == 0:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
389 return '<td class="noday">&nbsp;</td>' # day outside month
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
390 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
391 return '<td class="%s">%d</td>' % (self.cssclasses[weekday], day)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
392
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
393 def formatweek(self, theweek):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
394 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
395 Return a complete week as a table row.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
396 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
397 s = ''.join(self.formatday(d, wd) for (d, wd) in theweek)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
398 return '<tr>%s</tr>' % s
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
399
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
400 def formatweekday(self, day):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
401 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
402 Return a weekday name as a table header.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
403 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
404 return '<th class="%s">%s</th>' % (self.cssclasses[day], day_abbr[day])
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
405
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
406 def formatweekheader(self):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
407 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
408 Return a header for a week as a table row.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
409 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
410 s = ''.join(self.formatweekday(i) for i in self.iterweekdays())
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
411 return '<tr>%s</tr>' % s
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
412
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
413 def formatmonthname(self, theyear, themonth, withyear=True):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
414 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
415 Return a month name as a table row.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
416 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
417 if withyear:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
418 s = '%s %s' % (month_name[themonth], theyear)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
419 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
420 s = '%s' % month_name[themonth]
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
421 return '<tr><th colspan="7" class="month">%s</th></tr>' % s
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
422
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
423 def formatmonth(self, theyear, themonth, withyear=True):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
424 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
425 Return a formatted month as a table.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
426 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
427 v = []
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
428 a = v.append
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
429 a('<table border="0" cellpadding="0" cellspacing="0" class="month">')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
430 a('\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
431 a(self.formatmonthname(theyear, themonth, withyear=withyear))
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
432 a('\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
433 a(self.formatweekheader())
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
434 a('\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
435 for week in self.monthdays2calendar(theyear, themonth):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
436 a(self.formatweek(week))
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
437 a('\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
438 a('</table>')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
439 a('\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
440 return ''.join(v)
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
441
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
442 def formatyear(self, theyear, width=3):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
443 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
444 Return a formatted year as a table of tables.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
445 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
446 v = []
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
447 a = v.append
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
448 width = max(width, 1)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
449 a('<table border="0" cellpadding="0" cellspacing="0" class="year">')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
450 a('\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
451 a('<tr><th colspan="%d" class="year">%s</th></tr>' % (width, theyear))
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
452 for i in range(January, January+12, width):
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
453 # months in this row
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
454 months = range(i, min(i+width, 13))
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
455 a('<tr>')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
456 for m in months:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
457 a('<td>')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
458 a(self.formatmonth(theyear, m, withyear=False))
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
459 a('</td>')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
460 a('</tr>')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
461 a('</table>')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
462 return ''.join(v)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
463
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
464 def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
465 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
466 Return a formatted year as a complete HTML page.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
467 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
468 if encoding is None:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
469 encoding = sys.getdefaultencoding()
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
470 v = []
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
471 a = v.append
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
472 a('<?xml version="1.0" encoding="%s"?>\n' % encoding)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
473 a('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
474 a('<html>\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
475 a('<head>\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
476 a('<meta http-equiv="Content-Type" content="text/html; charset=%s" />\n' % encoding)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
477 if css is not None:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
478 a('<link rel="stylesheet" type="text/css" href="%s" />\n' % css)
42952
d1789d4ff00c Fix title endtag in HTMLCalender.formatyearpage(). Fix documentation for
Walter Dörwald <walter@livinglogic.de>
parents: 41744
diff changeset
479 a('<title>Calendar for %d</title>\n' % theyear)
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
480 a('</head>\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
481 a('<body>\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
482 a(self.formatyear(theyear, width))
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
483 a('</body>\n')
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
484 a('</html>\n')
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
485 return ''.join(v).encode(encoding, "xmlcharrefreplace")
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
486
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
487
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
488 class TimeEncoding:
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
489 def __init__(self, locale):
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
490 self.locale = locale
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
491
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
492 def __enter__(self):
66458
d6c5750153bb Merged revisions 85728 via svnmerge from
Georg Brandl <georg@python.org>
parents: 65544
diff changeset
493 self.oldlocale = _locale.getlocale(_locale.LC_TIME)
d6c5750153bb Merged revisions 85728 via svnmerge from
Georg Brandl <georg@python.org>
parents: 65544
diff changeset
494 _locale.setlocale(_locale.LC_TIME, self.locale)
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
495
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
496 def __exit__(self, *args):
43822
c4e2936d1307 Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 38084
diff changeset
497 _locale.setlocale(_locale.LC_TIME, self.oldlocale)
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
498
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
499
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
500 class LocaleTextCalendar(TextCalendar):
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
501 """
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
502 This class can be passed a locale name in the constructor and will return
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
503 month and weekday names in the specified locale. If this locale includes
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
504 an encoding all strings containing month and weekday names will be returned
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
505 as unicode.
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
506 """
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
507
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
508 def __init__(self, firstweekday=0, locale=None):
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
509 TextCalendar.__init__(self, firstweekday)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
510 if locale is None:
43822
c4e2936d1307 Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 38084
diff changeset
511 locale = _locale.getdefaultlocale()
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
512 self.locale = locale
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
513
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
514 def formatweekday(self, day, width):
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
515 with TimeEncoding(self.locale) as encoding:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
516 if width >= 9:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
517 names = day_name
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
518 else:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
519 names = day_abbr
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
520 name = names[day]
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
521 if encoding is not None:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
522 name = name.decode(encoding)
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
523 return name[:width].center(width)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
524
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
525 def formatmonthname(self, theyear, themonth, width, withyear=True):
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
526 with TimeEncoding(self.locale) as encoding:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
527 s = month_name[themonth]
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
528 if encoding is not None:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
529 s = s.decode(encoding)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
530 if withyear:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
531 s = "%s %r" % (s, theyear)
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
532 return s.center(width)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
533
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
534
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
535 class LocaleHTMLCalendar(HTMLCalendar):
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
536 """
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
537 This class can be passed a locale name in the constructor and will return
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
538 month and weekday names in the specified locale. If this locale includes
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
539 an encoding all strings containing month and weekday names will be returned
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
540 as unicode.
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
541 """
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
542 def __init__(self, firstweekday=0, locale=None):
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
543 HTMLCalendar.__init__(self, firstweekday)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
544 if locale is None:
43822
c4e2936d1307 Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 38084
diff changeset
545 locale = _locale.getdefaultlocale()
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
546 self.locale = locale
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
547
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
548 def formatweekday(self, day):
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
549 with TimeEncoding(self.locale) as encoding:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
550 s = day_abbr[day]
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
551 if encoding is not None:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
552 s = s.decode(encoding)
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
553 return '<th class="%s">%s</th>' % (self.cssclasses[day], s)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
554
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
555 def formatmonthname(self, theyear, themonth, withyear=True):
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
556 with TimeEncoding(self.locale) as encoding:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
557 s = month_name[themonth]
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
558 if encoding is not None:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
559 s = s.decode(encoding)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
560 if withyear:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
561 s = '%s %s' % (s, theyear)
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
562 return '<tr><th colspan="7" class="month">%s</th></tr>' % s
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
563
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
564
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
565 # Support for old module level interface
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
566 c = TextCalendar()
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
567
37432
796acde70195 Turn firstweekday into a property.
Walter Dörwald <walter@livinglogic.de>
parents: 37405
diff changeset
568 firstweekday = c.getfirstweekday
37434
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
569
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
570 def setfirstweekday(firstweekday):
59431
e2d41079d059 Fix some py3k warnings in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 46613
diff changeset
571 try:
e2d41079d059 Fix some py3k warnings in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 46613
diff changeset
572 firstweekday.__index__
e2d41079d059 Fix some py3k warnings in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 46613
diff changeset
573 except AttributeError:
e2d41079d059 Fix some py3k warnings in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 46613
diff changeset
574 raise IllegalWeekdayError(firstweekday)
37434
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
575 if not MONDAY <= firstweekday <= SUNDAY:
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
576 raise IllegalWeekdayError(firstweekday)
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
577 c.firstweekday = firstweekday
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
578
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
579 monthcalendar = c.monthdayscalendar
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
580 prweek = c.prweek
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
581 week = c.formatweek
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
582 weekheader = c.formatweekheader
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
583 prmonth = c.prmonth
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
584 month = c.formatmonth
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
585 calendar = c.formatyear
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
586 prcal = c.pryear
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
587
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
588
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
589 # Spacing of month columns for multi-column year calendar
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
590 _colwidth = 7*3 - 1 # Amount printed by prweek()
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
591 _spacing = 6 # Number of spaces between columns
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
592
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
593
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
594 def format(cols, colwidth=_colwidth, spacing=_spacing):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
595 """Prints multi-column formatting for year calendars"""
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
596 print formatstring(cols, colwidth, spacing)
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
597
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
598
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
599 def formatstring(cols, colwidth=_colwidth, spacing=_spacing):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
600 """Returns a string formatted from n strings, centered within n columns."""
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
601 spacing *= ' '
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
602 return spacing.join(c.center(colwidth) for c in cols)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
603
10940
8cc0c898f6a7 Add unrelated but handy function: timegm(), to calculate Unix
Guido van Rossum <guido@python.org>
parents: 10819
diff changeset
604
8cc0c898f6a7 Add unrelated but handy function: timegm(), to calculate Unix
Guido van Rossum <guido@python.org>
parents: 10819
diff changeset
605 EPOCH = 1970
26793
db9388f37d8c SF 658405: calendar.py to rely on the datetime module instead of the time
Raymond Hettinger <python@rcn.com>
parents: 25879
diff changeset
606 _EPOCH_ORD = datetime.date(EPOCH, 1, 1).toordinal()
db9388f37d8c SF 658405: calendar.py to rely on the datetime module instead of the time
Raymond Hettinger <python@rcn.com>
parents: 25879
diff changeset
607
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
608
10940
8cc0c898f6a7 Add unrelated but handy function: timegm(), to calculate Unix
Guido van Rossum <guido@python.org>
parents: 10819
diff changeset
609 def timegm(tuple):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
610 """Unrelated but handy function to calculate Unix timestamp from GMT."""
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
611 year, month, day, hour, minute, second = tuple[:6]
27881
148d8c0b9647 SF 685011: calendar module overflow handling
Raymond Hettinger <python@rcn.com>
parents: 26793
diff changeset
612 days = datetime.date(year, month, 1).toordinal() - _EPOCH_ORD + day - 1
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
613 hours = days*24 + hour
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
614 minutes = hours*60 + minute
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
615 seconds = minutes*60 + second
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
616 return seconds
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
617
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
618
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
619 def main(args):
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
620 import optparse
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
621 parser = optparse.OptionParser(usage="usage: %prog [options] [year [month]]")
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
622 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
623 "-w", "--width",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
624 dest="width", type="int", default=2,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
625 help="width of date column (default 2, text only)"
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
626 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
627 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
628 "-l", "--lines",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
629 dest="lines", type="int", default=1,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
630 help="number of lines for each week (default 1, text only)"
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
631 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
632 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
633 "-s", "--spacing",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
634 dest="spacing", type="int", default=6,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
635 help="spacing between months (default 6, text only)"
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
636 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
637 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
638 "-m", "--months",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
639 dest="months", type="int", default=3,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
640 help="months per row (default 3, text only)"
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
641 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
642 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
643 "-c", "--css",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
644 dest="css", default="calendar.css",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
645 help="CSS to use for page (html only)"
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
646 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
647 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
648 "-L", "--locale",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
649 dest="locale", default=None,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
650 help="locale to be used from month and weekday names"
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
651 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
652 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
653 "-e", "--encoding",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
654 dest="encoding", default=None,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
655 help="Encoding to use for output"
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
656 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
657 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
658 "-t", "--type",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
659 dest="type", default="text",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
660 choices=("text", "html"),
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
661 help="output type (text or html)"
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
662 )
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
663
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
664 (options, args) = parser.parse_args(args)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
665
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
666 if options.locale and not options.encoding:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
667 parser.error("if --locale is specified --encoding is required")
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
668 sys.exit(1)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
669
43822
c4e2936d1307 Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 38084
diff changeset
670 locale = options.locale, options.encoding
c4e2936d1307 Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 38084
diff changeset
671
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
672 if options.type == "html":
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
673 if options.locale:
43822
c4e2936d1307 Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 38084
diff changeset
674 cal = LocaleHTMLCalendar(locale=locale)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
675 else:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
676 cal = HTMLCalendar()
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
677 encoding = options.encoding
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
678 if encoding is None:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
679 encoding = sys.getdefaultencoding()
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
680 optdict = dict(encoding=encoding, css=options.css)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
681 if len(args) == 1:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
682 print cal.formatyearpage(datetime.date.today().year, **optdict)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
683 elif len(args) == 2:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
684 print cal.formatyearpage(int(args[1]), **optdict)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
685 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
686 parser.error("incorrect number of arguments")
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
687 sys.exit(1)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
688 else:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
689 if options.locale:
43822
c4e2936d1307 Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 38084
diff changeset
690 cal = LocaleTextCalendar(locale=locale)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
691 else:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
692 cal = TextCalendar()
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
693 optdict = dict(w=options.width, l=options.lines)
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
694 if len(args) != 3:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
695 optdict["c"] = options.spacing
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
696 optdict["m"] = options.months
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
697 if len(args) == 1:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
698 result = cal.formatyear(datetime.date.today().year, **optdict)
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
699 elif len(args) == 2:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
700 result = cal.formatyear(int(args[1]), **optdict)
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
701 elif len(args) == 3:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
702 result = cal.formatmonth(int(args[1]), int(args[2]), **optdict)
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
703 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
704 parser.error("incorrect number of arguments")
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
705 sys.exit(1)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
706 if options.encoding:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
707 result = result.encode(options.encoding)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
708 print result
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
709
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
710
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
711 if __name__ == "__main__":
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
712 main(sys.argv)