
    x7#h!                     v    d dl Z d dlZd dlmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZ dZdZe dZ G d	 d
      Zy)    N)datedatetime	timedeltatimezone)Union)HolidayBase)__version__K   z
 c            
           e Zd ZddededdfdZdedefdZdedefd	Zdd
e	dede
dee   fdZddedeeef   fdZdeddfdZy)ICalExporterinstanceshow_languagereturnNc                    || _         || _        t        j                  t        j
                        j                  d      | _        t        | _	        t        | j                   dd      xs t        | j                   dd      }t        |t              r*|t        | j                   dg       v r| j                  |      nd| _        | j                  r| j                  t        d      yy)aE  Initialize iCalendar exporter.

        Args:
            show_language:
                Determines whether to include the `;LANGUAGE=` attribute in the
                `SUMMARY` field. Defaults to `False`.

                If the `HolidaysBase` object has a `language` attribute, it will
                be used. Otherwise, `default_language` will be used if available.

                If neither attribute exists and `show_language=True`, an
                exception will be raised.

            instance:
                `HolidaysBase` object containing holiday data.
        z%Y%m%dT%H%M%SZlanguageNdefault_languagesupported_languageszALANGUAGE cannot be included because the language code is missing.)holidaysr   r   nowr   utcstrftimeical_timestampr	   holidays_versiongetattr
isinstancestr_validate_languager   
ValueError)selfr   r   r   s       M/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/holidays/ical.py__init__zICalExporter.__init__   s    " !*&ll8<<8AABRS +4==*d; 
wMM-t@

 (C(GDMM3H"MM ##H- 	 	 $--"7`aa #8    r   c                     |j                         j                  dd      }t        j                  d      }|j	                  |      st        d| d      |S )aa  Validate the language code to ensure it complies with RFC 5646.

        In the current implementation, all languages must comply with
        either ISO 639-1 or ISO 639-2 if specified (part of RFC 5646).

        Args:
            language:
                The language code to validate.

        Returns:
            Validated language code.
        _-z^[a-z]{2,3}(?:-[A-Z]{2})?$zInvalid language tag: 'z'. Expected format follows ISO 639-1 or ISO 639-2, e.g., 'en', 'en-US'. For more details, refer to: https://www.loc.gov/standards/iso639-2/php/code_list.php.)stripreplacerecompile	fullmatchr   )r    r   iso639_patterns      r!   r   zICalExporter._validate_language>   sc     >>#++C5 $AB''1)( 4V V 
 r#   linec           	         j                         rMt              t        kD  r9t        j	                  fdt        dt              t        dz
        D              S S t        j                               t        kD  rg }d}d}t              D ]H  \  }}t        |j                               }||z  }|t        kD  s.|j                  ||        |}|dz   }J |j                  |d        t        j	                  |      S S )aj  Fold long lines according to RFC 5545.

        Content lines SHOULD NOT exceed 75 octets. If a line is too long,
        it must be split into multiple lines, with each continuation line
        starting with a space.

        Args:
            line:
                The content line to be folded.

        Returns:
            The folded content line.
        c              3   <   K   | ]  }||t         z   d z
     yw)   N)CONTENT_LINE_MAX_LENGTH).0ir-   s     r!   	<genexpr>z*ICalExporter._fold_line.<locals>.<genexpr>l   s+      8M Q!881<=Ms   r   r0   N)	isasciilenr1   CONTENT_LINE_DELIMITER_WRAPjoinrangeencode	enumerateappend)r    r-   parts
part_startpart_lenr3   charchar_byte_lens    `      r!   
_fold_linezICalExporter._fold_lineZ   s     <<> 4y22277 8"1c$i1H11LM8  . % "99EJH$T?4 #DKKM 2M)55LLj!34!"J,q0H + LLjk*+.33E:: r#   dtholiday_nameholiday_lengthc           	      T   |j                  dd      j                  dd      j                  dd      }t        j                          d| j                   d}| j                  rd	| j
                   nd
}dd| j                   d| | j                  d| d|       d|dd| ddgS )a6  Generate a single holiday event.

        Args:
            dt:
                Holiday date.

            holiday_name:
                Holiday name.

            holiday_length:
                Holiday length in days, default to 1.

        Returns:
            List of iCalendar format event lines.
        \z\\,z\,:z\:@z.holidays.localz
;LANGUAGE= zBEGIN:VEVENTzDTSTAMP:zUID:SUMMARYzDTSTART;VALUE=DATE:z%Y%m%dz
DURATION:PDz
END:VEVENT)r(   uuiduuid4r   r   r   r   rB   )r    rC   rD   rE   sanitized_holiday_name	event_uidlanguage_tags          r!   _generate_eventzICalExporter._generate_event   s    (   v.66sEBJJ3PUV 	 zz|nAd&;&;%<OL	7;7I7IDMM?3r t**+,9+OOgl^15K4LMN!"V-(*
 	
r#   return_bytesc                 0   dd| j                    dddg}t        | j                  j                               }d}|t	        |      k  r||   }| j                  j                  |      }|D ]  }d}||z   t	        |      k  r|||z      ||   t        |      z   k(  rx|| j                  j                  |||z            v rU|dz  }||z   t	        |      k  r?|||z      ||   t        |      z   k(  r$|| j                  j                  |||z            v rU|j                  | j                  |||              |z  }|t	        |      k  r|j                  d	       |j                  d
       t        j                  |      }	|r|	j                         S |	S )zGenerate iCalendar data.

        Args:
            return_bytes:
                If True, return bytes instead of string.

        Returns:
            The complete iCalendar data
            (string or UTF-8 bytes depending on return_bytes).
        zBEGIN:VCALENDARz2PRODID:-//Vacanza//Open World Holidays Framework vz//ENzVERSION:2.0zCALSCALE:GREGORIANr   r0   )dayszEND:VCALENDARrK   )r   sortedr   keysr6   get_listr   extendrS   r<   CONTENT_LINE_DELIMITERr8   r:   )
r    rT   linessorted_datesr3   rC   namesnamerV   outputs
             r!   generatezICalExporter.generate   s    @AVAV@WW[\ 	
 dmm0023#l##aBMM**2.EHs<00$QX.,q/ISWDX2XX 6 6|AH7M NNAID	 Hs<00$QX.,q/ISWDX2XX 6 6|AH7M NN T11"dDAB  IA #l##" 	_%R',,U3".v}}:F:r#   	file_pathc                     | j                  d      }|st        d      t        |d      5 }|j                  |       ddd       y# 1 sw Y   yxY w)ag  Export the calendar data to a .ics file.

        While RFC 5545 does not specifically forbid filenames for .ics files, but it's advisable
        to follow general filesystem conventions and avoid using problematic characters.

        Args:
            file_path:
                Path to save the .ics file, including the filename (with extension).
        T)rT   z&Generated content is empty or invalid.wbN)ra   r   openwrite)r    rb   contentfiles       r!   save_icszICalExporter.save_ics   sG     --T-2EFF)T"dJJw #""s   AA)F)r0   )__name__
__module____qualname__r   boolr"   r   r   rB   r   intlistrS   r   bytesra   ri    r#   r!   r   r      s     b  bT  bd  bD3 3 8)s )s )V!
$ !
c !
3 !
W[\_W` !
F*;T *;eCJ6G *;X #  $  r#   r   )r)   rN   r   r   r   r   typingr   holidays.holiday_baser   holidays.versionr	   r1   r[   r7   r   rq   r#   r!   <module>ru      sD    
  8 8  - (   !7 8: I  I r#   