U
    ¤�©j‹$  ã                   @   sª  zd dl mZ W n ek
r,   d dlZY nX d dlmZ d dlZd dlZddlmZ zd dl	Z	W n ek
rz   dZ	Y nX d dl
Z
d dlZd dlZd dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ e	dk	Ze�re	je	 d¡gZdd„ gZdgZng Zg Zg ZG dd„ dƒZeeƒj Z!e!d Z"e!d Z#e!d Z$e!d Z%ej&dd�dd„ ƒZ'ej&dd�dd„ ƒZ(ej&dd�dd„ ƒZ)ej&dd�dd„ ƒZ*ej&ee +d¡ej,e -d¡g d�dd„ ƒZej&edd„ dd„ g d�d d!„ ƒZ.d"Z/d#d$„ e
 0e" 1¡ e# 1¡ e$ 1¡ ¡D ƒZ2ej&e2d�d%d&„ ƒZ3d'd$„ e" 1¡ D ƒZ4ej&e4d�d(d)„ ƒZ5ej&d*d+„ ƒZ6ej&d,d-„ ƒZ7ej&d.d/„ ƒZ8ej&d0d1„ ƒZ9ej&d2d3„ ƒZ:e:Z;ej&d4d5„ ƒZ<ej&d6d7„ ƒZ=e &¡ d8d9„ ƒZ>ej&dd�eeddf d:œd;d<„ƒZej&ed=g d�d>d?„ ƒZ?ej&e@d:œd@dA„ƒZAej&e@d:œdBdC„ƒZBej&e@d:œdDdE„ƒZCej&e@d:œdFdG„ƒZDdHdI„ ZEG dJdK„ dKej+ƒZFdLdM„ ZGdNdO„ ZHej&dPdQ„ ƒZIej&ejJd�ejKe@dRœdSdT„ƒZLdS )Ué    )ÚzoneinfoN)Ú	Generatoré   )Útimezone_ids)ÚPath)Útz)ÚCalendarÚ	Component)ÚTZP©ÚtzpÚUTCc                 C   s   t  |¡ | ¡S ©N)ÚpytzÚtimezoneZlocalize©ÚdtÚtzname© r   ú</tmp/pip-unpacked-wheel-g40nyxg1/icalendar/tests/conftest.pyÚ<lambda>#   ó    r   r   c                   @   sR   e Zd ZdZedœdd„Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
edd„ ƒZdS )Ú
DataSourcezFA collection of parsed ICS elements (e.g calendars, timezones, events))Údata_source_folderc                 C   s   || _ || _d S r   )Ú_parserÚ_data_source_folder)Úselfr   Úparserr   r   r   Ú__init__/   s    zDataSource.__init__c                 C   s   dd„ | j  ¡ D ƒS )z(Return all the files that could be used.c                 S   s    g | ]}|j  ¡ d kr|j‘qS )ú.ics)ÚsuffixÚlowerÚstem)Ú.0Úpr   r   r   Ú
<listcomp>5   s   þz#DataSource.keys.<locals>.<listcomp>)r   Úiterdir©r   r   r   r   Úkeys3   s    þzDataSource.keysc              	   C   s�   |  d¡r|}|dd… }n|d }| j| }| ¡ sDt|› d�ƒ‚| d¡�}| ¡ }W 5 Q R X |  |¡}t|tƒs‚||_	||_
|| j|< |S )z;Parse a file and return the result stored in the attribute.r   Néüÿÿÿz does not exist.Úrb)Úendswithr   Úis_fileÚAttributeErrorÚopenÚreadr   Ú
isinstanceÚlistÚraw_icsÚsource_fileÚ__dict__)r   Ú	attributer3   Úsource_pathÚfr2   Úsourcer   r   r   Ú__getitem__;   s    




zDataSource.__getitem__c                 C   s"   |  d¡r|dd… }||  ¡ kS )zkey in self.keys()r   Nr)   )r+   r(   ©r   Úkeyr   r   r   Ú__contains__N   s    
zDataSource.__contains__c                 C   s   | | S r   r   r:   r   r   r   Ú__getattr__T   s    zDataSource.__getattr__c                 C   s
   t | jƒS r   )Úreprr4   r'   r   r   r   Ú__repr__W   s    zDataSource.__repr__c                    s   ˆ   ˆ j‡ fdd„¡S )z'Return a list of all components parsed.c                    s   ˆ j | dd�S )NT)Úmultiple)r   )Údatar'   r   r   r   ^   r   z%DataSource.multiple.<locals>.<lambda>)Ú	__class__r   r'   r   r'   r   r@   Z   s     
