U e5da@s4dZddlZddlZddlZddlmZdddddd d d d d ddddddddddddddgZeZ GdddeZ GdddeZ dZ dZ dd d!d d"d d"d d d"d d"d g ZGd#d$d$ZGd%d&d&Zed'Zed(Zed)Zed*Zed+\ZZZZZZZd,dZd-d Zd.d Zd/d Z d0d1Z!d2d3Z"d4d5Z#Gd6dde$Z%Gd7dde%Z&Gd8dde%Z'Gd9d:d:Z(Gd;dde&Z)GdZ=d?Z>e=e>fd@dAZ?e=e>fdBdCZ@dDZAeBeAddCZDdEdZEdFdGZFeGdHkr0eFejHdS)Ia$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).N)repeatIllegalMonthErrorIllegalWeekdayErrorsetfirstweekday firstweekdayisleapleapdaysweekday monthrange monthcalendarprmonthmonthprcalcalendartimegm month_name month_abbrday_nameday_abbrCalendar TextCalendar HTMLCalendarLocaleTextCalendarLocaleHTMLCalendar weekheaderc@seZdZddZddZdS)rcCs ||_dSNr )selfr r /usr/lib64/python3.8/calendar.py__init__szIllegalMonthError.__init__cCs d|jS)Nz!bad month number %r; must be 1-12rrrrr__str__szIllegalMonthError.__str__N__name__ __module__ __qualname__r r"rrrrrsc@seZdZddZddZdS)rcCs ||_dSrr )rr rrrr szIllegalWeekdayError.__init__cCs d|jS)Nz7bad weekday number %r; must be 0 (Monday) to 6 (Sunday)r'r!rrrr""szIllegalWeekdayError.__str__Nr#rrrrrsc@sFeZdZddedDZedddddZd d Zd d Zd S)_localized_monthcCs g|]}td|ddjqSir(datetimedatestrftime.0irrr 4sz_localized_month. rcCsdS)Nr)xrrr5z_localized_month.cCs ||_dSrformatrr=rrrr 7sz_localized_month.__init__cs4j|}t|tr&fdd|DS|jSdS)Ncsg|]}|jqSrr<r4fr!rrr6=sz0_localized_month.__getitem__..)_months isinstanceslicer=rr5Zfuncsrr!r __getitem__:s  z_localized_month.__getitem__cCsdS)N rr!rrr__len__Asz_localized_month.__len__N) r$r%r&rangerAinsertr rErGrrrrr-2s r-c@s6eZdZddedDZddZddZdd Zd S) _localized_daycCs g|]}tdd|djqSr.r/r3rrrr6Hsz_localized_day.cCs ||_dSrr<r>rrrr Jsz_localized_day.__init__cs4j|}t|tr&fdd|DS|jSdS)Ncsg|]}|jqSrr<r?r!rrr6Psz._localized_day.__getitem__..)_daysrBrCr=rDrr!rrEMs  z_localized_day.__getitem__cCsdSNrKrr!rrrrGTsz_localized_day.__len__N)r$r%r&rHrLr rErGrrrrrJEsrJz%Az%az%Bz%brKcCs$|ddko"|ddkp"|ddkS)z5Return True for leap years, False for non-leap years.rdr)yearrrrrdscCs@|d8}|d8}|d|d|d|d|d|dS)zFReturn number of leap years in range [y1, y2). Assume y1 <= y2.r(rNrOrPr)Zy1Zy2rrrriscCs8tj|krtjks&nd|d}t|||S)zBReturn weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31).irP)r0ZMINYEARZMAXYEARr1r )rQr dayrrrr qs cCsJd|krdksnt|t||d}t||tko>t|}||fS)zQReturn weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month.r(r7)rr mdaysFebruaryr)rQr day1ndaysrrrr xs  cCst||tkot|Sr)rSrTrrQr rrr _monthlensrXcCs$|dkr|ddfS||dfSdS)Nr(r7rrWrrr _prevmonths rYcCs$|dkr|ddfS||dfSdS)Nr7r(rrWrrr _nextmonths rZc@seZdZdZd#ddZddZddZeeeZd d Z d d Z d dZ ddZ ddZ ddZddZddZddZd$ddZd%ddZd&d d!Zd"S)'rzo Base calendar class. This class doesn't do any formatting. It simply provides data to subclasses. rcCs ||_dSrrrrrrrr szCalendar.__init__cCs |jdSrMZ _firstweekdayr!rrrgetfirstweekdayszCalendar.getfirstweekdaycCs ||_dSrr]r\rrrrszCalendar.setfirstweekdayccs&t|j|jdD]}|dVqdS)zt Return an iterator for one week of weekday numbers starting with the configured first one. rKN)rHr)rr5rrr iterweekdaysszCalendar.iterweekdaysccs,|||D]\}}}t|||Vq dS)z 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. N)itermonthdays3r0r1)rrQr ymdrrritermonthdatesszCalendar.itermonthdatesccsft||\}}||jd}td|EdHtd|dEdH|j||d}td|EdHdS)z Like itermonthdates(), but will yield day numbers. For days outside the specified month the day number is 0. rKrNr()r rrrH)rrQr rUrV days_before days_afterrrr itermonthdayss zCalendar.itermonthdaysccs0t||||jD]\}}||dfVqdS)z Like itermonthdates(), but will yield (day number, weekday number) tuples. For days outside the specified month the day number is 0. rKN) enumeratergr)rrQr r5rcrrritermonthdays2szCalendar.itermonthdays2c cst||\}}||jd}|j||d}t||\}}t||d} t| || D]} ||| fVqXtd|dD]} ||| fVqxt||\}}td|dD]} ||| fVqdS)z Like itermonthdates(), but will yield (year, month, day) tuples. Can be used for dates outside of datetime.date range. rKr(N)r rrYrXrHrZ) rrQr rUrVrerfrarbendrcrrrr`szCalendar.itermonthdays3ccs<t|||D]&\}\}}}||||j|dfVqdS)z Like itermonthdates(), but will yield (year, month, day, day_of_week) tuples. Can be used for dates outside of datetime.date range. rKN)rhr`r)rrQr r5rarbrcrrritermonthdays4szCalendar.itermonthdays4cs.t|||fddtdtdDS)z Return a matrix (list of lists) representing a month's calendar. Each row represents a week; week entries are datetime.date values. csg|]}||dqSrKrr3Zdatesrrr6sz/Calendar.monthdatescalendar..rrK)listrdrHlenrrQr rrmrmonthdatescalendarszCalendar.monthdatescalendarcs.t|||fddtdtdDS)z 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. csg|]}||dqSrlrr3daysrrr6sz/Calendar.monthdays2calendar..rrK)rnrirHrorprrrrmonthdays2calendarszCalendar.monthdays2calendarcs.t|||fddtdtdDS)z Return a matrix representing a month's calendar. Each row represents a week; days outside this month are zero. csg|]}||dqSrlrr3rrrrr6sz.Calendar.monthdayscalendar..rrK)rnrgrHrorprrrrmonthdayscalendarszCalendar.monthdayscalendarcs>fddtttdDfddtdtDS)a' Return the data for the specified year ready for formatting. The return value is a list of month rows. Each month row contains up to width months. Each month contains between 4 and 6 weeks and each week contains 1-7 days. Days are datetime.date objects. csg|]}|qSr)rqr3rrQrrr6sz.Calendar.yeardatescalendar..r7csg|]}||qSrrr3monthswidthrrr6 srrHJanuaryrorrQrzrryrrzrQryeardatescalendars  zCalendar.yeardatescalendarcs>fddtttdDfddtdtDS)z 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. csg|]}|qSr)rtr3rwrrr6sz.Calendar.yeardays2calendar..r7csg|]}||qSrrr3rxrrr6srr{r}rr~ryeardays2calendar s  zCalendar.yeardays2calendarcs>fddtttdDfddtdtDS)z 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. csg|]}|qSr)rur3rwrrr6sz-Calendar.yeardayscalendar..r7csg|]}||qSrrr3rxrrr6"srr{r}rr~ryeardayscalendars  zCalendar.yeardayscalendarN)r)rv)rv)rv)r$r%r&__doc__r r^rpropertyrr_rdrgrir`rkrqrtrurrrrrrrrs"      c@sjeZdZdZddZddZddZdd Zd d Zdd dZ dddZ dddZ d ddZ d!ddZ dS)"rzr Subclass of Calendar that outputs a calendar as a simple plain text similar to the UNIX program cal. cCst|||dddS)z3 Print a single week (no newline). r8rjN)print formatweekrtheweekrzrrrprweek+szTextCalendar.prweekcCs |dkrd}nd|}||S)z* Returns a formatted day. rr8z%2icenter)rrRr rzsrrr formatday1szTextCalendar.formatdaycsdfdd|DS)zA Returns a single week in a string (no newline).  c3s |]\}}||VqdSrrr4rcZwdrrzrr ?sz*TextCalendar.formatweek..joinrrrrr;szTextCalendar.formatweekcCs(|dkrt}nt}||d||S)z4 Returns a formatted week day name. N)rrr)rrRrznamesrrr formatweekdayAszTextCalendar.formatweekdaycsdfddDS)z- Return a header for a week. rc3s|]}|VqdSrrr3rrrrOsz0TextCalendar.formatweekheader..rr_rrrrformatweekheaderKszTextCalendar.formatweekheaderTcCs"t|}|rd||f}||S)z0 Return a formatted month name. %s %r)rrrtheyearthemonthrzwithyearrrrrformatmonthnameQs zTextCalendar.formatmonthnamercCst|||||dddS)z+ Print a month's calendar. r8rN)r formatmonth)rrrwlrrrr ZszTextCalendar.prmonthcCstd|}td|}|||d|dd}|}|d|7}|||7}|d|7}|||D]$}||||7}|d|7}ql|S)z@ Return a month's calendar string (multi-line). r)r(rK )maxrrstriprrtr)rrrrrrweekrrrr`s    zTextCalendar.formatmonthr)r(rvc std|}td|}td|}|dddg}|j}|t|||d|d||t|D]"\}} t||dt ||ddd} |d|fdd| D} |t | ||d|fdd| D} |t | ||d|td d| D} t| D]f}g}| D]6}|t |krj|d n| |||qL|t |||d|q@qd |S) zC Returns a year's calendar as a multi-line string. r)r(rKrrFc3s|]}|dVqdS)FN)rr4k)colwidthrrrrrsz*TextCalendar.formatyear..c3s|] }VqdSrrr)headerrrrscss|]}t|VqdSr)ro)r4calrrrrsr8)rappendreprrrrrhrrHmin formatstringrorr)rrrrcrbvar5rowryrZheadersZheightjZweeksrr)rrrrr formatyearps<   &  $     zTextCalendar.formatyearcCst||||||dddS)zPrint a year's calendar.r8rN)rr)rrrrrrbrrrpryearszTextCalendar.pryearN)T)rr)rr)r)r(rrv)rrrrv)r$r%r&rrrrrrrr rrrrrrrr%s     %c@seZdZdZdddddddgZeZd Zd Zd Zd Z d Z d d Z ddZ ddZ ddZd ddZd!ddZd"ddZd#ddZdS)$rz4 This calendar returns complete HTML pages. ZmonZtueZwedZthuZfriZsatZsunZnodayr rQcCs(|dkrd|jSd|j||fSdS)z/ Return a day as a table cell. rz z%dN)cssclass_noday cssclasses)rrRr rrrrs zHTMLCalendar.formatdaycs dfdd|D}d|S)z8 Return a complete week as a table row. r8c3s|]\}}||VqdSrrrr!rrrsz*HTMLCalendar.formatweek.. %sr)rrrrr!rrszHTMLCalendar.formatweekcCsd|j|t|fS)z: Return a weekday name as a table header. %s)cssclasses_weekday_headr)rrRrrrrszHTMLCalendar.formatweekdaycs$dfddD}d|S)z< Return a header for a week as a table row. r8c3s|]}|VqdSrrr3r!rrrsz0HTMLCalendar.formatweekheader..rr)rrrr!rrszHTMLCalendar.formatweekheaderTcCs0|rdt||f}n dt|}d|j|fS)z5 Return a month name as a table row. %s %sz%sz+%s)rcssclass_month_headrrrrrrrrrs zHTMLCalendar.formatmonthnamecCsg}|j}|d|j|d||j|||d|d|||d|||D]}||||dq\|d|dd|S)z6 Return a formatted month as a table. =rr
r8)rcssclass_monthrrrtrr)rrrrrrrrrrrs   zHTMLCalendar.formatmonthrvcCsg}|j}t|d}|d|j|d|d||j|ftttd|D]V}t|t||d}|d|D](}|d||j||d d |d qr|d qN|d d|S)z? Return a formatted year as a table of tables. r(rrz,%sr7rFzzFrzzrr8) rr cssclass_yearcssclass_year_headrHr|rrr)rrrzrrr5ryrbrrrrs,   zHTMLCalendar.formatyear calendar.cssNcCs|dkrt}g}|j}|d||d|d|d|d||dk r^|d||d||d |d |||||d |d d ||dS)zB Return a formatted year as a complete HTML page. Nz$ zn z z zC z4 zCalendar for %d z z z z r8xmlcharrefreplace)sysgetdefaultencodingrrrencode)rrrzcssencodingrrrrrformatyearpage s$    zHTMLCalendar.formatyearpage)T)T)rv)rvrN)r$r%r&rrrrrrrrrrrrrrrrrrrrrs    c@s$eZdZddZddZddZdS)different_localecCs ||_dSrlocale)rrrrrr #szdifferent_locale.__init__cCs"ttj|_ttj|jdSr)_localeZ getlocaleLC_TIME oldlocale setlocalerr!rrr __enter__&szdifferent_locale.__enter__cGsttj|jdSr)rrrr)rargsrrr__exit__*szdifferent_locale.__exit__N)r$r%r&r rrrrrrr"src@s,eZdZdZd ddZddZd d d ZdS) r This class can be passed a locale name in the constructor and will return month and weekday names in the specified locale. If this locale includes an encoding all strings containing month and weekday names will be returned as unicode. rNcCs&t|||dkrt}||_dSr)rr rgetdefaultlocalerrrrrrrr 6s zLocaleTextCalendar.__init__c CsNt|j:|dkrt}nt}||}|d||W5QRSQRXdS)Nr)rrrrr)rrRrzrnamerrrr<s  z LocaleTextCalendar.formatweekdayTc CsDt|j0t|}|r$d||f}||W5QRSQRXdS)Nr)rrrrrrrrrEs   z"LocaleTextCalendar.formatmonthname)rN)Tr$r%r&rr rrrrrrr.s  c@s,eZdZdZd ddZddZd d d ZdS) rrrNcCs&t|||dkrt}||_dSr)rr rrrrrrrr Ts zLocaleHTMLCalendar.__init__c Cs<t|j(t|}d|j||fW5QRSQRXdS)Nr)rrrr)rrRrrrrrZs z LocaleHTMLCalendar.formatweekdayTc CsBt|j.t|}|r$d||f}d|W5QRSQRXdS)Nrz.%s)rrrrrrrr_s   z"LocaleHTMLCalendar.formatmonthname)rN)TrrrrrrMs cCs(t|krtksnt||t_dSr)MONDAYSUNDAYrrrr[rrrrlsrcCstt|||dS)z1Prints multi-column formatting for year calendarsN)rrZcolsrspacingrrrr=sr=cs |d9}|fdd|DS)zEReturns a string formatted from n strings, centered within n columns.rc3s|]}|VqdSrr)r4rrrrrszformatstring..rrrrrrsric Cs^|dd\}}}}}}t||dt|d}|d|}|d|} | d|} | S)zBUnrelated but handy function to calculate Unix timestamp from GMT.Nrr(<)r0r1 toordinal _EPOCH_ORD) tuplerQr rRZhourZminutesecondrsZhoursZminutesZsecondsrrrrs    c Csddl}|}|d}|d}|jddtddd|jd d td d d|jd dtddd|jddtddd|jddddd|jddddd|jddddd|jd d!d"d#d$d%|jd&d'td(d)|jd*d'td+d)||d d}|jr|js|d,t d |j|jf}|j d-kr|jrDt |d.}nt }|j}|dkrbt }t||jd/} t jjj} |jdkr| |jtjjf| n6|jdkr| |j|jf| n|d0t d n|jrt|d.}nt}t|j|jd1} |jdkr$|j| d2<|j| d3<|jdkrH|j tjjf| } n2|jdkrf|j |jf| } n|j!|j|jf| } t jj} |jr| "|j} t jjj} | | dS)4Nrztext only argumentszhtml only argumentsz-wz--widthr)z width of date column (default 2))typedefaulthelpz-lz--linesr(z)number of lines for each week (default 1)z-sz --spacingrz"spacing between months (default 6)z-mz--monthsrvzmonths per row (default 3)z-cz--cssrzCSS to use for page)rrz-Lz--localez.locale to be used from month and weekday namesz-ez --encodingzencoding to use for outputz-tz--typetext)rhtmlzoutput type (text or html))rchoicesrrQ?zyear number (1-9999))nargsrrr zmonth number (1-12, text only)z/if --locale is specified --encoding is requiredrr)rrzincorrect number of arguments)rrrrb)#argparseArgumentParserZadd_argument_group add_argumentint parse_argsrrerrorrexitrrrrdictrstdoutbufferwriterQrr0r1Ztodayr rrrzlinesrryrrr) rrparserZ textgroupZ htmlgroupZoptionsrrrZoptdictrresultrrrmains                     r__main__)Irrr0rr itertoolsr__all__ ValueErrorrrrr|rTrSr-rJrrrrrHrZTUESDAYZ WEDNESDAYZTHURSDAYZFRIDAYZSATURDAYrrrr r rXrYrZobjectrrrrrrrr^rrrur rrrrrr rr rrrrZ _colwidthZ_spacingr=rZEPOCHr1rrrrr$argvrrrrs  u   h