o
    í—›dá  ã                   @   sf   d dl m Z  d dlZd dlZd dlZd dlZd dlZd dlZd dlZ	d dl
Z	G dd„ dƒZdd„ ZdS )é    )ÚglobNc                   @   sh   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚQuirkCheckerú/usr/share/jockey/quirksc                 C   s.   || _ || _g | _|  ¡  |  ¡ | _d| _d S )Nz/usr/share/X11/xorg.conf.d)Ú_handlerÚquirks_pathÚ_quirksÚget_quirks_from_pathÚget_system_infoÚ_system_infoÚ_xorg_conf_d_path)ÚselfÚhandlerÚpath© r   ú5/usr/lib/python3/dist-packages/Quirks/quirkapplier.pyÚ__init__!   s   

zQuirkChecker.__init__c                 C   s|   g | _ tj | j¡r3ttj | jd¡ƒD ]}tj |¡r/t 	d| ¡ |  
|¡}|  j |7  _ q| j S t 	d| j ¡ | j S )ú5check all the files in a directory looking for quirksÚ*z
Parsing %sz%s does not exist)r   Úosr   Úisdirr   r   ÚjoinÚisfileÚloggingÚdebugÚget_quirks_from_file)r   ÚfÚquirksr   r   r   r   )   s   
€ÿz!QuirkChecker.get_quirks_from_pathc                 C   s   t j |¡}| ¡ S )r   )ÚQuirksÚquirkreaderÚ	ReadQuirkÚ
get_quirks)r   Ú
quirk_filer   r   r   r   7   s   z!QuirkChecker.get_quirks_from_filec                 C   s   t j ¡ }| ¡ S )zGet system info for the quirk)r   Ú	quirkinfoÚ	QuirkInfoÚget_dmi_info)r   Ú
quirk_infor   r   r   r	   =   s   
zQuirkChecker.get_system_infoc                 C   s‚   d}|j  ¡ D ]2}|j | D ]*}| j |¡r8| j |¡|kr8t|j | ƒdkr8t d| j |¡|f ¡   dS qqt d¡ |S )zSee if tags match system infoTé   zFailure to match %s with %sFÚSuccess)Ú
match_tagsÚkeysr
   ÚgetÚlenr   r   )r   ÚquirkÚresultÚtagÚvalr   r   r   Úmatches_tagsB   s   ÿ€û
zQuirkChecker.matches_tagsTc                 C   sŠ   | j D ]?}| j ¡ dd„ |jD ƒv rBt d|j ¡ |  |¡r=|r/t d|j ¡ |  	|¡ qt d|j ¡ |  
|¡ qt d¡ qdS )z)Process quirks and do something with themc                 S   s   g | ]}|  ¡  ¡ ‘qS r   )ÚlowerÚstrip)Ú.0Úxr   r   r   Ú
<listcomp>R   s    z.QuirkChecker._check_quirks.<locals>.<listcomp>zProcessing quirk %szApplying quirk %szUnapplying quirk %szQuirk doesn't matchN)r   r   r1   r   r   r   Úidr0   ÚinfoÚ_apply_quirkÚ_unapply_quirk)r   Úenabler,   r   r   r   Ú_check_quirksO   s   


€ôzQuirkChecker._check_quirksc                 C   ó   |   d¡ dS )zEnable all quirks for a handlerTN©r;   ©r   r   r   r   Úenable_quirks_   ó   zQuirkChecker.enable_quirksc                 C   r<   )z Disable all quirks for a handlerFNr=   r>   r   r   r   Údisable_quirksc   r@   zQuirkChecker.disable_quirksc                 C   s    d| j | j|j ¡  dd¡f S )z$Return the path to the X config filez%s/10-%s-%s.confú ú-)r   r   r6   r1   Úreplace)r   r,   r   r   r   Ú_get_destination_pathg   s   ÿz"QuirkChecker._get_destination_pathc                 C   s   |   |¡}tjddd}| |j¡ | ¡  tj |j	¡}t
 |j¡ t |j	¡ zt
 d| ¡ | |¡ W dS  tyG   t
 d¡ Y dS w )z!Get the xorg snippet and apply itÚwF)ÚmodeÚdeletezCreating %szError during write()T)rE   ÚtempfileÚNamedTemporaryFileÚwriteÚ	x_snippetÚcloseÚxkitÚ
xorgparserÚParserÚnamer   r   Ú
globaldictr   ÚunlinkÚIOErrorÚ	exception)r   r,   ÚdestinationÚtmp_fileÚtmp_xkitr   r   r   r8   l   s   
ý
þzQuirkChecker._apply_quirkc              	   C   sL   |   |¡}t d| ¡ zt |¡ W dS  ttfy%   t d¡ Y dS w )z%Remove the file with the xorg snippetzRemoving %s ...zCannot unlink destinationFT)rE   r   r   r   rS   ÚOSErrorrT   rU   )r   r,   rV   r   r   r   r9   €   s   
ý
þzQuirkChecker._unapply_quirkN)r   )T)Ú__name__Ú
__module__Ú__qualname__r   r   r   r	   r0   r;   r?   rA   rE   r8   r9   r   r   r   r   r       s    

r   c                  C   s0   t ddd} |  ¡  |  ¡  ttj t¡ƒ dS )NÚnvidiaz/home/alberto/oem/jockey/quirks)r   r   )r   r?   rA   Úprintr   r   ÚabspathÚ__file__)Úar   r   r   ÚmainŽ   s
   rb   )r   r   ÚsysrI   r   Úxkit.xutilsrN   Úxkit.xorgparserÚQuirks.quirkreaderr   ÚQuirks.quirkinfor   rb   r   r   r   r   Ú<module>   s   n