Skip to content

Commit 933faee

Browse files
committed
calendar: version 1.0.0-alpha
datetime: version 4.0.1 unittest: version 0.9.1 Initial version of Calendar. Basic implementation of most Calendar features. Doesn't include local or encoding. It can produce text and HTMl calendars. Included is a basic command line set of arguments. A set of unit tests for the calendar have also been implemented based on CPython unit tests. Basic strftime has been implemented in datetime. This covers the minimum set of formatting required for the calendar functionality implemented. Added assertRaisesRegex and assertCountEq to unittest. These two new functions are required for testing calendar. assertRaisesRegex does not support calling with a compiled regex. A string is expected. Added unit tests for assertRaisesRegex and assertCountEq. Porting of Calendar to Micropython Updated datetime version information for new function strftime.
1 parent 88a68e5 commit 933faee

File tree

11 files changed

+364
-318
lines changed

11 files changed

+364
-318
lines changed

python-stdlib/calendar/calendar.py

Lines changed: 28 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
"firstweekday", "isleap", "leapdays", "weekday", "monthrange",
1515
"monthcalendar", "prmonth", "month", "prcal", "calendar",
1616
"timegm", "month_name", "month_abbr", "day_name", "day_abbr",
17-
"Calendar", "TextCalendar", "HTMLCalendar", "LocaleTextCalendar",
18-
"LocaleHTMLCalendar", "weekheader",
17+
"Calendar", "TextCalendar", "HTMLCalendar", "weekheader",
1918
"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY",
20-
"SATURDAY", "SUNDAY"]
19+
"SATURDAY", "SUNDAY", "error", "repeat"]
2120

