a DOga@sdZddlZddlZddlZddlmZgdZeZ GdddeZ GdddeZ d Z d Z gd ZGd d d ZGdddZedZedZedZedZed\ZZZZZZZddZddZddZddZ ddZ!dd Z"d!d"Z#Gd#d$d$e$Z%Gd%d&d&e%Z&Gd'd(d(e%Z'Gd)d*d*Z(Gd+d,d,e&Z)Gd-d.d.e'Z*e&Z+e+j,Z-d/d0Z.e+j/Z0e+j1Z1e+j2Z3e+j4Z5e+j6Z6e+j7Z8e+j9Z:e+j;Ze=e>fd3d4Z?e=e>fd5d6Z@d7ZAeBeAd d CZDd8d9ZEd:d;ZFeGd4z_localized_month. rcCsdS)Nr)xrrr5r7z_localized_month.cCs ||_dSrformatrr=rrrr 7sz_localized_month.__init__cs4j|}t|tr&fdd|DS|jSdS)Ncsg|]}|jqSrr<r4fr!rrr6=r7z0_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/r3rrrr6Hr7z_localized_day.cCs ||_dSrr<r>rrrr Jsz_localized_day.__init__cs4j|}t|tr&fdd|DS|jSdS)Ncsg|]}|jqSrr<r?r!rrr6Pr7z._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)yearrrrrdsrcCs@|d8}|d8}|d|d|d|d|d|dS)zFReturn number of leap years in range [y1, y2). Assume y1 <= y2.r(rNrOrPr)Zy1Zy2rrrrisrcCs8tj|krtjks&nd|d}t|||S)zBReturn weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31).irP)r0ZMINYEARZMAXYEARr1r )rQr dayrrrr qs r cCsJd|krdksnt|t||d}t||tko>t|}||fS)zQReturn weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month.r(r8)rr mdaysFebruaryr)rQr day1ndaysrrrr xs  r cCst||tkot|Sr)rSrTrrQr rrr _monthlensrXcCs$|dkr|ddfS||dfSdS)Nr(r8rrWrrr _prevmonths rYcCs$|dkr|ddfS||dfSdS)Nr8r(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|]}||dqSrKrr3Zdatesrrr6r7z/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|]}||dqSrlrr3daysrrr6r7z/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|]}||dqSrlrr3rrrrr6r7z.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..r8csg|]}||qSrrr3monthswidthrrr6 r7rrHJanuaryrorrQrzrryrrzrQryeardatescalendars  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..r8csg|]}||qSrrr3rxrrr6r7rr{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..r8csg|]}||qSrrr3rxrrr6"r7rr{r}rr~ryeardayscalendars  zCalendar.yeardayscalendarN)r)rv)rv)rv)r$r%r&__doc__r r^rpropertyrr_rdrgrir`rkrqrtrurrrrrrrrs"      rc@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). r9rjN)print formatweekrtheweekrzrrrprweek+szTextCalendar.prweekcCs |dkrd}nd|}||S)z* Returns a formatted day. rr9z%2icenter)rrRr rzsrrr formatday1szTextCalendar.formatdaycsdfdd|DS)zA Returns a single week in a string (no newline).  c3s |]\}}||VqdSrrr4rcZwdrrzrr ?r7z*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|]}|VqdSrrr3rrrrOr7z0TextCalendar.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. r9rN)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)headerrrrr7css|]}t|VqdSr)ro)r4calrrrrr7r9)rappendreprrrrrhrrHmin formatstringrorr)rrrrcrbvar5rowryrZheadersZheightjZweeksrr)rrrrr formatyearps<   &  $     zTextCalendar.formatyearcCst||||||dddS)zPrint a year's calendar.r9rN)rr)rrrrrrbrrrpryearszTextCalendar.pryearN)T)rr)rr)r)r(rrv)rrrrv)r$r%r&rrrrrrrr rrrrrrrr%s     %rc@sxeZdZdZgdZeZdZdZdZdZ dZ ddZ dd Z d d Z d d ZdddZdddZdddZdddZdS)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. r9c3s|]\}}||VqdSrrrr!rrrr7z*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. r9c3s|]}|VqdSrrr3r!rrrr7z0HTMLCalendar.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
r9)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,%sr8rFzzFrzzrr9) rr cssclass_yearcssclass_year_headrHr|rrr)rrrzrrr5ryrbrrrrs(   zHTMLCalendar.formatyear calendar.cssNcCs|durt}g}|j}|d||d|d|d|d||dur^|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 r9xmlcharrefreplace)sysgetdefaultencodingrrrencode)rrrzcssencodingrrrrrformatyearpage s$    zHTMLCalendar.formatyearpage)T)T)rv)rvrN)r$r%r&rrrrrrrrrrrrrrrrrrrrrs    rc@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|||durt}||_dSr)rr rgetdefaultlocalerrrrrrrr 6s zLocaleTextCalendar.__init__cCsZt|j<|dkrt}nt}||}|d||WdS1sL0YdS)Nr)rrrrr)rrRrzrnamerrrr<s  z LocaleTextCalendar.formatweekdayTcCsPt|j2t|}|r$d||f}||WdS1sB0YdS)Nr)rrrrrrrrrEs   z"LocaleTextCalendar.formatmonthname)rN)Tr$r%r&rr rrrrrrr.s  rc@s,eZdZdZd ddZddZd d d ZdS) rrrNcCs&t|||durt}||_dSr)rr rrrrrrrr Ts zLocaleHTMLCalendar.__init__cCsHt|j*t|}d|j||fWdS1s:0YdS)Nr)rrrr)rrRrrrrrZs z LocaleHTMLCalendar.formatweekdayTcCsNt|j0t|}|r$d||f}d|WdS1s@0YdS)Nrz.%s)rrrrrrrr_s   z"LocaleHTMLCalendar.formatmonthname)rN)TrrrrrrMs rcCs(t|krtksnt||t_dSr)MONDAYSUNDAYrrrr[rrrrlsrrcCstt|||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)r4rrrrrr7zformatstring..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    rc 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}|durbt }t||jd/} t jjj} |jdur| |jtjjfi| n:|jdur| |j|jfi| n|d0t d n|jrt|d.}nt}t|j|jd1} |jdur,|j| d2<|j| d3<|jdurT|j tjjfi| } n:|jdurv|j |jfi| } n|j!|j|jfi| } 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$argvrrrrsl  u   h