Ë
    {Ôh0  ã                   ó\  — d Z 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mZmZmZmZ ddlmZmZmZmZ 	 ddlZd	„ Zd
„ Zd„ Zed„ «       Zedefd„«       Zed„ «       Zedd„«       Zed„ «       Z ed„ «       Z!ed„ «       Z"ed„ «       Z#ed„ «       Z$y# e$ rZ ed«      e‚dZ[ww xY w)z5
This module contains all the functions for konsave.
é    N)Údatetime)Úshuffle)Ú
is_zipfileÚZipFile)ÚHOMEÚCONFIG_FILEÚPROFILES_DIRÚEXPORT_EXTENSIONÚKONSAVE_DIR)ÚTOKEN_SYMBOLÚtokensÚparse_functionsÚparse_keywordsz?Please install the module PyYAML using pip: 
pip install PyYAMLc                 ó   ‡ — ˆ fd„}|S )zsHandles errors and prints nicely.

    Args:
        func: any function

    Returns:
        Returns function
    c                  ó²  •— 	  ‰| i |¤Ž}|S # t         $ rÀ}t        j                  «       j                  d«      }t        j
                  j                  t        d«      }t        |d«      5 }|j                  |dz   «       t        j                  |¬«       |j                  d«       d d d «       n# 1 sw Y   nxY wt        d|› d|› d�«       Y d }~y d }~ww xY w)	Nz[%d/%m/%Y %H:%M:%S]z.cache/konsave_log.txtÚaú
)Úfileú	Konsave: z
Please check the log at z for more details.)Ú	Exceptionr   ÚnowÚstrftimeÚosÚpathÚjoinr   ÚopenÚwriteÚ	tracebackÚ	print_excÚprint)ÚargsÚkwargsÚfunctionÚerrÚdateandtimeÚlog_filer   Úfuncs          €ú?/home/josh/.local/lib/python3.12/site-packages/konsave/funcs.pyÚ
inner_funcz%exception_handler.<locals>.inner_func&   sË   ø€ ð	Ù˜TÐ, VÑ,ˆHð ˆOøô ò 	Ü"Ÿ,™,›.×1Ñ1Ð2GÓHˆKÜ—w‘w—|‘|¤DÐ*BÓCˆHä�h Ó$ð !¨Ø—
‘
˜;¨Ñ-Ô.Ü×#Ñ#¨Õ.Ø—
‘
˜4Ô ÷!÷ !ñ !úô
 Ø˜C˜5Ð :¸8¸*ÐDVÐWôô ûð	ús-   ƒ �	C–ACÁ)<B.Â%	CÂ.B7	Â3CÃC© )r'   r)   s   ` r(   Úexception_handlerr+      s   ø€ ôð& Ðó    c                 ón   — t         j                  j                  | «      st        j                  | «       | S )zˆCreates directory if it doesn't exist.

    Args:
        path: path to the new directory

    Returns:
        path: the same path
    )r   r   ÚexistsÚmakedirs)r   s    r(   Úmkdirr0   <   s%   € ô �7‰7�>‰>˜$ÔÜ
�‰�DÔØ€Kr,   c                 ó(   — t        d| › �g|¢­i |¤Ž y)zµLogs text.

    Args:
        msg: the text to be printed
        *args: any arguments for the function print()
        **kwargs: any keyword arguments for the function print()
    r   N)r    )Úmsgr!   r"   s      r(   Úlogr3   J   s   € ô 
ˆI�c�UÐ
Ð-˜dÒ- fÓ-r,   c                 óþ  — t        | t        «      rt        |t        «      sJ d«       ‚| |k7  sJ d«       ‚t        j                  j	                  | «      sJ d«       ‚t        j                  j	                  |«      st        j
                  |«       t        j                  | «      D ]Ø  }t        j                  j                  | |«      }t        j                  j                  ||«      }t        j                  j                  |«      rt        ||«       Œot        j                  j	                  |«      rt        j                  |«       t        j                  j	                  |«      sŒÃt        j                  ||«       ŒÚ y)a  
    This function was created because shutil.copytree gives error if the destination folder
    exists and the argument "dirs_exist_ok" was introduced only after python 3.8.
    This restricts people with python 3.7 or less from using Konsave.
    This function will let people with python 3.7 or less use Konsave without any issues.
    It uses recursion to copy files and folders from "source" to "dest"

    Args:
        source: the source destination
        dest: the destination to copy the file/folder to
    zInvalid pathz$Source and destination can't be samezSource path doesn't existN)Ú