2221
# Exception raised for bad input (with string parameter for details)
2322
error = ValueError
@@ -371,6 +370,10 @@ def formatyear(self, theyear, w=2, l=1, c=6, m=3):
371370
"""
372371
Returns a year's calendar as a multi-line string.
373372
"""
373+
w = int(w)
374+
l = int(l)
375+
c = int(c)
376+
m = int(m)
374377
w = max(2, w)
375378
l = max(1, l)
376379
c = max(2, c)
@@ -526,7 +529,7 @@ def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None):
526529
Return a formatted year as a complete HTML page.
527530
"""
528531
if encoding is None:
529-
encoding = sys.getdefaultencoding()
532+
encoding = _locale.getpreferredencoding()
530533
v = []
531534
a = v.append
532535
a('<?xml version="1.0" encoding="%s"?>\n' % encoding)
@@ -542,64 +545,7 @@ def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None):
542545
a(self.formatyear(theyear, width))
543546
a('</body>\n')
544547
a('</html>\n')
545-
return ''.join(v).encode(encoding, "xmlcharrefreplace")
546-
547-
548-
class different_locale:
549-
def __init__(self, locale):
550-
self.locale = locale
551-
552-
def __enter__(self):
553-
self.oldlocale = _locale.getlocale(_locale.LC_TIME)
554-
_locale.setlocale(_locale.LC_TIME, self.locale)
555-
556-
def __exit__(self, *args):
557-
_locale.setlocale(_locale.LC_TIME, self.oldlocale)
558-
559-
560-
class LocaleTextCalendar(TextCalendar):
561-
"""
562-
This class can be passed a locale name in the constructor and will return
563-
month and weekday names in the specified locale. If this locale includes
564-
an encoding all strings containing month and weekday names will be returned
565-
as unicode.
566-
"""
567-
568-
def __init__(self, firstweekday=0, locale=None):
569-
TextCalendar.__init__(self, firstweekday)
570-
if locale is None:
571-
locale = _locale.getdefaultlocale()
572-
self.locale = locale
573-
574-
def formatweekday(self, day, width):
575-
with different_locale(self.locale):
576-
return super().formatweekday(day, width)
577-
578-
def formatmonthname(self, theyear, themonth, width, withyear=True):
579-
with different_locale(self.locale):
580-
return super().formatmonthname(theyear, themonth, width, withyear)
581-
582-
583-
class LocaleHTMLCalendar(HTMLCalendar):
584-
"""
585-
This class can be passed a locale name in the constructor and will return
586-
month and weekday names in the specified locale. If this locale includes
587-
an encoding all strings containing month and weekday names will be returned
588-
as unicode.
589-
"""
590-
def __init__(self, firstweekday=0, locale=None):
591-
HTMLCalendar.__init__(self, firstweekday)
592-
if locale is None:
593-
locale = _locale.getdefaultlocale()
594-
self.locale = locale
595-
596-
def formatweekday(self, day):
597-
with different_locale(self.locale):
598-
return super().formatweekday(day)
599-
600-
def formatmonthname(self, theyear, themonth, withyear=True):
601-
with different_locale(self.locale):
602-
return super().formatmonthname(theyear, themonth, withyear)
548+
return ''.join(v).encode(encoding)
603549

604550
# Support for old module level interface
605551
c = TextCalendar()
@@ -633,7 +579,7 @@ def format(cols, colwidth=_colwidth, spacing=_spacing):
633579

634580
def formatstring(cols, colwidth=_colwidth, spacing=_spacing):
635581
"""Returns a string formatted from n strings, centered within n columns."""
636-
spacing *= ' '
582+
spacing = spacing * ' '
637583
return spacing.join(c.center(colwidth) for c in cols)
638584

639585

@@ -654,48 +600,41 @@ def timegm(tuple):
654600
def main(args):
655601
import argparse
656602
parser = argparse.ArgumentParser()
657-
textgroup = parser.add_argument_group('text only arguments')
658-
htmlgroup = parser.add_argument_group('html only arguments')
659-
textgroup.add_argument(
603+
parser.add_argument(
660604
"-w", "--width",
661605
type=int, default=2,
662-
help="width of date column (default 2)"
606+
help="\twidth of date column (default 2)"
663607
)
664-
textgroup.add_argument(
608+
parser.add_argument(
665609
"-l", "--lines",
666610
type=int, default=1,
667-
help="number of lines for each week (default 1)"
611+
help="\tnumber of lines for each week (default 1)"
668612
)
669-
textgroup.add_argument(
613+
parser.add_argument(
670614
"-s", "--spacing",
671615
type=int, default=6,
672-
help="spacing between months (default 6)"
616+
help="\tspacing between months (default 6)"
673617
)
674-
textgroup.add_argument(
618+
parser.add_argument(
675619
"-m", "--months",
676620
type=int, default=3,
677-
help="months per row (default 3)"
621+
help="\tmonths per row (default 3)"
678622
)
679-
htmlgroup.add_argument(
623+
parser.add_argument(
680624
"-c", "--css",
681625
default="calendar.css",
682-
help="CSS to use for page"
683-
)
684-
parser.add_argument(
685-
"-L", "--locale",
686-
default=None,
687-
help="locale to be used from month and weekday names"
626+
help="\tCSS to use for page"
688627
)
689628
parser.add_argument(
690629
"-e", "--encoding",
691630
default=None,
692-
help="encoding to use for output"
631+
help="\tencoding to use for output"
693632
)
694633
parser.add_argument(
695634
"-t", "--type",
696635
default="text",
697636
choices=("text", "html"),
698-
help="output type (text or html)"
637+
help="\toutput type (text or html)"
699638
)
700639
parser.add_argument(
701640
"year",
@@ -710,48 +649,35 @@ def main(args):
710649

711650
options = parser.parse_args(args[1:])
712651

713-
if options.locale and not options.encoding:
714-
parser.error("if --locale is specified --encoding is required")
715-
sys.exit(1)
716-
717-
locale = options.locale, options.encoding
718-
719652
if options.type == "html":
720-
if options.locale:
721-
cal = LocaleHTMLCalendar(locale=locale)
722-
else:
723-
cal = HTMLCalendar()
653+
cal = HTMLCalendar()
724654
encoding = options.encoding
725655
if encoding is None:
726-
encoding = sys.getdefaultencoding()
656+
encoding = _locale.getpreferredencoding()
727657
optdict = dict(encoding=encoding, css=options.css)
728-
write = sys.stdout.buffer.write
658+
write = sys.stdout.write
729659
if options.year is None:
730660
write(cal.formatyearpage(datetime.date.today().year, **optdict))
731661
elif options.month is None:
732-
write(cal.formatyearpage(options.year, **optdict))
662+
write(cal.formatyearpage(int(options.year), **optdict))
733663
else:
734664
parser.error("incorrect number of arguments")
735665
sys.exit(1)
736666
else:
737-
if options.locale:
738-
cal = LocaleTextCalendar(locale=locale)
739-
else:
740-
cal = TextCalendar()
667+
cal = TextCalendar()
741668
optdict = dict(w=options.width, l=options.lines)
742669
if options.month is None:
743670
optdict["c"] = options.spacing
744671
optdict["m"] = options.months
745672
if options.year is None:
746673
result = cal.formatyear(datetime.date.today().year, **optdict)
747674
elif options.month is None:
748-
result = cal.formatyear(options.year, **optdict)
675+
result = cal.formatyear(int(options.year), **optdict)
749676
else:
750-
result = cal.formatmonth(options.year, options.month, **optdict)
677+
result = cal.formatmonth(int(options.year), int(options.month), **optdict)
751678
write = sys.stdout.write
752679
if options.encoding:
753680
result = result.encode(options.encoding)
754-
write = sys.stdout.buffer.write
755681
write(result)
756682

757683

python-stdlib/calendar/metadata.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
srctype = cpython
22
type = module
3-
version = 0.0.0-1
3+
version = 1.0.0-alpha

python-stdlib/calendar/setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
sys.path.append("..")
99
import sdist_upip
1010

11-
# TODO: Where does the version come from?
1211
setup(
1312
name="micropython-calendar",
14-
version="0.0.0-1",
13+
version="1.0.0-alpha",
1514
description="CPython calendar module ported to MicroPython",
1615
long_description="This is a module ported from CPython standard library to be compatible with\nMicroPython interpreter. Usually, this means applying small patches for\nfeatures not supported (yet, or at all) in MicroPython. Sometimes, heavier\nchanges are required. Note that CPython modules are written with availability\nof vast resources in mind, and may not work for MicroPython ports with\nlimited heap. If you are affected by such a case, please help reimplement\nthe module from scratch.",
1716
url="https://github.com/micropython/micropython-lib",

0 commit comments

Comments
 (0)