o
    IAf_                     @   s  	 d dl Z d dlZd dlZd dlmZ g dZeZG dd deZ	G dd deZ
dZd	Zg d
ZG dd dZG dd dZedZedZedZedZed\ZZZZZZZdd Zdd Zdd Zdd Zdd Z dd Z!d d! Z"G d"d# d#e#Z$G d$d% d%e$Z%G d&d' d'e$Z&G d(d) d)Z'G d*d+ d+e%Z(G d,d- d-e&Z)e% Z*e*j+Z,d.d/ Z-e*j.Z/e*j0Z0e*j1Z2e*j3Z4e*j5Z5e*j6Z7e*j8Z9e*j:Z;d0Z<d1Z=e<e=fd2d3Z>e<e=fd4d5Z?d6Z@eAe@ddB ZCd7d8 ZDd9d: ZEeFd;kreEe jG dS dS )<    N)repeat)IllegalMonthErrorIllegalWeekdayErrorsetfirstweekdayfirstweekdayisleapleapdaysweekday
monthrangemonthcalendarprmonthmonthprcalcalendartimegm
month_name
month_abbrday_nameday_abbrCalendarTextCalendarHTMLCalendarLocaleTextCalendarLocaleHTMLCalendar
weekheaderMONDAYTUESDAY	WEDNESDAYTHURSDAYFRIDAYSATURDAYSUNDAYc                   @      e Zd Zdd Zdd ZdS )r   c                 C   
   || _ d S Nr   )selfr    r'   /usr/lib/python3.10/calendar.py__init__      
zIllegalMonthError.__init__c                 C   
   d| j  S )Nz!bad month number %r; must be 1-12r%   r&   r'   r'   r(   __str__   r*   zIllegalMonthError.__str__N__name__
__module____qualname__r)   r-   r'   r'   r'   r(   r          r   c                   @   r"   )r   c                 C   r#   r$   r	   )r&   r	   r'   r'   r(   r)   "   r*   zIllegalWeekdayError.__init__c                 C   r+   )Nz7bad weekday number %r; must be 0 (Monday) to 6 (Sunday)r3   r,   r'   r'   r(   r-   $   r*   zIllegalWeekdayError.__str__Nr.   r'   r'   r'   r(   r   !   r2   r         )r         r6      r6   r8   r6   r6   r8   r6   r8   r6   c                   @   sF   e Zd Zdd edD Zeddd  dd Zd	d
 Zdd ZdS )_localized_monthc                 C   s    g | ]}t d |d djqS i  r4   datetimedatestrftime.0ir'   r'   r(   
<listcomp>6        z_localized_month.<listcomp>   r   c                 C      dS )N r'   )xr'   r'   r(   <lambda>7   s    z_localized_month.<lambda>c                 C   r#   r$   formatr&   rJ   r'   r'   r(   r)   9   r*   z_localized_month.__init__c                    0    j | }t|tr fdd|D S | jS )Nc                       g | ]}| j qS r'   rI   r@   fr,   r'   r(   rB   ?       z0_localized_month.__getitem__.<locals>.<listcomp>)_months
isinstanceslicerJ   r&   rA   Zfuncsr'   r,   r(   __getitem__<      


z_localized_month.__getitem__c                 C   rE   )N   r'   r,   r'   r'   r(   __len__C      z_localized_month.__len__N)	r/   r0   r1   rangerQ   insertr)   rU   rX   r'   r'   r'   r(   r9   4   s    r9   c                   @   s6   e Zd Zdd edD Zdd Zdd Zdd	 Zd
S )_localized_dayc                 C   s    g | ]}t d d|d jqS r:   r;   r?   r'   r'   r(   rB   J   rC   z_localized_day.<listcomp>   c                 C   r#   r$   rI   rK   r'   r'   r(   r)   L   r*   z_localized_day.__init__c                    rL   )Nc                    rM   r'   rI   rN   r,   r'   r(   rB   R   rP   z._localized_day.__getitem__.<locals>.<listcomp>)_daysrR   rS   rJ   rT   r'   r,   r(   rU   O   rV   z_localized_day.__getitem__c                 C   rE   Nr]   r'   r,   r'   r'   r(   rX   V   rY   z_localized_day.__len__N)r/   r0   r1   rZ   r^   r)   rU   rX   r'   r'   r'   r(   r\   G   s
    r\   z%Az%az%Bz%br]   c                 C   s&   	 | d dko| d dkp| d dkS )N   r   d     r'   )yearr'   r'   r(   r   f   s   $r   c                 C   sB   	 | d8 } |d8 }|d | d  |d | d   |d | d   S )Nr4   r`   ra   rb   r'   )Zy1Zy2r'   r'   r(   r   k   s   0r   c                 C   s:   	 t j|   krt jksn d| d  } t | || S )Ni  rb   )r<   ZMINYEARZMAXYEARr=   r	   )rc   r   dayr'   r'   r(   r	   s   s   r	   c                 C   sR   	 d|  krdkst | t |t| |d}t| |tko#t|  }||fS Nr4   rD   )r   r	   mdaysFebruaryr   )rc   r   day1ndaysr'   r'   r(   r
   z   s   r
   c                 C   s   t | |tko
