\d`JdZddlZddlZddlZddlmZgdZeZ GddeZ GddeZ d Z d Z gd ZGd d ZGddZedZedZedZedZed\ZZZZZZZdZdZdZdZ dZ!dZ"dZ#Gdde$Z%Gdde%Z&Gd d!e%Z'Gd"d#Z(d$Z)Gd%d&e&Z*Gd'd(e'Z+e&Z,e,j-Z.d)Z/e,j0Z1e,j2Z2e,j3Z4e,j5Z6e,j7Z7e,j8Z9e,j:Z;e,j<Z=d*Z>d+Z?e>e?fd,Z@e>e?fd-ZAd.ZBejCeBd d DZEd/ZFd0ZGeHd1kreGejIdSdS)2a$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)repeat)IllegalMonthErrorIllegalWeekdayErrorsetfirstweekday firstweekdayisleapleapdaysweekday monthrange monthcalendarprmonthmonthprcalcalendartimegm month_name month_abbrday_nameday_abbrCalendar TextCalendar HTMLCalendarLocaleTextCalendarLocaleHTMLCalendar weekheaderMONDAYTUESDAY WEDNESDAYTHURSDAYFRIDAYSATURDAYSUNDAYceZdZdZdZdS)rc||_dSNr)selfrs ..\python\lib\calendar.py__init__zIllegalMonthError.__init__s  cd|jzS)Nz!bad month number %r; must be 1-12r&r's r(__str__zIllegalMonthError.__str__s2TZ??r*N__name__ __module__ __qualname__r)r-r*r(rrs7@@@@@r*rceZdZdZdZdS)rc||_dSr%r )r'r s r(r)zIllegalWeekdayError.__init__"s  r*cd|jzS)Nz7bad weekday number %r; must be 0 (Monday) to 6 (Sunday)r5r,s r(r-zIllegalWeekdayError.__str__$sH4<WWr*Nr.r2r*r(rr!s7XXXXXr*r) rr9r9r;r9r9r;r9r;r9cxeZdZdedDZedddZdZdZdS) _localized_monthcJg|] }tjd|dzdj!Sir7datetimedatestrftime.0is r( z_localized_month.6s-GGGx}T1Q3**3GGGr* rcdS)Nr2)xs r(z_localized_month.7sr*c||_dSr%formatr'rOs r(r)z_localized_month.__init__9  r*cj|}t|trfd|DS|jS)Nc0g|]}|jSr2rNrEfr's r(rGz0_localized_month.__getitem__..?#222qAAdkNN222r*)_months isinstanceslicerOr'rFfuncss` r( __getitem__z_localized_month.__getitem__<sL Q a   &2222E222 25%% %r*cdS)N r2r,s r(__len__z_localized_month.__len__Csrr*N) r/r0r1rangerWinsertr)r\r_r2r*r(r=r=4srGGUU2YYGGGG NN1ll###&&&r*r=cJeZdZdedDZdZdZdZdS)_localized_daycJg|] }tjdd|dzj!Sr?r@rDs r(rGz_localized_day.Js- D D DaX]4AaC ( ( 1 D D Dr*c||_dSr%rNrPs r(r)z_localized_day.__init__LrQr*cj|}t|trfd|DS|jS)Nc0g|]}|jSr2rNrTs r(rGz._localized_day.__getitem__..RrVr*)_daysrXrYrOrZs` r(r\z_localized_day.__getitem__OsL 1  a   &2222E222 25%% %r*cdSNrer2r,s r(r_z_localized_day.__len__Vsqr*N)r/r0r1r`rir)r\r_r2r*r(rcrcGs_ E D5588 D D DE&&&r*rcz%Az%az%Bz%brec8|dzdko|dzdkp|dzdkS)z5Return True for leap years, False for non-leap years.rdr2)years r(rrfs* !8q= AdSjAo@qAr*c\|dz}|dz}|dz|dzz |dz|dzz z |dz|dzz zS)zFReturn number of leap years in range [y1, y2). Assume y1 <= y2.r7rmrnror2)y1y2s r(r r ksL!GB!GB EBEMb#gC/ 0BGb#g4E FFr*ctj|cxkrtjks nd|dzz}tj|||S)zBReturn weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31).iro)rAMINYEARMAXYEARrBr )rprdays r(r r ssU  t 7 7 7 7x'7 7 7 7 7dSj  =uc * * 2 2 4 44r*cd|cxkrdksnt|t||d}t||tkot |z}||fS)zQReturn weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month.r7rH)rr mdaysFebruaryr)rprday1ndayss r(r r zsj         &&& 4 " "D %LEX->&,, ?E ;r*cRt||tkot|zSr%)ryrzrrprs r( _monthlenrs! <5H,= >>r*c*|dkr|dz dfS||dz fS)Nr7rHr2r~s r( _prevmonthrs' zzAvrzU1W}r*c*|dkr|dzdfS||dzfS)NrHr7r2r~s r( _nextmonthrs' {{AvqyU1W}r*ceZdZdZddZdZdZeeeZdZ dZ dZ d Z d Z d Zd Zd ZdZddZddZddZdS)rzo Base calendar class. This class doesn't do any formatting. It simply provides data to subclasses. rc||_dSr%rr'rs r(r)zCalendar.__init__s(r*c|jdzSrk _firstweekdayr,s r(getfirstweekdayzCalendar.getfirstweekdays!A%%r*c||_dSr%rrs r(rzCalendar.setfirstweekdays)r*c#XKt|j|jdzD] }|dzV dS)zt Return an iterator for one week of weekday numbers starting with the configured first one. reN)r`r)r'rFs r( iterweekdayszCalendar.iterweekdayssE t($*;a*?@@  AA#IIII  r*c#tK|||D]\}}}tj|||VdS)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)itermonthdays3rArB)r'rprymds r(itermonthdateszCalendar.itermonthdatessT **477 ) )GAq!-1a(( ( ( ( ( ) )r*c#Kt||\}}||jz dz}td|Ed{Vtd|dzEd{V|j|z |z dz}td|Ed{VdS)z Like itermonthdates(), but will yield day numbers. For days outside the specified month the day number is 0. rerNr7)r rrr`)r'rprr{r| days_before days_afters r( itermonthdayszCalendar.itermonthdayss !u-- ed//14 ![)))))))))EAI&&&&&&&&&'$.6!; !Z(((((((((((r*c#zKt||||jD]\}}||dzfVdS)z Like itermonthdates(), but will yield (day number, weekday number) tuples. For days outside the specified month the day number is 0. reN) enumeraterr)r'rprrFrs r(itermonthdays2zCalendar.itermonthdays2sU d00u==t?PQQ  DAqQU(NNNN  r*c#Kt||\}}||jz dz}|j|z |z dz}t||\}}t||dz} t | |z | D] } ||| fV t d|dzD] } ||| fV t ||\}}t d|dzD] } ||| fV dS)z Like itermonthdates(), but will yield (year, month, day) tuples. Can be used for dates outside of datetime.date range. rer7N)r rrrr`r) r'rprr{r|rrrrendrs r(rzCalendar.itermonthdays3s !u-- ed//14 '$.6!; $&&11oo!s;,,  AQ'MMMMq%!)$$ ! !Aq. $&&1q*q.))  AQ'MMMM  r*c#Kt|||D]\}\}}}||||j|zdzfVdS)z Like itermonthdates(), but will yield (year, month, day, day_of_week) tuples. Can be used for dates outside of datetime.date range. reN)rrr)r'rprrFrrrs r(itermonthdays4zCalendar.itermonthdays4sh &d&9&9$&F&FGG 7 7LAy1aQD-1Q66 6 6 6 6 7 7r*ct|||fdtdtdDS)z Return a matrix (list of lists) representing a month's calendar. Each row represents a week; week entries are datetime.date values. c*g|]}||dzSrer2)rErFdatess r(rGz/Calendar.monthdatescalendar..s%@@@!q1u@@@r*rre)listrr`len)r'rprrs @r(monthdatescalendarzCalendar.monthdatescalendarsN T((u5566@@@@uQE A'>'>@@@@r*ct|||fdtdtdDS)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. c*g|]}||dzSrr2rErFdayss r(rGz/Calendar.monthdays2calendar..%>>>a!e>>>r*rre)rrr`rr'rprrs @r(monthdays2calendarzCalendar.monthdays2calendarsND''e4455>>>>eAs4yy!&<&<>>>>r*ct|||fdtdtdDS)z Return a matrix representing a month's calendar. Each row represents a week; days outside this month are zero. c*g|]}||dzSrr2rs r(rGz.Calendar.monthdayscalendar..rr*rre)rrr`rrs @r(monthdayscalendarzCalendar.monthdayscalendarsN D&&tU3344>>>>eAs4yy!&<&<>>>>r*cfdtttdzDfdtdtDS)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. c<g|]}|Sr2)rrErFr'rps r(rGz.Calendar.yeardatescalendar..9     # #D! , ,   r*rHc*g|]}||zSr2r2rErFmonthswidths r(rGz.Calendar.yeardatescalendar.. &IIIaq5y!IIIr*rr`Januaryrr'rprrs```@r(yeardatescalendarzCalendar.yeardatescalendars     7GBJ//   JIIII5CKK+G+GIIIIr*cfdtttdzDfdtdtDS)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. c<g|]}|Sr2)rrs r(rGz.Calendar.yeardays2calendar..rr*rHc*g|]}||zSr2r2rs r(rGz.Calendar.yeardays2calendar..rr*rrrs```@r(yeardays2calendarzCalendar.yeardays2calendar rr*cfdtttdzDfdtdtDS)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. c<g|]}|Sr2)rrs r(rGz-Calendar.yeardayscalendar.. s9     " "4 + +   r*rHc*g|]}||zSr2r2rs r(rGz-Calendar.yeardayscalendar..$rr*rrrs```@r(yeardayscalendarzCalendar.yeardayscalendarss      7GBJ//   JIIII5CKK+G+GIIIIr*N)rr)r/r0r1__doc__r)rrpropertyrrrrrrrrrrrrrr2r*r(rrs7 ))))&&&***8O_==L))) ) ) )$777AAA?????? J J J J J J J J J J J J J Jr*rcXeZdZdZdZdZdZdZdZddZ dd Z dd Z ddZ ddZ dS)rzr Subclass of Calendar that outputs a calendar as a simple plain text similar to the UNIX program cal. cPt|||ddS)z3 Print a single week (no newline). rJrN)print formatweekr'theweekrs r(prweekzTextCalendar.prweek-s+ doogu--2666666r*cH|dkrd}nd|z}||S)z* Returns a formatted day. rrJz%2icenter)r'rwr rss r( formatdayzTextCalendar.formatday3s- !88AA Axxr*cJdfd|DS)zA Returns a single week in a string (no newline).  c3LK|]\}}||VdSr%r)rErwdr'rs r( z*TextCalendar.formatweek..As7LL!Rq"e44LLLLLLr*joinrs` `r(rzTextCalendar.formatweek=s0xxLLLLLGLLLLLLr*cr|dkrt}nt}||d||S)z4 Returns a formatted week day name. N)rrr)r'rwrnamess r( formatweekdayzTextCalendar.formatweekdayCs: A::EEESz&5&!((///r*cndfdDS)z- Return a header for a week. rc3DK|]}|VdSr%r)rErFr'rs r(rz0TextCalendar.formatweekheader..Qs3RR**1e44RRRRRRr*rr)r'rs``r(formatweekheaderzTextCalendar.formatweekheaderMs<xxRRRRRd>O>O>Q>QRRRRRRr*TcXt|}|r|d|}||S)z0 Return a formatted month name. r)rr)r'theyearthemonthrwithyearrs r(formatmonthnamezTextCalendar.formatmonthnameSs6 x   '11gg&Axxr*rcTt|||||ddS)z+ Print a month's calendar. rJrN)r formatmonth)r'rrwls r(r zTextCalendar.prmonth\s1 dw!Q77R@@@@@@r*ctd|}td|}|||d|dzzdz }|}|d|zz }|||z }|d|zz }|||D]5}||||z }|d|zz }6|S)z@ Return a month's calendar string (multi-line). r8r7re )maxrrstriprrr)r'rrrrrweeks r(rzTextCalendar.formatmonthbs 1II 1II  (AQK!O D D HHJJ TAX  T " "1 % % , , . .. TAX ++GX>>  D q))0022 2A MAAr*r8r7rc  td|}td|}td|}|dzdzdz g}|j}|t|z||dz zz|d|z|t |D]\}} t||zdzt||dzzdzd} |d|zfd| D} |t| ||d|zfd| D} |t| ||d|ztd| D} t| D]}g}| D]Z}|t|kr|d +| |||[|t|||d|zd |S) zC Returns a year's calendar as a multi-line string. r8r7rerr^c3HK|]}|dVdS)FN)r)rEkcolwidthr'rs r(rz*TextCalendar.formatyear..sK&&))'1hFF&&&&&&r*c3K|]}VdSr%r2)rErheaders r(rz*TextCalendar.formatyear..s#..!v......r*c34K|]}t|VdSr%)r)rEcals r(rz*TextCalendar.formatyear..s(11cS111111r*rJ)rappendreprrrrrrr`min formatstringrrr)r'rrrcrvarFrowrrheadersheightjweeksrrrs`` @@r( formatyearzTextCalendar.formatyearrs 1II 1II 1IIEQ;?  H $w--  xz!QqS'1 2 2 9 9 ; ;<<< $q& &&q))!$"8"8!"D"DEE  HQ1Q3q5#a1gai"4"455F Ad1fIII&&&&&&$&&&E Al5(A..5577 8 8 8 Ad1fIII....v...G Al7Ha007799 : : : Ad1fIII11S11111F6]]  AACCHH}} R(((( T__SVQ%?%?@@@@,uh2299;;<<<$(  wwqzzr*c Vt||||||ddS)zPrint a year's calendar.rJrN)rr )r'rrrrrs r(pryearzTextCalendar.pryears/ doogq!Q22;;;;;;r*NT)rr)r8r7rr)rrrr)r/r0r1rrrrrrrr rr rr2r*r(rr's 777 MMM 000SSS AAAA  ####J<<<<< z%d)cssclass_noday cssclasses)r'rwr s r(rzHTMLCalendar.formatdays2 !88/$2EE E+tw/G.MM Mr*cPdfd|D}d|zS)z8 Return a complete week as a table row. rJc3JK|]\}}||VdSr%r)rErrr's r(rz*HTMLCalendar.formatweek..s5AAgq"DNN1b))AAAAAAr* %sr)r'rrs` r(rzHTMLCalendar.formatweeks6 GGAAAAAAA A Aq  r*c@d|j|dt|dS)z: Return a weekday name as a table header. z z)cssclasses_weekday_headr)r'rws r(rzHTMLCalendar.formatweekdays+  ( - - -x}}}> >r*ctdfdD}d|zS)z< Return a header for a week as a table row. rJc3BK|]}|VdSr%r)rErFr's r(rz0HTMLCalendar.formatweekheader..s1GGaD&&q))GGGGGGr*rr)r'rs` r(rzHTMLCalendar.formatweekheadersB GGGGGG43D3D3F3FGGG G Gq  r*Tch|rt|d|}ndt|z}d|jd|dS)z5 Return a month name as a table row. rz%szrrrJ)rcssclass_monthrrrrr)r'rrrrrrs r(rzHTMLCalendar.formatmonths  H  I   ! " " " $ $  w8  D DEEE $ $   ! !""" $++GX>>  D Adood## $ $ $ AdGGGG *  $wwqzzr*rc g}|j}t|d}|d|jz|d|d||j|fzt t t dz|D]u}t |t ||zd}|d|D]9}|d||||d |d :|d v|d d|S)z? Return a formatted year as a table of tables. r7r'rz,%srHr^zzFr(zzr)rJ) rr cssclass_yearcssclass_year_headr`rrrr)r'rrrrrFrrs r(r zHTMLCalendar.formatyears<  HE1   I       $  8 4*G<5 5 6 6 6w E22  A1c!E'2..//F AfIII  & $""7A">>???' AgJJJJ * wwqzzr* calendar.cssNc|tj}g}|j}|d|z|d|d|d|d|z||d|z|d|z|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 rJxmlcharrefreplace)sysgetdefaultencodingrr rencode)r'rrcssencodingrrs r(formatyearpagezHTMLCalendar.formatyearpage s   -//H  H  1H <===  {||| *  *   PS[ [\\\ ? AEK L L L  ,w 6777 + *  $//'5 ) )*** + +wwqzz  +>???r*rr)rr.N)r/r0r1rrr!rr%r*r-r,rrrrrrr r6r2r*r(rrs CBBJ)N"N MNNN!!!>>>!!! ) ) ) )(0@@@@@@r*rc eZdZdZdZdZdS)different_localec"||_d|_dSr%)locale oldlocale)r'r:s r(r)zdifferent_locale.__init__%s r*ctjtjd|_tjtj|jdSr%)_locale setlocaleLC_TIMEr;r:r,s r( __enter__zdifferent_locale.__enter__)s5 *7?DAA'/4;77777r*c`|jdStjtj|jdSr%)r;r=r>r?)r'argss r(__exit__zdifferent_locale.__exit__-s, > ! F'/4>:::::r*N)r/r0r1r)r@rCr2r*r(r8r8$sA888;;;;;r*r8ctjtjd}|dkrFtd5tjtjd}dddn #1swxYwY|S)NCrJ)r=r>r?r8r:s r(_get_default_localerG3s  w 5 5F }} b ! ! > >&w==F > > > > > > > > > > > > > > > Ms A!!A%(A%c6eZdZdZddZfdZd fd ZxZS) r This class can be passed a locale name in the constructor and will return month and weekday names in the specified locale. rNcjt|||t}||_dSr%)rr)rGr:r'rr:s r(r)zLocaleTextCalendar.__init__C3dL111 >(**F r*ct|j5t||cdddS#1swxYwYdSr%r8r:superr)r'rwr __class__s r(rz LocaleTextCalendar.formatweekdayIs dk * * 5 577((e44 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5s"AA  A Tct|j5t||||cdddS#1swxYwYdSr%r8r:rOr)r'rrrrrPs r(rz"LocaleTextCalendar.formatmonthnameMs dk * * O O77**7HeXNN O O O O O O O O O O O O O O O O O Os$AA A rNrr/r0r1rr)rr __classcell__rPs@r(rr=s~  55555OOOOOOOOOOr*rc6eZdZdZddZfdZd fd ZxZS) rrIrNcjt|||t}||_dSr%)rr)rGr:rKs r(r)zLocaleHTMLCalendar.__init__WrLr*ct|j5t|cdddS#1swxYwYdSr%rN)r'rwrPs r(rz LocaleHTMLCalendar.formatweekday]s dk * * . .77((-- . . . . . . . . . . . . . . . . . .s!AA ATct|j5t|||cdddS#1swxYwYdSr%rR)r'rrrrPs r(rz"LocaleHTMLCalendar.formatmonthnameas dk * * H H77**7HhGG H H H H H H H H H H H H H H H H H Hs#AA  A rSrrTrVs@r(rrRs~ .....HHHHHHHHHHr*rcnt|cxkr tksnt||t_dSr%)rr"rrrrs r(rrjs: \ + + + +V + + + +!,///!ANNNr*rcBtt|||dS)z1Prints multi-column formatting for year calendarsN)rrcolsrspacings r(rOrO~s" ,tXw / /00000r*cP|dz}|fd|DS)zEReturns a string formatted from n strings, centered within n columns.rc3BK|]}|VdSr%r)rErrs r(rzformatstring..s/99q**999999r*rr^s ` r(rrs3 sNG <<9999D999 9 99r*ic|dd\}}}}}}tj||dtz |zdz }|dz|z}|dz|z} | dz|z} | S)zBUnrelated but handy function to calculate Unix timestamp from GMT.Nrr7<)rArB toordinal _EPOCH_ORD) tuplerprrwhourminutesecondrhoursminutessecondss r(rrsx-22A2Y*D%dFF =ua ( ( 2 2 4 4z AC G! KD GdNEBhGbj6!G Nr*cddl}|}|d}|d}|ddtdd|d d td d |d dtdd|ddtdd|dddd|dddd|dddd|d d!d"d#d$%|d&d'td()|d*d'td+)||d d}|jr0|js)|d,tj d |j|jf}|j d-kr|jrt|.}nt}|j}|tj}t||j/} tjjj} |j:| |jt,jjfi| dS|j| |j|jfi| dS|d0tj d dS|jrt5|.}nt7}t|j|j1} |j|j| d2<|j| d3<|j0|j t,jjfi| } n4|j|j |jfi| } n|j!|j|jfi| } tjj} |jr0| "|j} tjjj} | | dS)4Nrztext only argumentszhtml only argumentsz-wz--widthr8z width of date column (default 2))typedefaulthelpz-lz--linesr7z)number of lines for each week (default 1)z-sz --spacingrz"spacing between months (default 6)z-mz--monthsrzmonths per row (default 3)z-cz--cssr.zCSS to use for page)rqrrz-Lz--localez.locale to be used from month and weekday namesz-ez --encodingzencoding to use for outputz-tz--typetext)rshtmlzoutput type (text or html))rqchoicesrrrp?zyear number (1-9999))nargsrprrrzmonth number (1-12, text only)z/if --locale is specified --encoding is requiredrtrF)r5r4zincorrect number of arguments)rrrr)#argparseArgumentParseradd_argument_group add_argumentint parse_argsr:r5errorr1exitrprrr2dictr4stdoutbufferwriterpr6rArBtodayrrrrlinesr`rr rr3) rBrxparser textgroup htmlgroupoptionsr:rr5optdictrresults r(mainrswOOO  $ $ & &F))*?@@I))*?@@I  i ! /  i ! 8  k ! 1  j ! )  g "   j =   l )   h )    #   - QRR))G~g. FGGG  ^W- -F|v > !$F333CC..C#  -//Hgk::: !' <  E$#$X]%8%8%:%:%?KK7KK L L L L L ] " E$#$W\==W== > > > > > LL8 9 9 9 HQKKKKK > !$F333CC..C'-888 = "?GCL">GCL < #S^HM$7$7$9$9$>JJ'JJFF ] "#S^GL<rsO77  ! ! ! @@@@@ @@@XXXXX*XXX   <;;&( >$   >$   d # #  d # # DI588@)XvxBBB GGG555???NJNJNJNJNJvNJNJNJbr<r<r<r<r<8r<r<r