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   Nz 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 )zThis 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rt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








"

	  	


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

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