isinstanceÚstrr   r   r.   r0   Úlistdirr   ÚisdirÚcopyÚremoveÚshutil)ÚsourceÚdestÚitemÚsource_pathÚ	dest_paths        r(   r9   r9   U   sÿ   € ô �fœcÔ"¤z°$¼Ô'<ÐL¸nÓLÐ<Ø�TŠ>ÐAÐAÓAˆ>Ü�7‰7�>‰>˜&Ô!Ð>Ð#>Ó>Ð!ä�7‰7�>‰>˜$ÔÜ
�‰�Œä—
‘
˜6Ó"ò 
/ˆÜ—g‘g—l‘l 6¨4Ó0ˆÜ—G‘G—L‘L  tÓ,ˆ	ä�7‰7�=‰=˜Ô%Ü�˜iÕ(ä�w‰w�~‰~˜iÔ(Ü—	‘	˜)Ô$Ü�w‰w�~‰~˜kÕ*Ü—‘˜K¨Õ.ñ
/r,   Úreturnc                 ó  ‡— t        | d«      5 }t        j                  |j                  «       t        j                  ¬«      }ddd«       t        t        t        «       t        t        t        |«       ˆfd„Š ‰|«      S # 1 sw Y   Œ@xY w)z]Reads "conf.yaml" and parses it.

    Args:
        config_file: path to the config file
    Úr)ÚLoaderNc                 óÔ   •— t        | t        «      r| D �cg c]
  } ‰|«      ‘Œ c}| d d  n3t        | t        «      r#| j                  «       D ]  \  }} ‰|«      | |<   Œ | €g S | S c c}w )N)r5   ÚlistÚdictÚitems)ÚdataÚiÚkÚvÚconvert_none_to_empty_lists       €r(   rM   z7read_konsave_config.<locals>.convert_none_to_empty_list…   sm   ø€ Ü�dœDÔ!Ø>BÖC¸Ñ1°!Õ4ÒCˆD‘‰GÜ˜œdÔ#ØŸ
™
›ò 8‘��1Ù4°QÓ7��Q’ð8à�\ˆrÐ+ tÐ+ùò	 Ds   –A%)	r   ÚyamlÚloadÚreadÚ
SafeLoaderr   r   r   r   )Úconfig_fileÚtextÚkonsave_configrM   s      @r(   Úread_konsave_configrU   v   sn   ø€ ô 
ˆk˜3Ó	ð H 4ÜŸ™ 4§9¡9£;´t·±ÔGˆ÷Hä”6œ<¨Ô8Ü”FœL¨.Ô9ô
,ñ & nÓ5Ð5÷!Hð Hús   Ž4BÂB
c                 óú   — t         j                  j                  t        «      r|dk7  sJ d«       ‚| j	                  «        t        d«       t        d«       t        | «      D ]  \  }}t        |dz   › d|› �«       Œ y)z˜Lists all the created profiles.

    Args:
        profile_list: the list of all created profiles
        profile_count: number of profiles created
    r   zNo profile found.zKonsave profiles:zID	NAMEé   ú	N)r   r   r.   r	   Úsortr    Ú	enumerate)Úprofile_listÚprofile_countrJ   r>   s       r(   Úlist_profilesr]   �   sw   € ô �7‰7�>‰>œ,Ô'¨M¸QÒ,>ÐSÐ@SÓSÐ>ð ×ÑÔô 