t|  S r$   )rf   rg   r   rc   r   r'   r'   r(   	_monthlen   s   rk   c                 C   s    |dkr
| d dfS | |d fS re   r'   rj   r'   r'   r(   
_prevmonth      rl   c                 C   s    |dkr
| d dfS | |d fS )NrD   r4   r'   rj   r'   r'   r(   
_nextmonth   rm   rn   c                   @   s   e Zd Z	 d"ddZdd Zdd ZeeeZdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zd#ddZd#ddZd#dd Zd!S )$r   r   c                 C   r#   r$   r   r&   r   r'   r'   r(   r)      r*   zCalendar.__init__c                 C   s
   | j d S r_   Z_firstweekdayr,   r'   r'   r(   getfirstweekday   r*   zCalendar.getfirstweekdayc                 C   r#   r$   rq   rp   r'   r'   r(   r      r*   zCalendar.setfirstweekdayc                 c   s*    	 t | j| jd D ]}|d V  qd S r_   )rZ   r   )r&   rA   r'   r'   r(   iterweekdays   s
   zCalendar.iterweekdaysc                 c   s0    	 |  ||D ]\}}}t|||V  qd S r$   )itermonthdays3r<   r=   )r&   rc   r   ymdr'   r'   r(   itermonthdates   s
   zCalendar.itermonthdatesc                 c   sj    	 t ||\}}|| j d }td|E d H  td|d E d H  | j| | d }td|E d H  d S )Nr]   r   r4   )r
   r   r   rZ   )r&   rc   r   rh   ri   days_before
days_afterr'   r'   r(   itermonthdays   s   zCalendar.itermonthdaysc                 c   s4    	 t | ||| jD ]\}}||d fV  qd S r_   )	enumerater{   r   )r&   rc   r   rA   rw   r'   r'   r(   itermonthdays2   s
   zCalendar.itermonthdays2c                 c   s    	 t ||\}}|| j d }| j| | d }t||\}}t||d }	t|	| |	D ]}
|||
fV  q.td|d D ]}
|||
fV  q>t||\}}td|d D ]}
|||
fV  qUd S )Nr]   r4   )r
   r   rl   rk   rZ   rn   )r&   rc   r   rh   ri   ry   rz   ru   rv   endrw   r'   r'   r(   rt      s   zCalendar.itermonthdays3c                 c   s@    	 t | ||D ]\}\}}}|||| j| d fV  q
d S r_   )r|   rt   r   )r&   rc   r   rA   ru   rv   rw   r'   r'   r(   itermonthdays4   s
   zCalendar.itermonthdays4c                    0   	 t | ||  fddtdt dD S )Nc                       g | ]
} ||d   qS r]   r'   r?   Zdatesr'   r(   rB          z/Calendar.monthdatescalendar.<locals>.<listcomp>r   r]   )listrx   rZ   lenr&   rc   r   r'   r   r(   monthdatescalendar      zCalendar.monthdatescalendarc                    r   )Nc                    r   r   r'   r?   daysr'   r(   rB      r   z/Calendar.monthdays2calendar.<locals>.<listcomp>r   r]   )r   r}   rZ   r   r   r'   r   r(   monthdays2calendar   s   zCalendar.monthdays2calendarc                    r   )Nc                    r   r   r'   r?   r   r'   r(   rB      r   z.Calendar.monthdayscalendar.<locals>.<listcomp>r   r]   )r   r{   rZ   r   r   r'   r   r(   monthdayscalendar   r   zCalendar.monthdayscalendar   c                    @   	 fddt ttd D   fddt dt D S )Nc                       g | ]}  |qS r'   )r   r?   r&   rc   r'   r(   rB         
z.Calendar.yeardatescalendar.<locals>.<listcomp>rD   c                       g | ]
} ||  qS r'   r'   r?   monthswidthr'   r(   rB     r   r   rZ   Januaryr   r&   rc   r   r'   r   r&   r   rc   r(   yeardatescalendar   
    zCalendar.yeardatescalendarc                    r   )Nc                    r   r'   )r   r?   r   r'   r(   rB     r   z.Calendar.yeardays2calendar.<locals>.<listcomp>rD   c                    r   r'   r'   r?   r   r'   r(   rB     r   r   r   r   r'   r   r(   yeardays2calendar  r   zCalendar.yeardays2calendarc                    r   )Nc                    r   r'   )r   r?   r   r'   r(   rB      r   z-Calendar.yeardayscalendar.<locals>.<listcomp>rD   c                    r   r'   r'   r?   r   r'   r(   rB   $  r   r   r   r   r'   r   r(   yeardayscalendar  s
    zCalendar.yeardayscalendarN)r   r   )r/   r0   r1   r)   rr   r   propertyr   rs   rx   r{   r}   rt   r   r   r   r   r   r   r   r'   r'   r'   r(   r      s$    

	


