2000-08-30 22:01:28 +08:00
|
|
|
"""Calendar printing functions
|
|
|
|
|
|
|
|
Note when comparing these calendars to the ones printed by cal(1): By
|
|
|
|
default, these calendars have Monday as the first day of the week, and
|
|
|
|
Sunday as the last (the European convention). Use setfirstweekday() to
|
|
|
|
set the first day of the week (0=Monday, 6=Sunday)."""
|
1990-10-14 03:23:40 +08:00
|
|
|
|
2007-11-12 09:32:03 +08:00
|
|
|
import sys
|
|
|
|
import datetime
|
|
|
|
import locale as _locale
|
2016-09-28 08:26:39 +08:00
|
|
|
from itertools import repeat
|
1990-10-14 03:23:40 +08:00
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
__all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
|
|
|
|
"firstweekday", "isleap", "leapdays", "weekday", "monthrange",
|
|
|
|
"monthcalendar", "prmonth", "month", "prcal", "calendar",
|
2016-01-16 14:49:30 +08:00
|
|
|
"timegm", "month_name", "month_abbr", "day_name", "day_abbr",
|
|
|
|
"Calendar", "TextCalendar", "HTMLCalendar", "LocaleTextCalendar",
|
2022-01-06 04:21:04 +08:00
|
|
|
"LocaleHTMLCalendar", "weekheader",
|
|
|
|
"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY",
|
|
|
|
"SATURDAY", "SUNDAY"]
|
2001-01-21 03:54:20 +08:00
|
|
|
|
1993-06-21 05:02:22 +08:00
|
|
|
# Exception raised for bad input (with string parameter for details)
|
1999-05-04 02:07:40 +08:00
|
|
|
error = ValueError
|
1993-06-17 20:38:10 +08:00
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
# Exceptions raised for bad input
|
|
|
|
class IllegalMonthError(ValueError):
|
|
|
|
def __init__(self, month):
|
|
|
|
self.month = month
|
|
|
|
def __str__(self):
|
|
|
|
return "bad month number %r; must be 1-12" % self.month
|
|
|
|
|
|
|
|
|
|
|
|
class IllegalWeekdayError(ValueError):
|
|
|
|
def __init__(self, weekday):
|
|
|
|
self.weekday = weekday
|
|
|
|
def __str__(self):
|
|
|
|
return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self.weekday
|
|
|
|
|
|
|
|
|
1990-10-14 03:23:40 +08:00
|
|
|
# Constants for months referenced later
|
|
|
|
January = 1
|
|
|
|
February = 2
|
|
|
|
|
|
|
|
# Number of days per month (except for February in leap years)
|
1992-07-09 19:05:12 +08:00
|
|
|
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
1990-10-14 03:23:40 +08:00
|
|
|
|
2002-03-23 11:26:53 +08:00
|
|
|
# This module used to have hard-coded lists of day and month names, as
|
|
|
|
# English strings. The classes following emulate a read-only version of
|
|
|
|
# that, but supply localized names. Note that the values are computed
|
|
|
|
# fresh on each call, in case the user changes locale between calls.
|
|
|
|
|
2002-06-20 11:38:12 +08:00
|
|
|
class _localized_month:
|
2004-11-14 00:18:32 +08:00
|
|
|
|
2007-05-08 06:24:25 +08:00
|
|
|
_months = [datetime.date(2001, i+1, 1).strftime for i in range(12)]
|
2004-11-14 00:18:32 +08:00
|
|
|
_months.insert(0, lambda x: "")
|
|
|
|
|
2002-03-23 11:26:53 +08:00
|
|
|
def __init__(self, format):
|
2001-05-22 23:58:30 +08:00
|
|
|
self.format = format
|
2002-03-23 11:26:53 +08:00
|
|
|
|
|
|
|
def __getitem__(self, i):
|
2004-11-14 00:18:32 +08:00
|
|
|
funcs = self._months[i]
|
|
|
|
if isinstance(i, slice):
|
|
|
|
return [f(self.format) for f in funcs]
|
|
|
|
else:
|
|
|
|
return funcs(self.format)
|
2002-03-23 11:26:53 +08:00
|
|
|
|
2002-03-15 12:08:38 +08:00
|
|
|
def __len__(self):
|
2002-03-23 11:26:53 +08:00
|
|
|
return 13
|
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
|
2002-06-20 11:38:12 +08:00
|
|
|
class _localized_day:
|
2004-11-14 00:18:32 +08:00
|
|
|
|
|
|
|
# January 1, 2001, was a Monday.
|
2007-05-08 06:24:25 +08:00
|
|
|
_days = [datetime.date(2001, 1, i+1).strftime for i in range(7)]
|
2004-11-14 00:18:32 +08:00
|
|
|
|
2002-03-23 11:26:53 +08:00
|
|
|
def __init__(self, format):
|
|
|
|
self.format = format
|
|
|
|
|
|
|
|
def __getitem__(self, i):
|
2004-11-14 00:18:32 +08:00
|
|
|
funcs = self._days[i]
|
|
|
|
if isinstance(i, slice):
|
|
|
|
return [f(self.format) for f in funcs]
|
|
|
|
else:
|
|
|
|
return funcs(self.format)
|
2002-03-23 11:26:53 +08:00
|
|
|
|
2004-06-07 11:47:06 +08:00
|
|
|
def __len__(self):
|
2002-03-23 11:26:53 +08:00
|
|
|
return 7
|
2001-05-22 23:58:30 +08:00
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
|
1990-10-14 03:23:40 +08:00
|
|
|
# Full and abbreviated names of weekdays
|
2002-03-23 11:26:53 +08:00
|
|
|
day_name = _localized_day('%A')
|
|
|
|
day_abbr = _localized_day('%a')
|
1990-10-14 03:23:40 +08:00
|
|
|
|
1993-06-23 17:30:50 +08:00
|
|
|
# Full and abbreviated names of months (1-based arrays!!!)
|
2002-03-23 11:26:53 +08:00
|
|
|
month_name = _localized_month('%B')
|
|
|
|
month_abbr = _localized_month('%b')
|
1990-10-14 03:23:40 +08:00
|
|
|
|
2000-08-30 22:01:28 +08:00
|
|
|
# Constants for weekdays
|
|
|
|
(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
|
|
|
|
|
|
|
|
|
1993-06-21 05:02:22 +08:00
|
|
|
def isleap(year):
|
2010-10-20 01:43:50 +08:00
|
|
|
"""Return True for leap years, False for non-leap years."""
|
2000-12-13 07:20:45 +08:00
|
|
|
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
|
1990-10-14 03:23:40 +08:00
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
|
1993-06-21 05:02:22 +08:00
|
|
|
def leapdays(y1, y2):
|
2000-02-02 23:10:15 +08:00
|
|
|
"""Return number of leap years in range [y1, y2).
|
2000-10-09 20:42:04 +08:00
|
|
|
Assume y1 <= y2."""
|
|
|
|
y1 -= 1
|
|
|
|
y2 -= 1
|
2002-12-26 00:37:19 +08:00
|
|
|
return (y2//4 - y1//4) - (y2//100 - y1//100) + (y2//400 - y1//400)
|
1990-10-14 03:23:40 +08:00
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
|
1990-10-14 03:23:40 +08:00
|
|
|
def weekday(year, month, day):
|
2017-10-27 03:34:11 +08:00
|
|
|
"""Return weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31)."""
|
|
|
|
if not datetime.MINYEAR <= year <= datetime.MAXYEAR:
|
|
|
|
year = 2000 + year % 400
|
2002-12-26 00:37:19 +08:00
|
|
|
return datetime.date(year, month, day).weekday()
|
1990-10-14 03:23:40 +08:00
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
|
1990-10-14 03:23:40 +08:00
|
|
|
def monthrange(year, month):
|
2000-08-30 22:01:28 +08:00
|
|
|
"""Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
|
|
|
|
year, month."""
|
|
|
|
if not 1 <= month <= 12:
|
2006-04-21 18:40:58 +08:00
|
|
|
raise IllegalMonthError(month)
|
2000-02-02 23:10:15 +08:00
|
|
|
day1 = weekday(year, month, 1)
|
|
|
|
ndays = mdays[month] + (month == February and isleap(year))
|
|
|
|
return day1, ndays
|
1990-10-14 03:23:40 +08:00
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
|
2019-08-05 04:14:03 +08:00
|
|
|
def _monthlen(year, month):
|
2017-10-25 01:17:10 +08:00
|
|
|
return mdays[month] + (month == February and isleap(year))
|
|
|
|
|
|
|
|
|
2019-08-05 04:14:03 +08:00
|
|
|
def _prevmonth(year, month):
|
2017-10-25 01:17:10 +08:00
|
|
|
if month == 1:
|
|
|
|
return year-1, 12
|
|
|
|
else:
|
|
|
|
return year, month-1
|
|
|
|
|
|
|
|
|
2019-08-05 04:14:03 +08:00
|
|
|
def _nextmonth(year, month):
|
2017-10-25 01:17:10 +08:00
|
|
|
if month == 12:
|
|
|
|
return year+1, 1
|
|
|
|
else:
|
|
|
|
return year, month+1
|
|
|
|
|
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
class Calendar(object):
|
|
|
|
"""
|
|
|
|
Base calendar class. This class doesn't do any formatting. It simply
|
|
|
|
provides data to subclasses.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, firstweekday=0):
|
|
|
|
self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday
|
|
|
|
|
|
|
|
def getfirstweekday(self):
|
|
|
|
return self._firstweekday % 7
|
|
|
|
|
|
|
|
def setfirstweekday(self, firstweekday):
|
|
|
|
self._firstweekday = firstweekday
|
|
|
|
|
|
|
|
firstweekday = property(getfirstweekday, setfirstweekday)
|
|
|
|
|
|
|
|
def iterweekdays(self):
|
|
|
|
"""
|
2015-11-02 11:37:02 +08:00
|
|
|
Return an iterator for one week of weekday numbers starting with the
|
2006-04-21 18:40:58 +08:00
|
|
|
configured first one.
|
|
|
|
"""
|
2007-05-08 06:24:25 +08:00
|
|
|
for i in range(self.firstweekday, self.firstweekday + 7):
|
2006-04-21 18:40:58 +08:00
|
|
|
yield i%7
|
|
|
|
|
|
|
|
def itermonthdates(self, year, month):
|
|
|
|
"""
|
|
|
|
Return an iterator for one month. The iterator will yield datetime.date
|
|
|
|
values and will always iterate through complete weeks, so it will yield
|
|
|
|
dates outside the specified month.
|
|
|
|
"""
|
2017-10-25 01:17:10 +08:00
|
|
|
for y, m, d in self.itermonthdays3(year, month):
|
|
|
|
yield datetime.date(y, m, d)
|
|
|
|
|
|
|
|
def itermonthdays(self, year, month):
|
|
|
|
"""
|
|
|
|
Like itermonthdates(), but will yield day numbers. For days outside
|
|
|
|
the specified month the day number is 0.
|
|
|
|
"""
|
|
|
|
day1, ndays = monthrange(year, month)
|
|
|
|
days_before = (day1 - self.firstweekday) % 7
|
|
|
|
yield from repeat(0, days_before)
|
|
|
|
yield from range(1, ndays + 1)
|
|
|
|
days_after = (self.firstweekday - day1 - ndays) % 7
|
|
|
|
yield from repeat(0, days_after)
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def itermonthdays2(self, year, month):
|
|
|
|
"""
|
|
|
|
Like itermonthdates(), but will yield (day number, weekday number)
|
|
|
|
tuples. For days outside the specified month the day number is 0.
|
|
|
|
"""
|
2016-09-28 08:26:39 +08:00
|
|
|
for i, d in enumerate(self.itermonthdays(year, month), self.firstweekday):
|
|
|
|
yield d, i % 7
|
2006-04-21 18:40:58 +08:00
|
|
|
|
2017-10-25 01:17:10 +08:00
|
|
|
def itermonthdays3(self, year, month):
|
2006-04-21 18:40:58 +08:00
|
|
|
"""
|
2017-10-25 01:17:10 +08:00
|
|
|
Like itermonthdates(), but will yield (year, month, day) tuples. Can be
|
|
|
|
used for dates outside of datetime.date range.
|
2006-04-21 18:40:58 +08:00
|
|
|
"""
|
2016-09-28 08:26:39 +08:00
|
|
|
day1, ndays = monthrange(year, month)
|
|
|
|
days_before = (day1 - self.firstweekday) % 7
|
|
|
|
days_after = (self.firstweekday - day1 - ndays) % 7
|
2019-08-05 04:14:03 +08:00
|
|
|
y, m = _prevmonth(year, month)
|
|
|
|
end = _monthlen(y, m) + 1
|
2017-10-25 01:17:10 +08:00
|
|
|
for d in range(end-days_before, end):
|
|
|
|
yield y, m, d
|
|
|
|
for d in range(1, ndays + 1):
|
|
|
|
yield year, month, d
|
2019-08-05 04:14:03 +08:00
|
|
|
y, m = _nextmonth(year, month)
|
2017-10-25 01:17:10 +08:00
|
|
|
for d in range(1, days_after + 1):
|
|
|
|
yield y, m, d
|
|
|
|
|
|
|
|
def itermonthdays4(self, year, month):
|
|
|
|
"""
|
|
|
|
Like itermonthdates(), but will yield (year, month, day, day_of_week) tuples.
|
|
|
|
Can be used for dates outside of datetime.date range.
|
|
|
|
"""
|
|
|
|
for i, (y, m, d) in enumerate(self.itermonthdays3(year, month)):
|
|
|
|
yield y, m, d, (self.firstweekday + i) % 7
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def monthdatescalendar(self, year, month):
|
|
|
|
"""
|
|
|
|
Return a matrix (list of lists) representing a month's calendar.
|
|
|
|
Each row represents a week; week entries are datetime.date values.
|
|
|
|
"""
|
|
|
|
dates = list(self.itermonthdates(year, month))
|
2007-05-08 06:24:25 +08:00
|
|
|
return [ dates[i:i+7] for i in range(0, len(dates), 7) ]
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def monthdays2calendar(self, year, month):
|
|
|
|
"""
|
|
|
|
Return a matrix representing a month's calendar.
|
|
|
|
Each row represents a week; week entries are
|
|
|
|
(day number, weekday number) tuples. Day numbers outside this month
|
|
|
|
are zero.
|
|
|
|
"""
|
|
|
|
days = list(self.itermonthdays2(year, month))
|
2007-05-08 06:24:25 +08:00
|
|
|
return [ days[i:i+7] for i in range(0, len(days), 7) ]
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def monthdayscalendar(self, year, month):
|
|
|
|
"""
|
|
|
|
Return a matrix representing a month's calendar.
|
|
|
|
Each row represents a week; days outside this month are zero.
|
|
|
|
"""
|
|
|
|
days = list(self.itermonthdays(year, month))
|
2007-05-08 06:24:25 +08:00
|
|
|
return [ days[i:i+7] for i in range(0, len(days), 7) ]
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def yeardatescalendar(self, year, width=3):
|
|
|
|
"""
|
|
|
|
Return the data for the specified year ready for formatting. The return
|
2013-08-17 20:50:46 +08:00
|
|
|
value is a list of month rows. Each month row contains up to width months.
|
2006-04-21 18:40:58 +08:00
|
|
|
Each month contains between 4 and 6 weeks and each week contains 1-7
|
|
|
|
days. Days are datetime.date objects.
|
|
|
|
"""
|
|
|
|
months = [
|
|
|
|
self.monthdatescalendar(year, i)
|
2007-05-08 06:24:25 +08:00
|
|
|
for i in range(January, January+12)
|
2006-04-21 18:40:58 +08:00
|
|
|
]
|
2007-05-08 06:24:25 +08:00
|
|
|
return [months[i:i+width] for i in range(0, len(months), width) ]
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def yeardays2calendar(self, year, width=3):
|
|
|
|
"""
|
|
|
|
Return the data for the specified year ready for formatting (similar to
|
|
|
|
yeardatescalendar()). Entries in the week lists are
|
|
|
|
(day number, weekday number) tuples. Day numbers outside this month are
|
|
|
|
zero.
|
|
|
|
"""
|
|
|
|
months = [
|
|
|
|
self.monthdays2calendar(year, i)
|
2007-05-08 06:24:25 +08:00
|
|
|
for i in range(January, January+12)
|
2006-04-21 18:40:58 +08:00
|
|
|
]
|
2007-05-08 06:24:25 +08:00
|
|
|
return [months[i:i+width] for i in range(0, len(months), width) ]
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def yeardayscalendar(self, year, width=3):
|
|
|
|
"""
|
|
|
|
Return the data for the specified year ready for formatting (similar to
|
|
|
|
yeardatescalendar()). Entries in the week lists are day numbers.
|
|
|
|
Day numbers outside this month are zero.
|
|
|
|
"""
|
|
|
|
months = [
|
|
|
|
self.monthdayscalendar(year, i)
|
2007-05-08 06:24:25 +08:00
|
|
|
for i in range(January, January+12)
|
2006-04-21 18:40:58 +08:00
|
|
|
]
|
2007-05-08 06:24:25 +08:00
|
|
|
return [months[i:i+width] for i in range(0, len(months), width) ]
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TextCalendar(Calendar):
|
|
|
|
"""
|
|
|
|
Subclass of Calendar that outputs a calendar as a simple plain text
|
|
|
|
similar to the UNIX program cal.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def prweek(self, theweek, width):
|
|
|
|
"""
|
|
|
|
Print a single week (no newline).
|
|
|
|
"""
|
2016-10-25 20:20:58 +08:00
|
|
|
print(self.formatweek(theweek, width), end='')
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def formatday(self, day, weekday, width):
|
|
|
|
"""
|
|
|
|
Returns a formatted day.
|
|
|
|
"""
|
2000-08-30 22:01:28 +08:00
|
|
|
if day == 0:
|
|
|
|
s = ''
|
|
|
|
else:
|
|
|
|
s = '%2i' % day # right-align single-digit days
|
2006-04-21 18:40:58 +08:00
|
|
|
return s.center(width)
|
|
|
|
|
|
|
|
def formatweek(self, theweek, width):
|
|
|
|
"""
|
|
|
|
Returns a single week in a string (no newline).
|
|
|
|
"""
|
|
|
|
return ' '.join(self.formatday(d, wd, width) for (d, wd) in theweek)
|
|
|
|
|
|
|
|
def formatweekday(self, day, width):
|
|
|
|
"""
|
|
|
|
Returns a formatted week day name.
|
|
|
|
"""
|
|
|
|
if width >= 9:
|
|
|
|
names = day_name
|
|
|
|
else:
|
|
|
|
names = day_abbr
|
|
|
|
return names[day][:width].center(width)
|
|
|
|
|
|
|
|
def formatweekheader(self, width):
|
|
|
|
"""
|
|
|
|
Return a header for a week.
|
|
|
|
"""
|
|
|
|
return ' '.join(self.formatweekday(i, width) for i in self.iterweekdays())
|
|
|
|
|
|
|
|
def formatmonthname(self, theyear, themonth, width, withyear=True):
|
|
|
|
"""
|
|
|
|
Return a formatted month name.
|
|
|
|
"""
|
|
|
|
s = month_name[themonth]
|
|
|
|
if withyear:
|
|
|
|
s = "%s %r" % (s, theyear)
|
|
|
|
return s.center(width)
|
|
|
|
|
|
|
|
def prmonth(self, theyear, themonth, w=0, l=0):
|
|
|
|
"""
|
|
|
|
Print a month's calendar.
|
|
|
|
"""
|
2016-10-25 20:00:52 +08:00
|
|
|
print(self.formatmonth(theyear, themonth, w, l), end='')
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def formatmonth(self, theyear, themonth, w=0, l=0):
|
|
|
|
"""
|
|
|
|
Return a month's calendar string (multi-line).
|
|
|
|
"""
|
|
|
|
w = max(2, w)
|
|
|
|
l = max(1, l)
|
|
|
|
s = self.formatmonthname(theyear, themonth, 7 * (w + 1) - 1)
|
|
|
|
s = s.rstrip()
|
|
|
|
s += '\n' * l
|
|
|
|
s += self.formatweekheader(w).rstrip()
|
|
|
|
s += '\n' * l
|
|
|
|
for week in self.monthdays2calendar(theyear, themonth):
|
|
|
|
s += self.formatweek(week, w).rstrip()
|
|
|
|
s += '\n' * l
|
|
|
|
return s
|
|
|
|
|
|
|
|
def formatyear(self, theyear, w=2, l=1, c=6, m=3):
|
|
|
|
"""
|
|
|
|
Returns a year's calendar as a multi-line string.
|
|
|
|
"""
|
|
|
|
w = max(2, w)
|
|
|
|
l = max(1, l)
|
|
|
|
c = max(2, c)
|
|
|
|
colwidth = (w + 1) * 7 - 1
|
|
|
|
v = []
|
|
|
|
a = v.append
|
|
|
|
a(repr(theyear).center(colwidth*m+c*(m-1)).rstrip())
|
|
|
|
a('\n'*l)
|
|
|
|
header = self.formatweekheader(w)
|
|
|
|
for (i, row) in enumerate(self.yeardays2calendar(theyear, m)):
|
|
|
|
# months in this row
|
2007-05-08 06:24:25 +08:00
|
|
|
months = range(m*i+1, min(m*(i+1)+1, 13))
|
2006-04-21 18:40:58 +08:00
|
|
|
a('\n'*l)
|
|
|
|
names = (self.formatmonthname(theyear, k, colwidth, False)
|
|
|
|
for k in months)
|
|
|
|
a(formatstring(names, colwidth, c).rstrip())
|
|
|
|
a('\n'*l)
|
|
|
|
headers = (header for k in months)
|
|
|
|
a(formatstring(headers, colwidth, c).rstrip())
|
|
|
|
a('\n'*l)
|
|
|
|
# max number of weeks for this row
|
|
|
|
height = max(len(cal) for cal in row)
|
2007-05-08 06:24:25 +08:00
|
|
|
for j in range(height):
|
2006-04-21 18:40:58 +08:00
|
|
|
weeks = []
|
|
|
|
for cal in row:
|
|
|
|
if j >= len(cal):
|
|
|
|
weeks.append('')
|
|
|
|
else:
|
|
|
|
weeks.append(self.formatweek(cal[j], w))
|
|
|
|
a(formatstring(weeks, colwidth, c).rstrip())
|
|
|
|
a('\n' * l)
|
|
|
|
return ''.join(v)
|
|
|
|
|
|
|
|
def pryear(self, theyear, w=0, l=0, c=6, m=3):
|
|
|
|
"""Print a year's calendar."""
|
2016-10-25 20:20:58 +08:00
|
|
|
print(self.formatyear(theyear, w, l, c, m), end='')
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
class HTMLCalendar(Calendar):
|
|
|
|
"""
|
|
|
|
This calendar returns complete HTML pages.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# CSS classes for the day <td>s
|
|
|
|
cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
|
|
|
|
|
2017-06-06 17:35:59 +08:00
|
|
|
# CSS classes for the day <th>s
|
|
|
|
cssclasses_weekday_head = cssclasses
|
|
|
|
|
|
|
|
# CSS class for the days before and after current month
|
|
|
|
cssclass_noday = "noday"
|
|
|
|
|
|
|
|
# CSS class for the month's head
|
|
|
|
cssclass_month_head = "month"
|
|
|
|
|
|
|
|
# CSS class for the month
|
|
|
|
cssclass_month = "month"
|
|
|
|
|
|
|
|
# CSS class for the year's table head
|
|
|
|
cssclass_year_head = "year"
|
|
|
|
|
|
|
|
# CSS class for the whole year table
|
|
|
|
cssclass_year = "year"
|
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
def formatday(self, day, weekday):
|
|
|
|
"""
|
|
|
|
Return a day as a table cell.
|
|
|
|
"""
|
|
|
|
if day == 0:
|
2017-06-06 17:35:59 +08:00
|
|
|
# day outside month
|
|
|
|
return '<td class="%s"> </td>' % self.cssclass_noday
|
2006-04-21 18:40:58 +08:00
|
|
|
else:
|
|
|
|
return '<td class="%s">%d</td>' % (self.cssclasses[weekday], day)
|
|
|
|
|
|
|
|
def formatweek(self, theweek):
|
|
|
|
"""
|
|
|
|
Return a complete week as a table row.
|
|
|
|
"""
|
|
|
|
s = ''.join(self.formatday(d, wd) for (d, wd) in theweek)
|
|
|
|
return '<tr>%s</tr>' % s
|
|
|
|
|
|
|
|
def formatweekday(self, day):
|
|
|
|
"""
|
|
|
|
Return a weekday name as a table header.
|
|
|
|
"""
|
2017-06-06 17:35:59 +08:00
|
|
|
return '<th class="%s">%s</th>' % (
|
|
|
|
self.cssclasses_weekday_head[day], day_abbr[day])
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def formatweekheader(self):
|
|
|
|
"""
|
|
|
|
Return a header for a week as a table row.
|
|
|
|
"""
|
|
|
|
s = ''.join(self.formatweekday(i) for i in self.iterweekdays())
|
|
|
|
return '<tr>%s</tr>' % s
|
|
|
|
|
|
|
|
def formatmonthname(self, theyear, themonth, withyear=True):
|
|
|
|
"""
|
|
|
|
Return a month name as a table row.
|
|
|
|
"""
|
|
|
|
if withyear:
|
|
|
|
s = '%s %s' % (month_name[themonth], theyear)
|
|
|
|
else:
|
|
|
|
s = '%s' % month_name[themonth]
|
2017-06-06 17:35:59 +08:00
|
|
|
return '<tr><th colspan="7" class="%s">%s</th></tr>' % (
|
|
|
|
self.cssclass_month_head, s)
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def formatmonth(self, theyear, themonth, withyear=True):
|
|
|
|
"""
|
|
|
|
Return a formatted month as a table.
|
|
|
|
"""
|
|
|
|
v = []
|
|
|
|
a = v.append
|
2017-06-06 17:35:59 +08:00
|
|
|
a('<table border="0" cellpadding="0" cellspacing="0" class="%s">' % (
|
|
|
|
self.cssclass_month))
|
2006-04-21 18:40:58 +08:00
|
|
|
a('\n')
|
|
|
|
a(self.formatmonthname(theyear, themonth, withyear=withyear))
|
|
|
|
a('\n')
|
|
|
|
a(self.formatweekheader())
|
|
|
|
a('\n')
|
|
|
|
for week in self.monthdays2calendar(theyear, themonth):
|
|
|
|
a(self.formatweek(week))
|
|
|
|
a('\n')
|
|
|
|
a('</table>')
|
|
|
|
a('\n')
|
|
|
|
return ''.join(v)
|
|
|
|
|
|
|
|
def formatyear(self, theyear, width=3):
|
|
|
|
"""
|
|
|
|
Return a formatted year as a table of tables.
|
|
|
|
"""
|
|
|
|
v = []
|
|
|
|
a = v.append
|
|
|
|
width = max(width, 1)
|
2017-06-06 17:35:59 +08:00
|
|
|
a('<table border="0" cellpadding="0" cellspacing="0" class="%s">' %
|
|
|
|
self.cssclass_year)
|
2006-04-21 18:40:58 +08:00
|
|
|
a('\n')
|
2017-06-06 17:35:59 +08:00
|
|
|
a('<tr><th colspan="%d" class="%s">%s</th></tr>' % (
|
|
|
|
width, self.cssclass_year_head, theyear))
|
2007-05-08 06:24:25 +08:00
|
|
|
for i in range(January, January+12, width):
|
2006-04-21 18:40:58 +08:00
|
|
|
# months in this row
|
2007-05-08 06:24:25 +08:00
|
|
|
months = range(i, min(i+width, 13))
|
2006-04-21 18:40:58 +08:00
|
|
|
a('<tr>')
|
|
|
|
for m in months:
|
|
|
|
a('<td>')
|
|
|
|
a(self.formatmonth(theyear, m, withyear=False))
|
|
|
|
a('</td>')
|
|
|
|
a('</tr>')
|
|
|
|
a('</table>')
|
|
|
|
return ''.join(v)
|
|
|
|
|
|
|
|
def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None):
|
|
|
|
"""
|
|
|
|
Return a formatted year as a complete HTML page.
|
|
|
|
"""
|
|
|
|
if encoding is None:
|
|
|
|
encoding = sys.getdefaultencoding()
|
|
|
|
v = []
|
|
|
|
a = v.append
|
|
|
|
a('<?xml version="1.0" encoding="%s"?>\n' % encoding)
|
|
|
|
a('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n')
|
|
|
|
a('<html>\n')
|
|
|
|
a('<head>\n')
|
|
|
|
a('<meta http-equiv="Content-Type" content="text/html; charset=%s" />\n' % encoding)
|
|
|
|
if css is not None:
|
|
|
|
a('<link rel="stylesheet" type="text/css" href="%s" />\n' % css)
|
Merged revisions 57620-57771 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r57771 | thomas.wouters | 2007-08-30 23:54:39 +0200 (Thu, 30 Aug 2007) | 5 lines
Don't lie in __all__ attributes when SSL is not available: only add the SSL
classes when they are actually created.
........
r57620 | walter.doerwald | 2007-08-28 18:38:26 +0200 (Tue, 28 Aug 2007) | 5 lines
Fix title endtag in HTMLCalender.formatyearpage(). Fix documentation for
HTMLCalender.formatyearpage() (there's no themonth parameter).
This fixes issue1046.
........
r57622 | georg.brandl | 2007-08-28 20:54:44 +0200 (Tue, 28 Aug 2007) | 2 lines
Add a crasher for the thread-unsafety of file objects.
........
r57626 | skip.montanaro | 2007-08-29 01:22:52 +0200 (Wed, 29 Aug 2007) | 1 line
fixes 813986
........
r57628 | walter.doerwald | 2007-08-29 01:35:33 +0200 (Wed, 29 Aug 2007) | 2 lines
Fix test output.
........
r57631 | skip.montanaro | 2007-08-29 03:24:11 +0200 (Wed, 29 Aug 2007) | 2 lines
Install pygettext (once the scriptsinstall target is working again).
........
r57633 | skip.montanaro | 2007-08-29 03:33:45 +0200 (Wed, 29 Aug 2007) | 2 lines
Recent items.
........
r57650 | neal.norwitz | 2007-08-29 08:15:33 +0200 (Wed, 29 Aug 2007) | 1 line
Add Bill as a developer
........
r57651 | facundo.batista | 2007-08-29 12:28:28 +0200 (Wed, 29 Aug 2007) | 5 lines
Ignore test failures caused by 'resource temporarily unavailable'
exceptions raised during FailingServerTestCase tests.
[GSoC - Alan McIntyre]
........
r57680 | bill.janssen | 2007-08-30 00:35:05 +0200 (Thu, 30 Aug 2007) | 17 lines
This contains a number of things:
1) Improve the documentation of the SSL module, with a fuller
explanation of certificate usage, another reference, proper
formatting of this and that.
2) Fix Windows bug in ssl.py, and general bug in sslsocket.close().
Remove some unused code from ssl.py. Allow accept() to be called on
sslsocket sockets.
3) Use try-except-else in import of ssl in socket.py. Deprecate use of
socket.ssl().
4) Remove use of socket.ssl() in every library module, except for
test_socket_ssl.py and test_ssl.py.
........
r57714 | georg.brandl | 2007-08-30 12:09:42 +0200 (Thu, 30 Aug 2007) | 2 lines
Stronger urge to convert filenames to str before using them as argument to ZipFile.write().
........
r57716 | georg.brandl | 2007-08-30 12:38:56 +0200 (Thu, 30 Aug 2007) | 2 lines
Patch #1680959: add test suite for pipes module.
........
r57717 | georg.brandl | 2007-08-30 14:32:23 +0200 (Thu, 30 Aug 2007) | 3 lines
* Skip test_pipes on non-POSIX.
* Don't raise TestSkipped within a test function.
........
r57723 | mark.summerfield | 2007-08-30 17:03:03 +0200 (Thu, 30 Aug 2007) | 3 lines
Added more cross-references.
........
r57726 | walter.doerwald | 2007-08-30 17:30:09 +0200 (Thu, 30 Aug 2007) | 2 lines
Rewrap line.
........
r57727 | walter.doerwald | 2007-08-30 17:34:55 +0200 (Thu, 30 Aug 2007) | 2 lines
Set startinpos before calling the error handler.
........
r57730 | bill.janssen | 2007-08-30 19:07:28 +0200 (Thu, 30 Aug 2007) | 3 lines
Added docstrings to methods and functions.
........
r57743 | bill.janssen | 2007-08-30 20:08:06 +0200 (Thu, 30 Aug 2007) | 1 line
added note on new ssl module and deprecation of socket.ssl
........
r57747 | martin.v.loewis | 2007-08-30 20:14:01 +0200 (Thu, 30 Aug 2007) | 1 line
Fix popen usage.
........
r57748 | martin.v.loewis | 2007-08-30 20:15:22 +0200 (Thu, 30 Aug 2007) | 1 line
Fix typo.
........
r57750 | martin.v.loewis | 2007-08-30 20:25:47 +0200 (Thu, 30 Aug 2007) | 1 line
Bug #1746880: Correctly install DLLs into system32 folder on Win64.
........
r57760 | martin.v.loewis | 2007-08-30 21:04:09 +0200 (Thu, 30 Aug 2007) | 1 line
Bug #1709599: Run test_1565150 only if the file system is NTFS.
........
r57762 | martin.v.loewis | 2007-08-30 22:10:57 +0200 (Thu, 30 Aug 2007) | 2 lines
Bump autoconf minimum version to 2.61.
........
r57764 | lars.gustaebel | 2007-08-30 22:24:31 +0200 (Thu, 30 Aug 2007) | 2 lines
Warn about possible risks when extracting untrusted archives.
........
r57769 | thomas.wouters | 2007-08-30 23:01:17 +0200 (Thu, 30 Aug 2007) | 7 lines
Somewhat-preliminary slice-object and extended slicing support for ctypes.
The exact behaviour of omitted and negative indices for the Pointer type may
need a closer look (especially as it's subtly different from simple slices)
but there's time yet before 2.6, and not enough before 3.0a1 :-)
........
2007-08-31 06:15:33 +08:00
|
|
|
a('<title>Calendar for %d</title>\n' % theyear)
|
2006-04-21 18:40:58 +08:00
|
|
|
a('</head>\n')
|
|
|
|
a('<body>\n')
|
|
|
|
a(self.formatyear(theyear, width))
|
|
|
|
a('</body>\n')
|
|
|
|
a('</html>\n')
|
|
|
|
return ''.join(v).encode(encoding, "xmlcharrefreplace")
|
|
|
|
|
|
|
|
|
2008-06-08 16:40:05 +08:00
|
|
|
class different_locale:
|
2006-04-21 18:40:58 +08:00
|
|
|
def __init__(self, locale):
|
|
|
|
self.locale = locale
|
2022-02-24 21:29:08 +08:00
|
|
|
self.oldlocale = None
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def __enter__(self):
|
2022-02-24 21:29:08 +08:00
|
|
|
self.oldlocale = _locale.setlocale(_locale.LC_TIME, None)
|
2010-10-20 02:54:25 +08:00
|
|
|
_locale.setlocale(_locale.LC_TIME, self.locale)
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def __exit__(self, *args):
|
2022-02-24 21:29:08 +08:00
|
|
|
if self.oldlocale is None:
|
|
|
|
return
|
2007-11-12 09:32:03 +08:00
|
|
|
_locale.setlocale(_locale.LC_TIME, self.oldlocale)
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
|
2022-02-24 21:29:08 +08:00
|
|
|
def _get_default_locale():
|
|
|
|
locale = _locale.setlocale(_locale.LC_TIME, None)
|
|
|
|
if locale == "C":
|
|
|
|
with different_locale(""):
|
|
|
|
# The LC_TIME locale does not seem to be configured:
|
|
|
|
# get the user preferred locale.
|
|
|
|
locale = _locale.setlocale(_locale.LC_TIME, None)
|
|
|
|
return locale
|
|
|
|
|
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
class LocaleTextCalendar(TextCalendar):
|
|
|
|
"""
|
|
|
|
This class can be passed a locale name in the constructor and will return
|
2021-09-16 04:36:38 +08:00
|
|
|
month and weekday names in the specified locale.
|
2006-04-21 18:40:58 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, firstweekday=0, locale=None):
|
|
|
|
TextCalendar.__init__(self, firstweekday)
|
|
|
|
if locale is None:
|
2022-02-24 21:29:08 +08:00
|
|
|
locale = _get_default_locale()
|
2006-04-21 18:40:58 +08:00
|
|
|
self.locale = locale
|
|
|
|
|
|
|
|
def formatweekday(self, day, width):
|
2008-06-08 16:40:05 +08:00
|
|
|
with different_locale(self.locale):
|
2020-06-02 19:33:09 +08:00
|
|
|
return super().formatweekday(day, width)
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def formatmonthname(self, theyear, themonth, width, withyear=True):
|
2008-06-08 16:40:05 +08:00
|
|
|
with different_locale(self.locale):
|
2020-06-02 19:33:09 +08:00
|
|
|
return super().formatmonthname(theyear, themonth, width, withyear)
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
class LocaleHTMLCalendar(HTMLCalendar):
|
|
|
|
"""
|
|
|
|
This class can be passed a locale name in the constructor and will return
|
2021-09-16 04:36:38 +08:00
|
|
|
month and weekday names in the specified locale.
|
2006-04-21 18:40:58 +08:00
|
|
|
"""
|
|
|
|
def __init__(self, firstweekday=0, locale=None):
|
|
|
|
HTMLCalendar.__init__(self, firstweekday)
|
|
|
|
if locale is None:
|
2022-02-24 21:29:08 +08:00
|
|
|
locale = _get_default_locale()
|
2006-04-21 18:40:58 +08:00
|
|
|
self.locale = locale
|
|
|
|
|
|
|
|
def formatweekday(self, day):
|
2008-06-08 16:40:05 +08:00
|
|
|
with different_locale(self.locale):
|
2020-06-02 19:33:09 +08:00
|
|
|
return super().formatweekday(day)
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def formatmonthname(self, theyear, themonth, withyear=True):
|
2008-06-08 16:40:05 +08:00
|
|
|
with different_locale(self.locale):
|
2020-06-02 19:33:09 +08:00
|
|
|
return super().formatmonthname(theyear, themonth, withyear)
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
# Support for old module level interface
|
|
|
|
c = TextCalendar()
|
|
|
|
|
|
|
|
firstweekday = c.getfirstweekday
|
|
|
|
|
|
|
|
def setfirstweekday(firstweekday):
|
|
|
|
if not MONDAY <= firstweekday <= SUNDAY:
|
|
|
|
raise IllegalWeekdayError(firstweekday)
|
|
|
|
c.firstweekday = firstweekday
|
|
|
|
|
|
|
|
monthcalendar = c.monthdayscalendar
|
|
|
|
prweek = c.prweek
|
|
|
|
week = c.formatweek
|
|
|
|
weekheader = c.formatweekheader
|
|
|
|
prmonth = c.prmonth
|
|
|
|
month = c.formatmonth
|
|
|
|
calendar = c.formatyear
|
|
|
|
prcal = c.pryear
|
|
|
|
|
|
|
|
|
|
|
|
# Spacing of month columns for multi-column year calendar
|
2000-02-02 23:10:15 +08:00
|
|
|
_colwidth = 7*3 - 1 # Amount printed by prweek()
|
2000-08-30 22:01:28 +08:00
|
|
|
_spacing = 6 # Number of spaces between columns
|
1990-10-14 03:23:40 +08:00
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
def format(cols, colwidth=_colwidth, spacing=_spacing):
|
|
|
|
"""Prints multi-column formatting for year calendars"""
|
2007-02-09 13:37:30 +08:00
|
|
|
print(formatstring(cols, colwidth, spacing))
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
def formatstring(cols, colwidth=_colwidth, spacing=_spacing):
|
|
|
|
"""Returns a string formatted from n strings, centered within n columns."""
|
|
|
|
spacing *= ' '
|
|
|
|
return spacing.join(c.center(colwidth) for c in cols)
|
|
|
|
|
2000-02-02 23:10:15 +08:00
|
|
|
|
1999-06-09 23:07:38 +08:00
|
|
|
EPOCH = 1970
|
2010-06-15 02:33:19 +08:00
|
|
|
_EPOCH_ORD = datetime.date(EPOCH, 1, 1).toordinal()
|
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
|
1999-06-09 23:07:38 +08:00
|
|
|
def timegm(tuple):
|
2000-02-02 23:10:15 +08:00
|
|
|
"""Unrelated but handy function to calculate Unix timestamp from GMT."""
|
2010-06-15 02:33:19 +08:00
|
|
|
year, month, day, hour, minute, second = tuple[:6]
|
|
|
|
days = datetime.date(year, month, 1).toordinal() - _EPOCH_ORD + day - 1
|
|
|
|
hours = days*24 + hour
|
|
|
|
minutes = hours*60 + minute
|
|
|
|
seconds = minutes*60 + second
|
|
|
|
return seconds
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
2015-11-01 23:14:27 +08:00
|
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
textgroup = parser.add_argument_group('text only arguments')
|
|
|
|
htmlgroup = parser.add_argument_group('html only arguments')
|
|
|
|
textgroup.add_argument(
|
2006-04-21 18:40:58 +08:00
|
|
|
"-w", "--width",
|
2015-11-01 23:14:27 +08:00
|
|
|
type=int, default=2,
|
|
|
|
help="width of date column (default 2)"
|
2006-04-21 18:40:58 +08:00
|
|
|
)
|
2015-11-01 23:14:27 +08:00
|
|
|
textgroup.add_argument(
|
2006-04-21 18:40:58 +08:00
|
|
|
"-l", "--lines",
|
2015-11-01 23:14:27 +08:00
|
|
|
type=int, default=1,
|
|
|
|
help="number of lines for each week (default 1)"
|
2006-04-21 18:40:58 +08:00
|
|
|
)
|
2015-11-01 23:14:27 +08:00
|
|
|
textgroup.add_argument(
|
2006-04-21 18:40:58 +08:00
|
|
|
"-s", "--spacing",
|
2015-11-01 23:14:27 +08:00
|
|
|
type=int, default=6,
|
|
|
|
help="spacing between months (default 6)"
|
2006-04-21 18:40:58 +08:00
|
|
|
)
|
2015-11-01 23:14:27 +08:00
|
|
|
textgroup.add_argument(
|
2006-04-21 18:40:58 +08:00
|
|
|
"-m", "--months",
|
2015-11-01 23:14:27 +08:00
|
|
|
type=int, default=3,
|
|
|
|
help="months per row (default 3)"
|
2006-04-21 18:40:58 +08:00
|
|
|
)
|
2015-11-01 23:14:27 +08:00
|
|
|
htmlgroup.add_argument(
|
2006-04-21 18:40:58 +08:00
|
|
|
"-c", "--css",
|
2015-11-01 23:14:27 +08:00
|
|
|
default="calendar.css",
|
|
|
|
help="CSS to use for page"
|
2006-04-21 18:40:58 +08:00
|
|
|
)
|
2015-11-01 23:14:27 +08:00
|
|
|
parser.add_argument(
|
2006-04-21 18:40:58 +08:00
|
|
|
"-L", "--locale",
|
2015-11-01 23:14:27 +08:00
|
|
|
default=None,
|
2006-04-21 18:40:58 +08:00
|
|
|
help="locale to be used from month and weekday names"
|
|
|
|
)
|
2015-11-01 23:14:27 +08:00
|
|
|
parser.add_argument(
|
2006-04-21 18:40:58 +08:00
|
|
|
"-e", "--encoding",
|
2015-11-01 23:14:27 +08:00
|
|
|
default=None,
|
|
|
|
help="encoding to use for output"
|
2006-04-21 18:40:58 +08:00
|
|
|
)
|
2015-11-01 23:14:27 +08:00
|
|
|
parser.add_argument(
|
2006-04-21 18:40:58 +08:00
|
|
|
"-t", "--type",
|
2015-11-01 23:14:27 +08:00
|
|
|
default="text",
|
2006-04-21 18:40:58 +08:00
|
|
|
choices=("text", "html"),
|
|
|
|
help="output type (text or html)"
|
|
|
|
)
|
2015-11-01 23:14:27 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"year",
|
|
|
|
nargs='?', type=int,
|
|
|
|
help="year number (1-9999)"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"month",
|
|
|
|
nargs='?', type=int,
|
|
|
|
help="month number (1-12, text only)"
|
|
|
|
)
|
2006-04-21 18:40:58 +08:00
|
|
|
|
2015-11-01 23:14:27 +08:00
|
|
|
options = parser.parse_args(args[1:])
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
if options.locale and not options.encoding:
|
|
|
|
parser.error("if --locale is specified --encoding is required")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2007-11-12 09:32:03 +08:00
|
|
|
locale = options.locale, options.encoding
|
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
if options.type == "html":
|
|
|
|
if options.locale:
|
2007-11-12 09:32:03 +08:00
|
|
|
cal = LocaleHTMLCalendar(locale=locale)
|
2006-04-21 18:40:58 +08:00
|
|
|
else:
|
|
|
|
cal = HTMLCalendar()
|
|
|
|
encoding = options.encoding
|
|
|
|
if encoding is None:
|
|
|
|
encoding = sys.getdefaultencoding()
|
|
|
|
optdict = dict(encoding=encoding, css=options.css)
|
2011-08-11 09:22:52 +08:00
|
|
|
write = sys.stdout.buffer.write
|
2015-11-01 23:14:27 +08:00
|
|
|
if options.year is None:
|
2011-08-11 09:22:52 +08:00
|
|
|
write(cal.formatyearpage(datetime.date.today().year, **optdict))
|
2015-11-01 23:14:27 +08:00
|
|
|
elif options.month is None:
|
|
|
|
write(cal.formatyearpage(options.year, **optdict))
|
2006-04-21 18:40:58 +08:00
|
|
|
else:
|
|
|
|
parser.error("incorrect number of arguments")
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
if options.locale:
|
2007-11-12 09:32:03 +08:00
|
|
|
cal = LocaleTextCalendar(locale=locale)
|
2006-04-21 18:40:58 +08:00
|
|
|
else:
|
|
|
|
cal = TextCalendar()
|
|
|
|
optdict = dict(w=options.width, l=options.lines)
|
2015-11-01 23:14:27 +08:00
|
|
|
if options.month is None:
|
2006-04-21 18:40:58 +08:00
|
|
|
optdict["c"] = options.spacing
|
|
|
|
optdict["m"] = options.months
|
2015-11-01 23:14:27 +08:00
|
|
|
if options.year is None:
|
2006-04-21 18:40:58 +08:00
|
|
|
result = cal.formatyear(datetime.date.today().year, **optdict)
|
2015-11-01 23:14:27 +08:00
|
|
|
elif options.month is None:
|
|
|
|
result = cal.formatyear(options.year, **optdict)
|
2006-04-21 18:40:58 +08:00
|
|
|
else:
|
2015-11-01 23:14:27 +08:00
|
|
|
result = cal.formatmonth(options.year, options.month, **optdict)
|
2011-08-11 09:22:52 +08:00
|
|
|
write = sys.stdout.write
|
2006-04-21 18:40:58 +08:00
|
|
|
if options.encoding:
|
|
|
|
result = result.encode(options.encoding)
|
2011-08-11 09:22:52 +08:00
|
|
|
write = sys.stdout.buffer.write
|
|
|
|
write(result)
|
2006-04-21 18:40:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv)
|