Ð
ÔÜ	ˆ*ÔÜ˜\Ó*ò "‰ˆˆ4Ü��Q‘��r˜$˜Ð Õ!ñ"r,   c                 óÒ  — | |vs	|sJ d«       ‚t        d«       t        j                  j                  t        | «      }t        |«       t        t        «      d   }|D ]ä  }||   d   }t        j                  j                  ||«      }t        |«       ||   d   D ]¤  }t        j                  j                  ||«      }	t        j                  j                  ||«      }
t        j                  j                  |	«      sŒct        j                  j                  |	«      rt        |	|
«       Œ�t        j                  |	|
«       Œ¦ Œæ t        j                  t        |«       t        d«       y)zìSaves necessary config files in ~/.config/konsave/profiles/<name>.

    Args:
        name: name of the profile
        profile_list: the list of all created profiles
        force: force overwrite already created profile, optional
    z%Profile with this name already existszsaving profile...ÚsaveÚlocationÚentrieszProfile saved successfully!N)r3   r   r   r   r	   r0   rU   r   r.   r8   r9   r;   )Únamer[   ÚforceÚprofile_dirrT   Úsectionr`   ÚfolderÚentryr<   r=   s              r(   Úsave_profilerh   ¦   s  € ð �|Ñ#¡uÐUÐ.UÓUÐ,ô ÐÔÜ—'‘'—,‘,œ|¨TÓ2€KÜ	ˆ+Ôä(¬Ó5°fÑ=€Nà!ò .ˆØ! 'Ñ*¨:Ñ6ˆÜ—‘—‘˜k¨7Ó3ˆÜˆfŒØ# GÑ,¨YÑ7ò 	.ˆEÜ—W‘W—\‘\ (¨EÓ2ˆFÜ—7‘7—<‘< ¨Ó.ˆDÜ�w‰w�~‰~˜fÕ%Ü—7‘7—=‘= Ô(Ü˜ Õ&ä—K‘K ¨Õ-ñ	.ð	.ô ‡K�K”˜[Ô)äÐ%Õ&r,   c                 ót  — |dk7  sJ d«       ‚| |v sJ d«       ‚t         j                  j                  t        | «      }t	        d«       t         j                  j                  |d«      }t        |«      d   }|D ]4  }t         j                  j                  ||«      }t        |||   d   «       Œ6 t	        d«       y	)
zÑApplies profile of the given id.

    Args:
        profile_name: name of the profile to be applied
        profile_list: the list of all created profiles
        profile_count: number of profiles created
    r   úNo profile saved yet.zProfile not found :(zcopying files...ú	conf.yamlr_   r`   zVProfile applied successfully! Please log-out and log-in to see the changes completely!N)r   r   r   r	   r3   rU   r9   )Úprofile_namer[   r\   rd   Úconfig_locationÚprofile_configrb   r`   s           r(   Úapply_profilero   Ì   s²   € ð ˜AÒÐ6Ð6Ó6ÐØ˜<Ñ'Ð?Ð)?Ó?Ð'ô —'‘'—,‘,œ|¨\Ó:€KäÐÔä—g‘g—l‘l ;°Ó<€OÜ(¨Ó9¸&ÑA€NØò 9ˆÜ—7‘7—<‘< ¨TÓ2ˆÜˆX�~ dÑ+¨JÑ7Õ8ð9ô Ø`õr,   c                 óÌ   — |dk7  sJ d«       ‚| |v sJ d«       ‚t        d«       t        j                  t        j                  j                  t        | «      «       t        d«       y)zÏRemoves the specified profile.

    Args:
        profile_name: name of the profile to be removed
        profile_list: the list of all created profiles
        profile_count: number of profiles created
    r   rj   úProfile not found.zremoving profile...zremoved profile successfullyN)r3   r;   Úrmtreer   r   r   r	   )rl   r[   r\   s      r(   Úremove_profilers   ê   s[   € ð ˜AÒÐ6Ð6Ó6ÐØ˜<Ñ'Ð=Ð)=Ó=Ð'ô ÐÔÜ
‡M�M”"—'‘'—,‘,œ|¨\Ó:Ô;ÜÐ&Õ'r,   c                 óð  — |dk7  sJ d«       ‚| |v sJ d«       ‚t         j                  j                  t        | «      }|r|} |r!t         j                  j                  || «      }n2t         j                  j                  t        j                  «       | «      }|sp	 |› |› d�|› d�g}t        |D �	cg c]!  }	t         j                  j                  |	«      ‘Œ# c}	«      r+dj                  t        j                  «       «      }
|› d|
› �}nnŒot        d«       t         j                  j                  |d	«      }t        |«      }t        t         j                  j                  |d
«      «      }|d
   D ][  }t         j                  j                  ||«      }t        d|› d�«       t        |t         j                  j                  ||«      «       Œ] |d   }t        t         j                  j                  |d«      «      }|D ]ñ  }||   d   }t        t         j                  j                  ||«      «      }	||   d   D ]³  }t         j                  j                  ||«      }t         j                  j                  |	|«      }t        d|› d�«       t         j                  j                  |«      sŒrt         j                  j                  |«      rt        ||«       Œžt        j                  ||«       Œµ Œó t        j                  t         |«       t        d«       t        j"                  |d|«       t        j$                  |«       t        j&                  |dz   |t(        z   «       t        d|› t(        › �«       yc c}	w )aÿ  It will export the specified profile as a ".knsv" to the specified directory.
       If there is no specified directory, the directory is set to the current working directory.

    Args:
        profile_name: name of the profile to be exported
        profile_list: the list of all created profiles
        profile_count: number of profiles created
        directory: output directory for the export
        force: force the overwrite of existing export file
        name: the name of the resulting archive
    r   rj   rq   z.knsvz.zipzf{:%d-%m-%Y:%H-%M-%S}Ú_z3Exporting profile. It might take a minute or two...rk   r_   zExporting "ú"...Úexportr`   ra   zCreating archiveÚzipzSuccessfully exported to N)r   r   r   r	   ÚgetcwdÚanyr.   Úformatr   r   r3   rU   r0   r9   r8   r;   r   Úmake_archiverr   Úmover
   )rl   r[   r\   Úarchive_dirÚarchive_namerc   rd   Úexport_pathÚpathsr   ÚtimeÚprofile_config_filerT   Úexport_path_saverb   r`   Úkonsave_config_exportÚexport_path_exportrg   r<   r=   s                        r(   rw   rw   þ   sÚ  € ð ˜AÒÐ6Ð6Ó6ÐØ˜<Ñ'Ð=Ð)=Ó=Ð'ô —'‘'—,‘,œ|¨\Ó:€KáØ#ˆáÜ—g‘g—l‘l ;°Ó=‰ä—g‘g—l‘l¤2§9¡9£;°Ó=ˆñ ØØ#�}¨+¨°eÐ'<ÀÀÈTÐ>RÐSˆEÜ°UÖ;¨T”B—G‘G—N‘N 4Õ(Ò;Ô<Ø.×5Ñ5´h·l±l³nÓE�Ø!, ¨Q¨t¨fÐ5‘àð ô Ð=Ô>äŸ'™'Ÿ,™, {°KÓ@ÐÜ(Ð)<Ó=€NäœRŸW™WŸ\™\¨+°vÓ>Ó?ÐØ˜vÑ&ò =ˆÜ—7‘7—<‘< ¨TÓ2ˆÜˆk˜$˜˜tÐ$Ô%ÜˆX”r—w‘w—|‘|Ð$4°dÓ;Õ<ð=ð
 +¨8Ñ4ÐÜœrŸw™wŸ|™|¨K¸ÓBÓCÐØ%ò .ˆØ(¨Ñ.¨zÑ:ˆÜ”R—W‘W—\‘\Ð"4°dÓ;Ó<ˆØ*¨4Ñ0°Ñ;ò 	.ˆEÜ—W‘W—\‘\ (¨EÓ2ˆFÜ—7‘7—<‘<  eÓ,ˆDÜ�+˜e˜W DÐ)Ô*Ü�w‰w�~‰~˜fÕ%Ü—7‘7—=‘= Ô(Ü˜ Õ&ä—K‘K ¨Õ-ñ	.ð.ô ‡K�K”˜[Ô)äÐÔÜ
×Ñ˜ U¨KÔ8ä
‡M�M�+ÔÜ
‡K�K�˜fÑ$ kÔ4DÑ&DÔEäÐ
# K =Ô1AÐ0BÐCÕDùòS <s   Â-&M3c                 óÞ  — t        | «      r| dd t        k(  sJ d«       ‚t        j                  j	                  | «      dd }t        j                  j                  t        j                  j                  t        |«      «      rJ d«       ‚t        d«       t        j                  j	                  | «      j                  t        d«      }t        j                  j                  t        d|«      }t        | d«      5 }|j                  |«       ddd«       t        j                  j                  |d	«      }t        |«      }t        j                  j                  t        |«      }t        t        j                  j                  |d
«      |«       t        j                  t        j                  j                  |d	«      |«       |d   D ]ú  }|d   |   d   }t        j                  j                  |d|«      } t!        | «       |d   |   d   D ]³  }	t        j                  j                  | |	«      }
t        j                  j                  ||	«      }t        d|	› d�«       t        j                  j                  |
«      sŒrt        j                  j#                  |
«      rt        |
|«       Œžt        j                  |
|«       Œµ Œü t        j$                  |«       t        d«       y# 1 sw Y   �ŒÚxY w)z\This will import an exported profile.

    Args:
        path: path of the `.knsv` file
    éûÿÿÿNzNot a valid konsave filez'A profile with this name already existsz3Importing profile. It might take a minute or two...Ú ÚtemprC   rk   r_   rw   r`   ra   zImporting "rv   zProfile successfully imported!)r   r
   r   r   Úbasenamer.   r   r	   r3   Úreplacer   r   Ú
