a da@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=rr5funcsrr!r __getitem__:s  z_localized_month.__getitem__cCsdS)N rr!rrr__len__Asz_localized_month.__len__N) r$r%r&rangerAinsertr rFrHrrrrr-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!rrFMs  z_localized_day.__getitem__cCsdSNrLrr!rrrrHTsz_localized_day.__len__N)r$r%r&rIrMr rFrHrrrrrKEsrKz%Az%az%Bz%brLcCs$|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(rOrPrQr)y1y2rrrrisrcCs8tj|krtjks&nd|d}t|||S)zBReturn weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31).irQ)r0MINYEARMAXYEARr1r )rRr 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)rRr day1ndaysrrrr xs  r cCst||tkot|Sr)rXrYrrRr rrr _monthlensr]cCs$|dkr|ddfS||dfSdS)Nr(r8rr\rrr _prevmonths r^cCs$|dkr|ddfS||dfSdS)Nr8r(rr\rrr _nextmonths r_c@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 |jdSrN _firstweekdayr!rrrgetfirstweekdayszCalendar.getfirstweekdaycCs ||_dSrrbrarrrrszCalendar.setfirstweekdayccs&t|j|jdD]}|dVqdS)zt Return an iterator for one week of weekday numbers starting with the configured first one. rLN)rIr)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)rrRr 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. rLrNr()r rrrI)rrRr rZr[ 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. rLN) enumeratermr)rrRr r5rirrritermonthdays2szCalendar.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. rLr(N)r rr^r]rIr_) rrRr rZr[rkrlrgrhendrirrrrfszCalendar.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. rLN)rnrfr)rrRr r5rgrhrirrritermonthdays4szCalendar.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|]}||dqSrLrr3datesrrr6r7z/Calendar.monthdatescalendar..rrL)listrjrIlenrrRr rrsrmonthdatescalendarszCalendar.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|]}||dqSrrrr3daysrrr6r7z/Calendar.monthdays2calendar..rrL)rurorIrvrwrryrmonthdays2calendarszCalendar.monthdays2calendarcs.t|||fddtdtdDS)z Return a matrix representing a month's calendar. Each row represents a week; days outside this month are zero. csg|]}||dqSrrrr3ryrrr6r7z.Calendar.monthdayscalendar..rrL)rurmrIrvrwrryrmonthdayscalendarszCalendar.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)rxr3rrRrrr6sz.Calendar.yeardatescalendar..r8csg|]}||qSrrr3monthswidthrrr6 r7rrIJanuaryrvrrRrrrrrrRryeardatescalendars  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)r{r3r~rrr6sz.Calendar.yeardays2calendar..r8csg|]}||qSrrr3rrrr6r7rrrrrryeardays2calendar 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)r|r3r~rrr6sz-Calendar.yeardayscalendar..r8csg|]}||qSrrr3rrrr6"r7rrrrrryeardayscalendars  zCalendar.yeardayscalendarN)r)r})r})r})r$r%r&__doc__r rdrpropertyrrerjrmrorfrqrxr{r|rrrrrrrrs"      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). r9rpN)print formatweekrtheweekrrrrprweek+szTextCalendar.prweekcCs |dkrd}nd|}||S)z* Returns a formatted day. rr9z%2icenter)rrWr rsrrr formatday1szTextCalendar.formatdaycsdfdd|DS)zA Returns a single week in a string (no newline).  c3s |]\}}||VqdSrrr4riwdrrrr ?r7z*TextCalendar.formatweek..joinrrrrr;szTextCalendar.formatweekcCs(|dkrt}nt}||d||S)z4 Returns a formatted week day name. N)rrr)rrWrnamesrrr formatweekdayAszTextCalendar.formatweekdaycsdfddDS)z- Return a header for a week. rc3s|]}|VqdSrrr3rrrrOr7z0TextCalendar.formatweekheader..rrerrrrformatweekheaderKszTextCalendar.formatweekheaderTcCs"t|}|rd||f}||S)z0 Return a formatted month name. %s %r)rrrtheyearthemonthrwithyearrrrrformatmonthnameQs 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(rL )maxrrstriprr{r)rrrrrrweekrrrr`s    zTextCalendar.formatmonthr)r(r}c 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(rLrrGc3s|]}|dVqdS)FN)rr4k)colwidthrrrrrsz*TextCalendar.formatyear..c3s|] }VqdSrrr)headerrrrr7css|]}t|VqdSr)rv)r4calrrrrr7r9)rappendreprrrrrnrrImin formatstringrvrr)rrrrcrhvar5rowrrheadersheightjweeksrr)rrrrr formatyearps<   &  $     zTextCalendar.formatyearcCst||||||dddS)zPrint a year's calendar.r9rN)rr)rrrrrrhrrrpryearszTextCalendar.pryearN)T)rr)rr)r)r(rr})rrrr})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. )montuewedthufrisatsunnodayr rRcCs(|dkrd|jSd|j||fSdS)z/ Return a day as a table cell. rz z%dN)cssclass_noday cssclasses)rrWr 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)rrWrrrrszHTMLCalendar.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_monthrrr{rr)rrrrrrrrrrrs   zHTMLCalendar.formatmonthr}cCsg}|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,%sr8rGzzFrzzrr9) rr cssclass_yearcssclass_year_headrIrrrr)rrrrrr5rrhrrrrs(   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)rrrcssencodingrrrrrformatyearpage s$    zHTMLCalendar.formatyearpage)T)T)r})r}rN)r$r%r&rrrrrrrrrrrrrrrrrrrrrs    rc@s$eZdZddZddZddZdS)different_localecCs ||_dSrlocale)rrrrrr #szdifferent_locale.__init__cCs"ttj|_ttj|jdSr)_locale 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)rrWrrnamerrrr<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)rrWrrrrrZs 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)rrcolsrspacingrrrr=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) tuplerRr rWhourminutesecondrzhoursminutessecondsrrrrs    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--monthsr}zmonths 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))rchoicesrrR?zyear number (1-9999))nargsrrr zmonth number (1-12, text only)z/if --locale is specified --encoding is requiredrr)rrzincorrect number of arguments)rrrrh)#argparseArgumentParseradd_argument_group add_argumentint parse_argsrrerrorrexitrrrrdictrstdoutbufferwriterRrr0r1todayr rrrlinesrrrrr) rr parser textgroup htmlgroupoptionsrrroptdictrresultrrrmains                      r __main__)Irrr0rr itertoolsr__all__ ValueErrorrrrrrYrXr-rKrrrrrIrTUESDAY WEDNESDAYTHURSDAYFRIDAYSATURDAYrrrr r r]r^r_objectrrrrrrrrdrrr|r rrrrrr rr rrrr _colwidth_spacingr=rEPOCHr1rrrr r$argvrrrrsl  u   h