o
    à;ïj6D  ã                   @   s`   d Z ddlmZmZ ddlZddlmZ ddlm	Z	 ddl
ZddlZddlZG dd„ deƒZdS )a/  
Complete implementation of the XDG Desktop Entry Specification
http://standards.freedesktop.org/desktop-entry-spec/

Not supported:
- Encoding: Legacy Mixed
- Does not check exec parameters
- Does not check URL's
- Does not completly validate deprecated/kde items
- Does not completly check categories
é    )ÚIniFileÚis_asciiN)ÚParsingError)Úwhichc                   @   sÎ  e Zd ZdZdZdrd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„ 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+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Z d<d=„ Z!d>d?„ Z"d@dA„ Z#dBdC„ Z$dDdE„ Z%dFdG„ Z&dHdI„ Z'dJdK„ Z(dLdM„ Z)dNdO„ Z*dPdQ„ Z+dRdS„ Z,dTdU„ Z-dVdW„ Z.dXdY„ Z/dZd[„ Z0d\d]„ Z1d^d_„ Z2d`da„ Z3dbdc„ Z4ddde„ Z5dfdg„ Z6dhdi„ Z7djdk„ Z8dldm„ Z9dndo„ Z:dpdq„ Z;dS )sÚDesktopEntryz+Class to parse and validate Desktop EntriesúDesktop EntryNc                 C   s<   t ƒ | _|rtj |¡r|  |¡ dS |r|  |¡ dS dS )z¼Create a new DesktopEntry.
        
        If filename exists, it will be parsed as a desktop entry file. If not,
        or if filename is None, a blank DesktopEntry is created.
        N)ÚdictÚcontentÚosÚpathÚexistsÚparseÚnew)ÚselfÚfilename© r   úY/home/hd29/karthikselvaraju/venvs/deluge/lib/python3.10/site-packages/xdg/DesktopEntry.pyÚ__init__   s   ÿzDesktopEntry.__init__c                 C   s   |   ¡ S ©N)ÚgetName©r   r   r   r   Ú__str__&   s   zDesktopEntry.__str__c                 C   s   t  | |ddg¡ dS )z×Parse a desktop entry file.
        
        This can raise :class:`~xdg.Exceptions.ParsingError`,
        :class:`~xdg.Exceptions.DuplicateGroupError` or
        :class:`~xdg.Exceptions.DuplicateKeyError`.
        r   úKDE Desktop EntryN)r   r   )r   Úfiler   r   r   r   )   s   zDesktopEntry.parsec                 C   s   | j ddd�}t|ƒS )zíLooks in the PATH for the executable given in the TryExec field.
        
        Returns the full path to the executable if it is found, None if not.
        Raises :class:`~xdg.Exceptions.NoKeyError` if TryExec is not present.
        ÚTryExecT)Ústrict)Úgetr   )r   Útryexecr   r   r   ÚfindTryExec2   s   zDesktopEntry.findTryExecc                 C   ó
   |   d¡S )NÚType©r   r   r   r   r   ÚgetType<   ó   