r   c                   @   sh   e Zd Z	 dd Zdd Zdd Zdd Zd	d
 ZdddZdddZ	dddZ
dddZdddZdS ) r   c                 C   s   	 t | ||dd d S NrF   )r~   )print
formatweekr&   theweekr   r'   r'   r(   prweek-  s   zTextCalendar.prweekc                 C   s"   	 |dkrd}nd| }| |S )Nr   rF   z%2icenter)r&   rd   r	   r   sr'   r'   r(   	formatday3  s
   
zTextCalendar.formatdayc                    s   	 d  fdd|D S )N c                 3   s"    | ]\}}  ||V  qd S r$   r   r@   rw   Zwdr&   r   r'   r(   	<genexpr>A  s     z*TextCalendar.formatweek.<locals>.<genexpr>joinr   r'   r   r(   r   =  s   zTextCalendar.formatweekc                 C   s*   	 |dkrt }nt}|| d | |S )N	   )r   r   r   )r&   rd   r   namesr'   r'   r(   formatweekdayC  s
   zTextCalendar.formatweekdayc                    s    	 d  fdd  D S )Nr   c                 3   s    | ]	}  |V  qd S r$   r   r?   r   r'   r(   r   Q  s    z0TextCalendar.formatweekheader.<locals>.<genexpr>r   rs   r   r'   r   r(   formatweekheaderM  s   zTextCalendar.formatweekheaderTc                 C   s$   	 t | }|rd||f }||S )Nz%s %r)r   r   )r&   theyearthemonthr   withyearr   r'   r'   r(   formatmonthnameS  s
   
zTextCalendar.formatmonthnamer   c                 C   s   	 t | ||||dd d S r   )r   formatmonth)r&   r   r   wlr'   r'   r(   r   \  s   zTextCalendar.prmonthc                 C   s   	 t d|}t d|}| ||d|d  d }| }|d| 7 }|| | 7 }|d| 7 }| ||D ]}|| || 7 }|d| 7 }q7|S )Nr5   r4   r]   
)maxr   rstripr   r   r   )r&   r   r   r   r   r   weekr'   r'   r(   r   b  s   

zTextCalendar.formatmonthr5   r4      r   c              	      s  	 t d|}t d|}t d|}|d d d  g }|j}|t | ||d     |d|  |t|D ]\}}	t|| d t	||d  d d}
|d|   fdd|
D }|t
| |  |d|  fdd|
D }|t
| |  |d|  t d	d |	D }t|D ]0}g }|	D ]}|t|kr|d
 q||| | q|t
| |  |d|  qqCd
|S )Nr5   r4   r]   r   rW   c                 3   s     | ]} | d V  qdS )FN)r   r@   k)colwidthr&   r   r'   r(   r     s    z*TextCalendar.formatyear.<locals>.<genexpr>c                 3   s    | ]} V  qd S r$   r'   r   )headerr'   r(   r     s    c                 s   s    | ]}t |V  qd S r$   )r   )r@   calr'   r'   r(   r     s    rF   )r   appendreprr   r   r   r|   r   rZ   minformatstringr   r   r   )r&   r   r   r   crv   varA   rowr   r   ZheadersZheightjZweeksr   r'   )r   r   r&   r   r(   
formatyearr  s@   


&
$
	zTextCalendar.formatyearc                 C   s    	 t | |||||dd d S r   )r   r   )r&   r   r   r   r   rv   r'   r'   r(   pryear  s   zTextCalendar.pryearNT)r   r   )r5   r4   r   r   )r   r   r   r   )r/   r0   r1   r   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   r   '  s    



	