extractallrU   r9   r;   r0   r8   rr   )r   r>   Ú	temp_pathÚzip_fileÚconfig_file_locationrT   rd   re   r`   rg   r<   r=   s               r(   Úimport_profiler‘   L  sB  € ô 	�4Ô˜T " #˜YÔ*:Ò:ð"à!ó"Ø:ä�7‰7×Ñ˜DÓ! # 2Ð&€DÜ�w‰w�~‰~Ü
�‰�‰”\ 4Ó(ôð 1à0ó1ð ô
 Ð=Ô>ä�7‰7×Ñ˜DÓ!×)Ñ)Ô*:¸BÓ?€Dä—‘—‘œ[¨&°$Ó7€Iä	��sÓ	ð '˜xØ×Ñ˜IÔ&÷'ô Ÿ7™7Ÿ<™<¨	°;Ó?ÐÜ(Ð)=Ó>€Nä—'‘'—,‘,œ|¨TÓ2€KÜŒ�‰�‰�i Ó	(¨+Ô6Ü
‡K�K”—‘—‘˜Y¨Ó4°kÔBà! (Ñ+ò .ˆØ! (Ñ+¨GÑ4°ZÑ@ˆÜ�w‰w�|‰|˜I x°Ó9ˆÜˆdŒØ# HÑ-¨gÑ6°yÑAò 	.ˆEÜ—W‘W—\‘\ $¨Ó.ˆFÜ—7‘7—<‘< ¨%Ó0ˆDÜ�+˜e˜W DÐ)Ô*Ü�w‰w�~‰~˜fÕ%Ü—7‘7—=‘= Ô(Ü˜ Õ&ä—K‘K ¨Õ-ñ	.ð	.ô ‡M�M�)ÔäÐ(Õ)÷5'ñ 'ús   Ã8K"Ë"K,c                  ó„   — t        d«      } | dk(  r%t        j                  t        «       t	        d«       yt	        d«       y)zWipes all profiles.z<This will wipe all your profiles. Enter "WIPE" To continue: ÚWIPEzRemoved all profiles!zAborting...N)Úinputr;   rr   r	   r3   )Úconfirms    r(   Úwiper–   �  s4   € ô ÐRÓS€GØ�&ÒÜ�‰”lÔ#ÜÐ#Õ$äˆMÕr,   )F)%Ú__doc__r   r;   r   r   Úrandomr   Úzipfiler   r   Úkonsave.constsr   r   r	   r
   r   Úkonsave.parser   r   r   r   rN   ÚModuleNotFoundErrorÚerrorr+   r0   r3   r9   rG   rU   r]   rh   ro   rs   rw   r‘   r–   r*   r,   r(   ú<module>rž      s2  ðñó 
Û Û Ý Ý ß '÷õ ÷ PÓ OðÛòò@ò.ð ñ/ó ð/ð@ ð6¨ò 6ó ð6ð2 ñ"ó ð"ð* ò"'ó ð"'ðJ ñó ðð: ñ(ó ð(ð& ñJEó ðJEðZ ñ1*ó ð1*ðh ñó ñøðY ò Ù
ØMóàðûðús   ¾B ÂB+Â	B&Â&B+