annotate Lib/calendar.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 68cfc51e4331
children e2d41079d059
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
43821
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
diff changeset
8 import sys
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
diff changeset
9 import datetime
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
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):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
98 """Return 1 for leap years, 0 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
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
164 date += oneday
37434
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
165 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
166 break
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
167
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
168 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
169 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
170 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
171 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
172 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
173 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
174 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
175 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
176 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
177 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
178
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
179 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
180 """
45223
4e33fc82e7f3 Fix typo in docstring for Calendar.itermonthdays().
Walter Dörwald <walter@livinglogic.de>
parents: 43827
diff changeset
181 Like itermonthdates(), but will yield day numbers. For days outside
4e33fc82e7f3 Fix typo in docstring for Calendar.itermonthdays().
Walter Dörwald <walter@livinglogic.de>
parents: 43827
diff changeset
182 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
183 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
184 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
185 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
186 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
187 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
188 yield date.day
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
190 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
191 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
192 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
193 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
194 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
195 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
196 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
197
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
198 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
199 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
200 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
201 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
202 (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
203 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
204 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
205 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
206 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
207
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
208 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
209 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
210 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
211 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
212 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
213 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
214 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
215
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
216 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
217 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
218 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
219 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
220 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
221 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
222 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
223 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 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
225 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
226 ]
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
227 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
228
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
229 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
230 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
231 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
232 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
233 (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
234 zero.
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
235 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
236 months = [
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
237 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
238 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
239 ]
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
240 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
241
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
242 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
243 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
244 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
245 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
246 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
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 months = [
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
249 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
250 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
251 ]
41744
756e1d404cb8 calendar.py gets no benefit from xrange() instead of range()
Raymond Hettinger <python@rcn.com>
parents: 38084
diff changeset
252 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
253
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
254
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
255 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
256 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
257 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
258 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
259 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
260
37532
1583d3fdddcd missing 'self' from TextCalendar.prweek.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37434
diff changeset
261 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
262 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
263 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
264 """
43827
af3428fae15f Fix TextCalendar.prweek(). This closes issue #1427.
Walter Dörwald <walter@livinglogic.de>
parents: 43821
diff changeset
265 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
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 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
268 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
269 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
270 """
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
271 if day == 0:
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
272 s = ''
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
273 else:
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
274 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
275 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
276
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
277 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
278 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
279 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
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 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
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 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
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 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
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 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
288 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
289 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
290 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
291 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
292
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
293 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
294 """
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 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
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 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
298
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
299 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
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 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
302 """
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
303 s = month_name[themonth]
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
304 if withyear:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
305 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
306 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
307
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
308 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
309 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
310 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
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 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
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 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
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 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
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 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
319 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
320 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
321 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
322 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
323 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
324 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
325 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
326 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
327 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
328 return s
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
330 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
331 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
332 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
333 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
334 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
335 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
336 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
337 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
338 v = []
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
339 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
340 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
341 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
342 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
343 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
344 # 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
345 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
346 a('\n'*l)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
347 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
348 for k in months)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
349 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
350 a('\n'*l)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
351 headers = (header for k in months)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
352 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
353 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
354 # 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
355 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
356 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
357 weeks = []
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
358 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
359 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
360 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
361 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
362 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
363 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
364 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
365 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
366
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
367 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
368 """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
369 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
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
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
372 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
373 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
374 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
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
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
377 # 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
378 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
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 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
381 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
382 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
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 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
385 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
386 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
387 return '<td class="%s">%d</td>' % (self.cssclasses[weekday], day)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
388
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
389 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
390 """
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 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
392 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
393 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
394 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
395
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
396 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
397 """
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 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
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 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
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 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
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 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
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 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
407 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
408
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
409 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
410 """
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 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
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 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
414 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
415 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
416 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
417 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
418
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
419 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
420 """
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 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
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 v = []
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
424 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
425 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
426 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
427 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
428 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
429 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
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 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
432 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
433 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
434 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
435 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
436 return ''.join(v)
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
437
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
438 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
439 """
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 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
441 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
442 v = []
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
443 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
444 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
445 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
446 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
447 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
448 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
449 # 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
450 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
451 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
452 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
453 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
454 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
455 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
456 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
457 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
458 return ''.join(v)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
459
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
460 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
461 """
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 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
463 """
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
464 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
465 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
466 v = []
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
467 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
468 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
469 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
470 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
471 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
472 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
473 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
474 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
475 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
476 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
477 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
478 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
479 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
480 a('</html>\n')
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
481 return ''.join(v).encode(encoding, "xmlcharrefreplace")
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
482
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
483
37628
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
484 class TimeEncoding:
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
485 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
486 self.locale = locale
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
487
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
488 def __enter__(self):
43821
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
diff changeset
489 self.oldlocale = _locale.setlocale(_locale.LC_TIME, self.locale)
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
diff changeset
490 return _locale.getlocale(_locale.LC_TIME)[1]
37628
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 __exit__(self, *args):
43821
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
diff changeset
493 _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
494
0dd32a6b2964 Patch #1463288: use a context manager to temporarily switch locales.
Walter Dörwald <walter@livinglogic.de>
parents: 37532
diff changeset
495
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
496 class LocaleTextCalendar(TextCalendar):
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
497 """
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
498 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
499 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
500 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
501 as unicode.
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
502 """
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
503
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
504 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
505 TextCalendar.__init__(self, firstweekday)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
506 if locale is None:
43821
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
diff changeset
507 locale = _locale.getdefaultlocale()
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
508 self.locale = locale
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
509
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
510 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
511 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
512 if width >= 9:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
513 names = day_name
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
514 else:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
515 names = day_abbr
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
516 name = names[day]
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
517 if encoding is not None:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
518 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
519 return name[:width].center(width)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
520
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
521 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
522 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
523 s = month_name[themonth]
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
524 if encoding is not None:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
525 s = s.decode(encoding)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
526 if withyear:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
527 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
528 return s.center(width)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
529
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
530
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
531 class LocaleHTMLCalendar(HTMLCalendar):
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
532 """
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
533 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
534 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
535 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
536 as unicode.
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
537 """
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
538 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
539 HTMLCalendar.__init__(self, firstweekday)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
540 if locale is None:
43821
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
diff changeset
541 locale = _locale.getdefaultlocale()
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
542 self.locale = locale
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
543
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
544 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
545 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
546 s = day_abbr[day]
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
547 if encoding is not None:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
548 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
549 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
550
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
551 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
552 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
553 s = month_name[themonth]
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
554 if encoding is not None:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
555 s = s.decode(encoding)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
556 if withyear:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
557 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
558 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
559
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
560
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
561 # 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
562 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
563
37432
796acde70195 Turn firstweekday into a property.
Walter Dörwald <walter@livinglogic.de>
parents: 37405
diff changeset
564 firstweekday = c.getfirstweekday
37434
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
565
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
566 def setfirstweekday(firstweekday):
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
567 if not MONDAY <= firstweekday <= SUNDAY:
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
568 raise IllegalWeekdayError(firstweekday)
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
569 c.firstweekday = firstweekday
01fd093b878c For backwards compatibility reasons the global function
Walter Dörwald <walter@livinglogic.de>
parents: 37433
diff changeset
570
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
571 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
572 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
573 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
574 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
575 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
576 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
577 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
578 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
579
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
580
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
581 # 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
582 _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
583 _spacing = 6 # Number of spaces between columns
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
584
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
585
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
586 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
587 """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
588 print formatstring(cols, colwidth, spacing)
14261
adf3a284a2cf patches from David Goodger. Closes patch 101085.
Skip Montanaro <skip@pobox.com>
parents: 13050
diff changeset
589
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
590
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
591 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
592 """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
593 spacing *= ' '
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
594 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
595
10940
8cc0c898f6a7 Add unrelated but handy function: timegm(), to calculate Unix
Guido van Rossum <guido@python.org>
parents: 10819
diff changeset
596
8cc0c898f6a7 Add unrelated but handy function: timegm(), to calculate Unix
Guido van Rossum <guido@python.org>
parents: 10819
diff changeset
597 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
598 _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
599
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
600
10940
8cc0c898f6a7 Add unrelated but handy function: timegm(), to calculate Unix
Guido van Rossum <guido@python.org>
parents: 10819
diff changeset
601 def timegm(tuple):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
602 """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
603 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
604 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
605 hours = days*24 + hour
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
606 minutes = hours*60 + minute
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
607 seconds = minutes*60 + second
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10940
diff changeset
608 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
609
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
610
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
611 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
612 import optparse
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
613 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
614 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
615 "-w", "--width",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
616 dest="width", type="int", default=2,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
617 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
618 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
619 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
620 "-l", "--lines",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
621 dest="lines", type="int", default=1,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
622 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
623 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
624 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
625 "-s", "--spacing",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
626 dest="spacing", type="int", default=6,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
627 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
628 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
629 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
630 "-m", "--months",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
631 dest="months", type="int", default=3,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
632 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
633 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
634 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
635 "-c", "--css",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
636 dest="css", default="calendar.css",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
637 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
638 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
639 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
640 "-L", "--locale",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
641 dest="locale", default=None,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
642 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
643 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
644 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
645 "-e", "--encoding",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
646 dest="encoding", default=None,
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
647 help="Encoding to use for output"
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
648 )
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
649 parser.add_option(
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
650 "-t", "--type",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
651 dest="type", default="text",
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
652 choices=("text", "html"),
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
653 help="output type (text or html)"
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
654 )
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
655
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
656 (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
657
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
658 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
659 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
660 sys.exit(1)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
661
43821
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
diff changeset
662 locale = options.locale, options.encoding
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
diff changeset
663
37331
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
664 if options.type == "html":
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
665 if options.locale:
43821
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
diff changeset
666 cal = LocaleHTMLCalendar(locale=locale)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
667 else:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
668 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
669 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
670 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
671 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
672 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
673 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
674 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
675 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
676 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
677 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
678 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
679 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
680 else:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
681 if options.locale:
43821
01a5bdc2820a Fix for #1427: Error in standard module calendar
Christian Heimes <christian@cheimes.de>
parents: 42952
diff changeset
682 cal = LocaleTextCalendar(locale=locale)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
683 else:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
684 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
685 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
686 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
687 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
688 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
689 if len(args) == 1:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
690 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
691 elif len(args) == 2:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
692 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
693 elif len(args) == 3:
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
694 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
695 else:
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
696 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
697 sys.exit(1)
37379
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
698 if options.encoding:
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
699 result = result.encode(options.encoding)
2cce9bcdc9f6 Bug #947906: Add classes LocaleTextCalendar and LocaleHTMLCalendar,
Walter Dörwald <walter@livinglogic.de>
parents: 37331
diff changeset
700 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
701
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
702
aac005e8bb80 Bug #947906: An object oriented interface has been added to the calendar
Walter Dörwald <walter@livinglogic.de>
parents: 34947
diff changeset
703 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
704 main(sys.argv)