zDesktopEntry.getTypec                 C   ó   | j ddd�S )z)deprecated, use getVersionString instead ÚVersionÚnumeric©Útyper!   r   r   r   r   Ú
getVersion>   s   zDesktopEntry.getVersionc                 C   r   )Nr%   r!   r   r   r   r   ÚgetVersionStringA   r#   zDesktopEntry.getVersionStringc                 C   r$   )NÚNameT©Úlocaler!   r   r   r   r   r   C   ó   zDesktopEntry.getNamec                 C   r$   )NÚGenericNameTr,   r!   r   r   r   r   ÚgetGenericNameE   r.   zDesktopEntry.getGenericNamec                 C   r$   )NÚ	NoDisplayÚbooleanr'   r!   r   r   r   r   ÚgetNoDisplayG   r.   zDesktopEntry.getNoDisplayc                 C   r$   )NÚCommentTr,   r!   r   r   r   r   Ú
getCommentI   r.   zDesktopEntry.getCommentc                 C   r$   )NÚIconTr,   r!   r   r   r   r   ÚgetIconK   r.   zDesktopEntry.getIconc                 C   r$   )NÚHiddenr2   r'   r!   r   r   r   r   Ú	getHiddenM   r.   zDesktopEntry.getHiddenc                 C   r$   )NÚ
OnlyShowInT©Úlistr!   r   r   r   r   ÚgetOnlyShowInO   r.   zDesktopEntry.getOnlyShowInc                 C   r$   )NÚ	NotShowInTr;   r!   r   r   r   r   ÚgetNotShowInQ   r.   zDesktopEntry.getNotShowInc                 C   r   )Nr   r!   r   r   r   r   Ú
getTryExecS   r#   zDesktopEntry.getTryExecc                 C   r   )NÚExecr!   r   r   r   r   ÚgetExecU   r#   zDesktopEntry.getExecc                 C   r   )NÚPathr!   r   r   r   r   ÚgetPathW   r#   zDesktopEntry.getPathc                 C   r$   )NÚTerminalr2   r'   r!   r   r   r   r   ÚgetTerminalY   r.   zDesktopEntry.getTerminalc                 C   s   | j dddd�S )z%deprecated, use getMimeTypes instead ÚMimeTypeTÚregex)r<   r(   r!   r   r   r   r   ÚgetMimeType[   s   zDesktopEntry.getMimeTypec                 C   r$   )NrG   Tr;   r!   r   r   r   r   ÚgetMimeTypes^   r.   zDesktopEntry.getMimeTypesc                 C   r$   )NÚ
CategoriesTr;   r!   r   r   r   r   ÚgetCategories`   r.   zDesktopEntry.getCategoriesc                 C   r$   )NÚStartupNotifyr2   r'   r!   r   r   r   r   ÚgetStartupNotifyb   r.   zDesktopEntry.getStartupNotifyc                 C   r   )NÚStartupWMClassr!   r   r   r   r   ÚgetStartupWMClassd   r#   zDesktopEntry.getStartupWMClassc                 C   r   )NÚURLr!   r   r   r   r   ÚgetURLf   r#   zDesktopEntry.getURLc                 C   r$   )NÚServiceTypesTr;   r!   r   r   r   r   ÚgetServiceTypesk   r.   zDesktopEntry.getServiceTypesc                 C   r   )NÚDocPathr!   r   r   r   r   Ú
getDocPathm   r#   zDesktopEntry.getDocPathc                 C   s   | j dddd�S )NÚKeywordsT)r<   r-   r!   r   r   r   r   ÚgetKeywordso   s   zDesktopEntry.getKeywordsc                 C   r   )NÚInitialPreferencer!   r   r   r   r   ÚgetInitialPreferenceq   r#   z!DesktopEntry.getInitialPreferencec                 C   r   )NÚDevr!   r   r   r   r   ÚgetDevs   r#   zDesktopEntry.getDevc                 C   r   )NÚFSTyper!   r   r   r   r   Ú	getFSTypeu   r#   zDesktopEntry.getFSTypec                 C   r   )NÚ
MountPointr!   r   r   r   r   ÚgetMountPointw   r#   zDesktopEntry.getMountPointc                 C   r$   )NÚReadOnlyr2   r'   r!   r   r   r   r   ÚgetReadonlyy   r.   zDesktopEntry.getReadonlyc                 C   r$   )NÚUnmountIconTr,   r!   r   r   r   r   ÚgetUnmountIcon{   r.   zDesktopEntry.getUnmountIconc                 C   r$   )NÚMiniIconTr,   r!   r   r   r   r   ÚgetMiniIcon€   r.   zDesktopEntry.getMiniIconc                 C   r   )NÚTerminalOptionsr!   r   r   r   r   ÚgetTerminalOptions‚   r#   zDesktopEntry.getTerminalOptionsc                 C   r   )NÚ
DefaultAppr!   r   r   r   r   ÚgetDefaultApp„   r#   zDesktopEntry.getDefaultAppc                 C   r$   )NÚ	ProtocolsTr;   r!   r   r   r   r   ÚgetProtocols†   r.   zDesktopEntry.getProtocolsc                 C   r$   )NÚ
ExtensionsTr;   r!   r   r   r   r   ÚgetExtensionsˆ   r.   zDesktopEntry.getExtensionsc                 C   r   )NÚBinaryPatternr!   r   r   r   r   ÚgetBinaryPatternŠ   r#   zDesktopEntry.getBinaryPatternc                 C   r   )NÚ	MapNotifyr!   r   r   r   r   ÚgetMapNotifyŒ   r#   zDesktopEntry.getMapNotifyc                 C   r   )NÚEncodingr!   r   r   r   r   ÚgetEncodingŽ   r#   zDesktopEntry.getEncodingc                 C   r$   )NÚSwallowTitleTr,   r!   r   r   r   r   ÚgetSwallowTitle�   r.   zDesktopEntry.getSwallowTitlec                 C   r   )NÚSwallowExecr!   r   r   r   r   ÚgetSwallowExec’   r#   zDesktopEntry.getSwallowExecc                 C   r$   )NÚ	SortOrderTr;   r!   r   r   r   r   ÚgetSortOrder”   r.   zDesktopEntry.getSortOrderc                 C   r$   )NÚFilePatternrH   r'   r!   r   r   r   r   ÚgetFilePattern–   r.   zDesktopEntry.getFilePatternc                 C   r$   )NÚActionsTr;   r!   r   r   r   r   Ú
getActions˜   r.   zDesktopEntry.getActionsc                 C   sh   t j |¡d dkrd}nt j |¡d dkrd}ntd|ƒ‚tƒ | _|  | j¡ |  d|¡ || _	dS )	a$  Make this instance into a new, blank desktop entry.
        
        If filename has a .desktop extension, Type is set to Application. If it
        has a .directory extension, Type is Directory. Other extensions will
        cause :class:`~xdg.Exceptions.ParsingError` to be raised.
        é   ú.desktopÚApplicationú
.directoryÚ	DirectoryzUnknown extensionr    N)
r
   r   Úsplitextr   r   r	   ÚaddGroupÚdefaultGroupÚsetr   )r   r   r(   r   r   r   r   �   s   

zDesktopEntry.newc                 C   s¾   | j dkr| j d¡ | jdkr| j d¡ n| jdkr'| jdkr'| j d¡ z| j| j  d | _W n tyA   | j d	¡ Y nw z| j| j  d
 | _W d S  ty^   | j d¡ Y d S w )Nr   z([KDE Desktop Entry]-Header is deprecatedz.kdelnkz$File extension .kdelnk is deprecatedr€   r‚   zUnknown File extensionr    zKey 'Type' is missingr+   zKey 'Name' is missing)	r†   ÚwarningsÚappendÚfileExtensionr	   r(   ÚKeyErrorÚerrorsÚnamer   r   r   r   ÚcheckExtras²   s    

ÿÿzDesktopEntry.checkExtrasc                 C   sr   || j kst d|¡st d|¡rt|ƒs| j d| ¡ d S d| j| v r5d| j| v r7| j d¡ d S d S d S )Nz^Desktop Action [a-zA-Z0-9-]+$z^X-zInvalid Group name: %sr:   r>   z;Group may either have OnlyShowIn or NotShowIn, but not both)r†   ÚreÚmatchr   rŒ   r‰   r	   )r   Úgroupr   r   r   Ú
checkGroupÉ   s   

ÿ
þþÿzDesktopEntry.checkGroupc                 C   sÎ  |dkr�|dks|dks|dkr| j  d| ¡ n|dkr$| j  d¡ n|dks8|d	ks8|d
ks8| j d| ¡ | jdkrJ|d
ksJ| j  d| ¡ n| jdkrY|d
krY| j  d¡ |dkrjd| j| vrj| j  d¡ |d	kr}d| j| vr| j  d¡ d S d S d S |dkr�|  ||¡ d S t dtj	j
 d |¡r›d S t dtj	j
 d |¡r©d S |dkr·| j||dd� d S t dtj	j
 d |¡rÅd S t dtj	j
 d |¡rÙ|  ||¡ d S |dkrç| j||dd� d S |dkrú| j||dd � |  |¡ d S |d!k�r| j||dd � |  |¡ d S |d"k�r!|  ||¡ |  |d¡ d S |dk�r4|  ||¡ |  |d¡ d S |d#k�rG|  ||¡ |  |d¡ d S |d$k�r\| j||dd� |  |d¡ d S |d%k�rq| j||dd � |  |d¡ d S |dk�r†| j||dd � |  |d¡ d S |d&k�rž|  ||¡ |  |d¡ |  |¡ d S t d'tj	j
 d |¡�r¼| j||d(dd)� |  |d¡ d S |d*k�rÑ| j||dd� |  |d¡ d S |d+k�rÞ|  |d¡ d S |dk�rñ|  ||¡ |  |d¡ d S |d,k�r| j||dd � | j  d-| ¡ d S |d.k�r|  ||¡ | j  d-| ¡ d S |d/k�r4| j||d0d� | j  d-| ¡ d S |d1k�rO|  ||¡ |  |d¡ | j  d-| ¡ d S |d2k�rj|  ||¡ |  |d¡ | j  d-| ¡ d S |d3k�r…|  ||¡ |  |d¡ | j  d-| ¡ d S |d4k�r¢| j||dd� |  |d¡ | j  d-| ¡ d S t d5tj	j
 d |¡�rÅ|  ||¡ |  |d¡ | j  d-| ¡ d S |d6k�rÚ|  ||¡ | j  d7| ¡ d S t d8tj	j
 d |¡�r÷|  ||¡ | j  d7| ¡ d S |d9k�r|  ||¡ | j  d7| ¡ d S |d:k�r!|  ||¡ | j  d7| ¡ d S |d;k�r8| j||dd � | j  d7| ¡ d S |d<k�rO| j||dd � | j  d7| ¡ d S |d=k�rd|  ||¡ | j  d7| ¡ d S |d>k�ry|  ||¡ | j  d7| ¡ d S t d?tj	j
 d |¡�r�| j  d7| ¡ d S |d@k�r¥|  ||¡ | j  d7| ¡ d S |dAk�r½| j||dBdd)� | j  d7| ¡ d S |dCk�rÔ| j||dd � | j  d7| ¡ d S t dD|¡�rÝd S | j dE| ¡ d S )FNr    ÚServiceTypeÚServiceÚFSDevicezType=%s is a KDE extensionrG   zType=MimeType is deprecatedr�   ÚLinkrƒ   zGValue of key 'Type' must be Application, Link or Directory, but is '%s'r‚   z.File extension is .directory, but Type is '%s'r€   z>Files with Type=Directory should have the extension .directoryrA   z!Type=Application needs 'Exec' keyrQ   zType=Link needs 'URL' keyr%   z^Nameú$z^GenericNamer1   r2   r'   z^Commentz^Iconr8   r:   Tr;   r>   r   rC   rE   r}   rK   z	^KeywordsÚlocalestring)r(   r<   rM   rO   rS   zKey '%s' is a KDE extensionrU   rY   r&   r[   r]   r_   ra   z^UnmountIconrs   zKey '%s' is deprecatedz	^MiniIconrg   ri   rk   rm   ro   rq   z^SwallowTitlerw   r{   rH   ry   z^X-[a-zA-Z0-9-]+zInvalid key: %s)rˆ   r‰   rŒ   rŠ   r	   Ú
checkValuer�   r�   ÚxdgÚLocalerH   ÚcheckOnlyShowInÚ	checkTypeÚcheckCategories)r   ÚkeyÚvaluer‘   r   r   r   ÚcheckKeyÔ   s  þ



























zDesktopEntry.checkKeyc                 C   s(   |   ¡ |ks| j d||f ¡ d S d S )Nz Key '%s' only allowed in Type=%s)r"   rŒ   r‰   )r   rŸ   r(   r   r   r   r�   �  s   ÿzDesktopEntry.checkTypec                 C   sH   |   |¡}g d¢}|D ]}||vr!|dd… dkr!| j d| ¡ qd S )N)
ÚGNOMEÚKDEÚLXDEÚMATEÚRazorÚROXÚTDEÚUnityÚXFCEÚOldr   é   úX-z)'%s' is not a registered OnlyShowIn value)ÚgetListrŒ   r‰   )r   r    ÚvaluesÚvalidÚitemr   r   r   rœ   ”  s   
€þzDesktopEntry.checkOnlyShowInc                    st   |   |¡}g d¢‰ t‡ fdd„|D ƒƒs| j d¡ g d¢}|ˆ  }|D ]}||vr7| d¡s7| j d| ¡ q$d S )N)Ú
AudioVideoÚAudioÚVideoÚDevelopmentÚ	EducationÚGameÚGraphicsÚNetworkÚOfficeÚScienceÚSettingsÚSystemÚUtilityc                 3   s   � | ]}|ˆ v V  qd S r   r   )Ú.0r±   ©Úmainr   r   Ú	<genexpr>   s   € z/DesktopEntry.checkCategories.<locals>.<genexpr>zMissing main category)~ÚBuildingÚDebuggerÚIDEÚGUIDesignerÚ	ProfilingÚRevisionControlÚTranslationÚCalendarÚContactManagementÚDatabaseÚ
DictionaryÚChartÚEmailÚFinanceÚ	FlowChartÚPDAÚProjectManagementÚPresentationÚSpreadsheetÚWordProcessorÚ
2DGraphicsÚVectorGraphicsÚRasterGraphicsÚ
3DGraphicsÚScanningÚOCRÚPhotographyÚ
PublishingÚViewerÚ	TextToolsÚDesktopSettingsÚHardwareSettingsÚPrintingÚPackageManagerÚDialupÚInstantMessagingÚChatÚ	IRCClientÚFeedÚFileTransferÚHamRadioÚNewsÚP2PÚRemoteAccessÚ	TelephonyÚTelephonyToolsÚVideoConferenceÚ
WebBrowserÚWebDevelopmentÚMidiÚMixerÚ	SequencerÚTunerÚTVÚAudioVideoEditingÚPlayerÚRecorderÚDiscBurningÚ
ActionGameÚAdventureGameÚ
ArcadeGameÚ	BoardGameÚ
BlocksGameÚCardGameÚKidsGameÚ	LogicGameÚRolePlayingÚShooterÚ
SimulationÚ
SportsGameÚStrategyGameÚArtÚConstructionÚMusicÚ	LanguagesÚArtificialIntelligenceÚ	AstronomyÚBiologyÚ	ChemistryÚComputerScienceÚDataVisualizationÚEconomyÚElectricityÚ	GeographyÚGeologyÚ
GeoscienceÚHistoryÚ
HumanitiesÚImageProcessingÚ
LiteratureÚMapsÚMathÚNumericalAnalysisÚMedicalSoftwareÚPhysicsÚRoboticsÚSpiritualityÚSportsÚParallelComputingÚ	AmusementÚ	ArchivingÚCompressionÚElectronicsÚEmulatorÚEngineeringÚ	FileToolsÚFileManagerÚTerminalEmulatorÚ
FilesystemÚMonitorÚSecurityÚAccessibilityÚ
CalculatorÚClockÚ
TextEditorÚDocumentationÚAdultÚCorer£   r¢   rª   ÚGTKÚQtÚMotifÚJavaÚConsoleOnlyr­   z!'%s' is not a registered Category)r®   ÚanyrŒ   r‰   Ú
startswith)r   r    r¯   Ú
additionalÚallcategoriesr±   r   rÀ   r   rž   œ  s   
€þzDesktopEntry.checkCategoriesc                 C   s   t  dt¡ |  |¡S )z`Deprecated alias for checkCategories - only exists for backwards
        compatibility.
        z1checkCategorie is deprecated, use checkCategories)rˆ   ÚwarnÚDeprecationWarningrž   )r   r    r   r   r   ÚcheckCategorieª  s   ÿ
zDesktopEntry.checkCategorier   )<Ú__name__Ú
__module__Ú__qualname__Ú__doc__r†   r   r   r   r   r"   r)   r*   r   r0   r3   r5   r7   r9   r=   r?   r@   rB   rD   rF   rI   rJ   rL   rN   rP   rR   rT   rV   rX   rZ   r\   r^   r`   rb   rd   rf   rh   rj   rl   rn   rp   rr   rt   rv   rx   rz   r|   r~   r   rŽ   r’   r¡   r�   rœ   rž   rD  r   r   r   r   r      sv    
	
 =r   )rH  Úxdg.IniFiler   r   Ú
xdg.Localerš   Úxdg.Exceptionsr   Úxdg.utilr   Úos.pathr
   r�   rˆ   r   r   r   r   r   Ú<module>   s    