%r   c                   @   sv   e Zd Z	 g dZeZdZdZdZdZdZ	dd Z
dd Zd	d
 Zdd ZdddZdddZdddZdddZdS )r   )ZmonZtueZwedZthuZfriZsatZsunZnodayr   rc   c                 C   s&   	 |dkr
d| j  S d| j| |f S )Nr   z<td class="%s">&nbsp;</td>z<td class="%s">%d</td>)cssclass_noday
cssclasses)r&   rd   r	   r'   r'   r(   r     s   
zHTMLCalendar.formatdayc                    s"   	 d  fdd|D }d| S )NrF   c                 3   s     | ]\}}  ||V  qd S r$   r   r   r,   r'   r(   r     s    z*HTMLCalendar.formatweek.<locals>.<genexpr><tr>%s</tr>r   )r&   r   r   r'   r,   r(   r     s   zHTMLCalendar.formatweekc                 C   s   	 d| j | t| f S )Nz<th class="%s">%s</th>)cssclasses_weekday_headr   r&   rd   r'   r'   r(   r     s   zHTMLCalendar.formatweekdayc                    s&   	 d  fdd  D }d| S )NrF   c                 3   s    | ]}  |V  qd S r$   r   r?   r,   r'   r(   r         z0HTMLCalendar.formatweekheader.<locals>.<genexpr>r   r   )r&   r   r'   r,   r(   r     s   zHTMLCalendar.formatweekheaderTc                 C   s2   	 |rdt | |f }ndt |  }d| j|f S )Nz%s %sz%sz+<tr><th colspan="7" class="%s">%s</th></tr>)r   cssclass_month_head)r&   r   r   r   r   r'   r'   r(   r     s   zHTMLCalendar.formatmonthnamec                 C   s   	 g }|j }|d| j  |d || j|||d |d ||   |d | ||D ]}|| | |d q/|d |d d|S )N=<table border="0" cellpadding="0" cellspacing="0" class="%s">r   r   </table>rF   )r   cssclass_monthr   r   r   r   r   )r&   r   r   r   r   r   r   r'   r'   r(   r     s"   

zHTMLCalendar.formatmonthr   c                 C   s   	 g }|j }t|d}|d| j  |d |d|| j|f  tttd |D ]+}t|t|| d}|d |D ]}|d || j||d	d
 |d q:|d q(|d d|S )Nr4   r   r   z,<tr><th colspan="%d" class="%s">%s</th></tr>rD   rW   z<tr>z<td>Fr   z</td>z</tr>r   rF   )	r   r   cssclass_yearcssclass_year_headrZ   r   r   r   r   )r&   r   r   r   r   rA   r   rv   r'   r'   r(   r     s*   



zHTMLCalendar.formatyearcalendar.cssNc                 C   s   	 |d u r	t  }g }|j}|d|  |d |d |d |d|  |d ur0|d|  |d|  |d |d	 || || |d
 |d d||dS )Nz$<?xml version="1.0" encoding="%s"?>
zn<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
z<html>
z<head>
zC<meta http-equiv="Content-Type" content="text/html; charset=%s" />
z4<link rel="stylesheet" type="text/css" href="%s" />
z<title>Calendar for %d</title>
z</head>
z<body>
z</body>
z</html>
rF   xmlcharrefreplace)sysgetdefaultencodingr   r   r   encode)r&   r   r   cssencodingr   r   r'   r'   r(   formatyearpage  s&   zHTMLCalendar.formatyearpager   r   )r   r   N)r/   r0   r1   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   r     s"    