ÿzDataSource.multipleN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r(   r9   r<   r=   r?   Úpropertyr@   r   r   r   r   r   ,   s   r   Ú	calendarsÚ	timezonesÚeventsÚalarmsÚmodule©Zscopec                 C   s   t ttjjƒS r   )r   ÚCALENDARS_FOLDERÚ	icalendarr   Ú	from_icalr   r   r   r   rH   i   s    c                 C   s   t ttjjƒS r   )r   ÚTIMEZONES_FOLDERrO   ZTimezonerP   r   r   r   r   rI   n   s    c                 C   s   t ttjjƒS r   )r   ÚEVENTS_FOLDERrO   ÚEventrP   r   r   r   r   rJ   s   s    c                 C   s   t ttjjƒS r   )r   ÚALARMS_FOLDERrO   ZAlarmrP   r   r   r   r   rK   x   s    )Úparamsc                 C   s   | j S r   ©Úparam©Úrequestr   r   r   r   Úutc}   s    rZ   c                 C   s   | j t |¡d�S ©N)Útzinfo)Úreplacer   Úgettzr   r   r   r   r   …   r   c                 C   s   | j t |¡d�S r[   )r]   r   ÚZoneInfor   r   r   r   r   †   r   c                 C   s   | j S r   rV   rX   r   r   r   Úin_timezone‚   s    r`   )zbig_bad_calendar.icszissue_104_broken_calendar.icszsmall_bad_calendar.icsz multiple_calendar_components.icszpr_480_summary_with_colon.icszparsing_error_in_UTC_offset.icszparsing_error.icsc                 C   s   g | ]}|j tkr|j ‘qS r   )ÚnameÚICS_FILES_EXCLUDE)r#   Úfiler   r   r   r%   —   s   
ür%   c                 C   sH   |j }td|ƒ |||fD ]}||kr||   S qtd|› d�ƒ‚dS )zAn example ICS file.zexample file:zCould not find file Ú.N)rW   ÚprintÚ
ValueError)r   rH   rI   rJ   rY   Úics_filerA   r   r   r   rg       s    
rg   c                 C   s   g | ]}d t |ƒkr|‘qS )zfuzz-testcase)Ústr)r#   r;   r   r   r   r%   «   s      c                 C   s   | j S )zClusterfuzz calendars.rV   ©rY   r   r   r   Úfuzz_v1_calendar®   s    rj   c                   c   s(   dt jjjd< dV  t jjj d¡ dS )zMap x_sometime to timeÚtimez
X-SOMETIMEN)rO   ÚcalZtypes_factoryÚ	types_mapÚpopr   r   r   r   Ú
x_sometime´   s    ro   c                   C   s   t  ¡ S )zReturn a new component factory.)rO   ZComponentFactoryr   r   r   r   Úfactory¼   s    rp   c                   c   s   dt j_d V  dt j_d S )NTF)rO   Z
vUTCOffsetZignore_exceptionsr   r   r   r   ÚvUTCOffset_ignore_exceptionsÂ   s    rq   c                 C   s   t ƒ }d|_|S )zReturn an event component.ÚVEVENT©r	   ra   ©r   Úcr   r   r   Úevent_componentÉ   s    rv   c                 C   s
   t ƒ }|S )úReturn an empty component.)r	   rt   r   r   r   ru   Ñ   s    ru   c                 C   s   t ƒ }d|_|S )rw   Z	VCALENDARrs   rt   r   r   r   Úcalendar_componentÛ   s    rx   c                 C   s>   t dd�}d|_|jdddd� |jdd	dd� | |¡ |S )
zBReturn an event with some values and add it to calendar_component.zA brief history of time)Úsummaryrr   ZdtendZ20000102T000000r   )ÚencodeZdtstartZ20000101T000000)r	   ra   ÚaddZadd_component)ru   rx   Úer   r   r   Úfilled_event_componentã   s    

r}   c                 C   s   t ƒ }d|d< |S )NzChair, Table, "Room: 42"Ú	resources)r   rt   r   r   r   Úcalendar_with_resourcesî   s    r   )Úreturnc                 c   s   t  | ¡ t V  t  ¡  dS )zThe timezone provider.N)Ú_tzpZuseZuse_default©Útzp_namer   r   r   r   õ   s    
r   r   c                 C   s
   t | jƒS )zžThis is annother timezone provider.

    The purpose here is to cross test: pytz <-> zoneinfo.
    tzp as parameter makes sure we test the cross product.
    )r
   rW   rX   r   r   r   Ú	other_tzpý   s    r„   c                 C   s   |   ¡ st‚|S )z+Skip tests that are not running under pytz.)Z	uses_pytzÚAssertionError)r   rƒ   r   r   r   Ú	pytz_only  s    r†   c                 C   s   |   ¡ st‚|S )z/Skip tests that are not running under zoneinfo.)Zuses_zoneinfor…   )r   rY   rƒ   r   r   r   Úzoneinfo_only  s    r‡   c                 C   s   | dkst ‚| S )zDo not run tests with pytz.r   ©r…   r‚   r   r   r   Úno_pytz  s    r‰   c                 C   s   | dkst ‚| S )zDo not run tests with zoneinfo.r   rˆ   r‚   r   r   r   Úno_zoneinfo  s    rŠ   c                 C   sŽ   d| j krŠtdg }d| j kr$dg}d| j kr2t}d| j krNd| j krNtdƒ‚dD ]&}d|› �| j krR||krR| |¡ qR| jd|dd	� d
S )a  Parametrize without skipping:

    tzp_name will be parametrized according to the use of
    - pytz_only
    - zoneinfo_only
    - no_pytz
    - no_zoneinfo

    See https://docs.pytest.org/en/6.2.x/example/parametrize.html#deferring-the-setup-of-parametrized-resources
    rƒ   r   r‡   r†   z,Use pytz_only or zoneinfo_only but not both!)r   r   Zno_rL   rM   N)ZfixturenamesÚPYTZ_TZPr…   ÚremoveZparametrize)ZmetafuncZ	tzp_namesra   r   r   r   Úpytest_generate_tests#  s     



ÿþýr�   c                   @   s   e Zd ZdZdd„ ZdS )ÚDoctestZoneInfoz+Constent ZoneInfo representation for tests.c                 C   s   d| j ›d�S )NzZoneInfo(key=ú))r;   r'   r   r   r   r?   A  s    zDoctestZoneInfo.__repr__N)rC   rD   rE   rF   r?   r   r   r   r   rŽ   >  s   rŽ   c                 C   s8   t | tƒr|  d¡} tt| ƒ ¡  dd¡ dd¡ƒ dS )zdoctest printzUTF-8z
Ú
úN)r0   ÚbytesÚdecodere   rh   Ústripr]   )Úobjr   r   r   Údoctest_printE  s    

r–   c                 O   s   | dkrt S t| f|ž|ŽS )zIReplace the import mechanism to skip the whole doctest if we import pytz.r   )r   Ú
__import__)ra   ÚargsÚkwr   r   r   Údoctest_importL  s    rš   c                    sj   |   tjdt¡ |  tdt¡ ddlm} tj	ddd�‰ |  td‡ fd	d
„¡ |  |dt 
d¡¡ dtiS )z,Modify the environment to make doctests run.r   r_   r   )ÚZONEINFOz$d755cef5-2311-46ed-a0e1-6733c9e15c63é   )ÚversionZuuid4c                      s   ˆ S r   r   r   ©Úuidr   r   r   Z  r   z!env_for_doctest.<locals>.<lambda>rZ   r   re   )ÚsetitemÚsysÚmodulesr   ÚsetattrrŽ   Zicalendar.timezone.zoneinfor›   ÚuuidÚUUIDr_   r–   )Zmonkeypatchr›   r   rž   r   Úenv_for_doctestS  s    r¦   )rY   r€   c                 C   s   | j S )zxReturn a timezone id to be used with pytz or zoneinfo.

    This goes through all the different timezones possible.
    rV   ri   r   r   r   Útzid`  s    r§   )MZ	backportsr   ÚImportErrorÚtypingr   ZpytestrO   Ú r   r   Ú	itertoolsr¡   r¤   Úpathlibr   Zdateutilr   Zicalendar.calr   r	   Zicalendar.timezoner
   r   r�   ZHAS_PYTZrZ   r   ZPYTZ_UTCZPYTZ_IN_TIMEZONEr‹   r   Ú__file__ÚparentZHERErN   rQ   rR   rT   ZfixturerH   rI   rJ   rK   r_   r   r^   r`   rb   Úchainr&   Z	ICS_FILESrg   ZFUZZ_V1rj   ro   rp   rq   rv   ru   Úcomprx   r}   r   r„   rh   r†   r‡   r‰   rŠ   r�   rŽ   r–   rš   r¦   ZTZIDSZFixtureRequestr§   r   r   r   r   Ú<module>   sÐ   
þÿ6








"
þÿÿ
	  ÿþ	















	