r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )different_localec                 C   r#   r$   locale)r&   r   r'   r'   r(   r)   %  r*   zdifferent_locale.__init__c                 C   s"   t t j| _t t j| j d S r$   )_locale	getlocaleLC_TIME	oldlocale	setlocaler   r,   r'   r'   r(   	__enter__(  s   zdifferent_locale.__enter__c                 G   s   t t j| j d S r$   )r   r   r   r   )r&   argsr'   r'   r(   __exit__,  s   zdifferent_locale.__exit__N)r/   r0   r1   r)   r   r   r'   r'   r'   r(   r   $  s    r   c                       6   e Zd Z	 d
ddZ fddZd fdd		Z  ZS )r   r   Nc                 C   &   t | | |d u rt }|| _d S r$   )r   r)   r   getdefaultlocaler   r&   r   r   r'   r'   r(   r)   8     
zLocaleTextCalendar.__init__c                    s<   t | j t ||W  d    S 1 sw   Y  d S r$   r   r   superr   )r&   rd   r   	__class__r'   r(   r   >  s   $z LocaleTextCalendar.formatweekdayTc                    s@   t | j t ||||W  d    S 1 sw   Y  d S r$   r   r   r   r   )r&   r   r   r   r   r   r'   r(   r   B  s   $z"LocaleTextCalendar.formatmonthnamer   Nr   r/   r0   r1   r)   r   r   __classcell__r'   r'   r   r(   r   0  s
    
r   c                       r   )r   r   Nc                 C   r   r$   )r   r)   r   r   r   r   r'   r'   r(   r)   N  r   zLocaleHTMLCalendar.__init__c                    s:   t | j t |W  d    S 1 sw   Y  d S r$   r   r   r   r'   r(   r   T  s   
$z LocaleHTMLCalendar.formatweekdayTc                    s>   t | j t |||W  d    S 1 sw   Y  d S r$   r   )r&   r   r   r   r   r'   r(   r   X  s   $z"LocaleHTMLCalendar.formatmonthnamer   r   r   r'   r'   r   r(   r   G  s
    
r   c                 C   s.   t |   krtkst|  t| | t_d S r$   )r   r!   r   r   r   ro   r'   r'   r(   r   a  s
   
r      r   c                 C   s   	 t t| || d S r$   )r   r   Zcolsr   spacingr'   r'   r(   rJ   u  s   rJ   c                    s"   	 |d9 }|  fdd| D S )Nr   c                 3   s    | ]}|  V  qd S r$   r   )r@   r   r   r'   r(   r   }  r   zformatstring.<locals>.<genexpr>r   r  r'   r  r(   r   z  s   r   i  c                 C   s`   	 | d d \}}}}}}t ||d t | d }|d | }|d | }	|	d | }
|
S )Nr   r4      <   )r<   r=   	toordinal
_EPOCH_ORD)tuplerc   r   rd   ZhourZminutesecondr   ZhoursZminutesZsecondsr'   r'   r(   r     s   r   c                 C   s  dd l }| }|d}|d}|jddtddd |jd	d
tddd |jddtddd |jddtddd |jddddd |jddd dd |jddd dd |jd d!d"d#d$d% |jd&d'td(d) |jd*d'td+d) || dd  }|jr|js|d, t	
d |j|jf}|jd-kr|jrt|d.}nt }|j}|d u rt	 }t||jd/}	t	jjj}
|jd u r|
|jtj jfi |	 d S |jd u r|
|j|jfi |	 d S |d0 t	
d d S |jrt|d.}nt }t|j|jd1}	|jd u r|j|	d2< |j|	d3< |jd u r%|j tj jfi |	}n|jd u r6|j |jfi |	}n|j!|j|jfi |	}t	jj}
|jrU|"|j}t	jjj}
|
| d S )4Nr   ztext only argumentszhtml only argumentsz-wz--widthr5   z width of date column (default 2))typedefaulthelpz-lz--linesr4   z)number of lines for each week (default 1)z-sz	--spacingr   z"spacing between months (default 6)z-mz--monthsr   zmonths per row (default 3)z-cz--cssr   zCSS to use for page)r  r  z-Lz--localez.locale to be used from month and weekday namesz-ez
--encodingzencoding to use for outputz-tz--typetext)r  htmlzoutput type (text or html))r  choicesr  rc   ?zyear number (1-9999))nargsr  r  r   zmonth number (1-12, text only)z/if --locale is specified --encoding is requiredr  r   )r   r   zincorrect number of arguments)r   r   r   rv   )#argparseArgumentParseradd_argument_groupadd_argumentint
parse_argsr   r   errorr   exitr  r   r   r   dictr   stdoutbufferwriterc   r   r<   r=   Ztodayr   r   r   r   linesr  r   r   r   r   )r   r  parserZ	textgroupZ	htmlgroupoptionsr   r   r   Zoptdictr  resultr'   r'   r(   main  s   






"




r$  __main__)Hr   r<   r   r   	itertoolsr   __all__
ValueErrorr  r   r   r   rg   rf   r9   r\   r   r   r   r   rZ   r   r   r   r   r   r    r!   r   r   r	   r
   rk   rl   rn   objectr   r   r   r   r   r   r   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z	_colwidthZ_spacingrJ   r   ZEPOCHr=   r  r	  r   r$  r/   argvr'   r'   r'   r(   <module>   sp    

 u 	
h