o
    •Û_A"  ã                   @   s‚   d dl mZ d dlZ d dlZ d dlmZ d dlZd dlmZ d dlm	Z	 dgZ
e j ¡ ZG dd„ deƒZG dd„ de jjjƒZdS )	é    )ÚexportsN)ÚTunedException)ÚcommandsÚ
Controllerc                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú
TimerStorec                 C   s   t ƒ | _t ¡ | _d S ©N)ÚdictÚ_timersÚ	threadingÚLockÚ_timers_lock©Úself© r   ú9/usr/lib/python3/dist-packages/tuned/daemon/controller.pyÚ__init__   s   zTimerStore.__init__c                 C   s6   | j  || j|< W d   ƒ d S 1 sw   Y  d S r   )r   r	   ©r   ÚtokenÚtimerr   r   r   Ústore_timer   s   "ÿzTimerStore.store_timerc                 C   sd   | j % z| j| }| ¡  | j|= W n   Y W d   ƒ d S W d   ƒ d S 1 s+w   Y  d S r   )r   r	   Úcancelr   r   r   r   Ú
drop_timer   s   
ú"üzTimerStore.drop_timerc                 C   sN   | j  | j ¡ D ]}| ¡  q	| j ¡  W d   ƒ d S 1 s w   Y  d S r   )r   r	   Úvaluesr   Úclear)r   r   r   r   r   Ú
cancel_all   s
   
"ýzTimerStore.cancel_allN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r      s
    	r   c                       s   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Ze d¡d	d
„ ƒZ	dd„ Z
e dd¡dEdd„ƒZe dd¡dEdd„ƒZe dd¡dEdd„ƒZe dd¡dEdd„ƒZe dd¡dEdd„ƒZdd„ Ze dd¡dEdd „ƒZe dd¡dEd!d"„ƒZe dd¡dEd#d$„ƒZe dd%¡dEd&d'„ƒZe dd¡dEd(d)„ƒZe dd¡dEd*d+„ƒZe dd¡dEd,d-„ƒZe dd.¡dEd/d0„ƒZe dd1¡dEd2d3„ƒZe dd4¡dEd5d6„ƒZe dd¡dEd7d8„ƒZe dd¡dEd9d:„ƒZe dd¡dEd;d<„ƒZe dd=¡dEd>d?„ƒZe dd¡dEd@dA„ƒZ e ddB¡dEdCdD„ƒZ!‡  Z"S )Fr   z‘
	Controller's purpose is to keep the program running, start/stop the tuning,
	and export the controller interface (currently only over D-Bus).
	c                    s8   t t| ƒ ¡  || _|| _t ¡ | _tƒ | _	t
ƒ | _d S r   )Úsuperr   r   Ú_daemonÚ_global_configr
   ÚEventÚ
_terminater   Ú_cmdr   Ú_timer_store)r   ÚdaemonÚglobal_config©Ú	__class__r   r   r   +   s   
zController.__init__c                 C   sz   t  d¡ |  ¡ }| j tjtj¡}|s|rt ¡  |r2| j	 
¡  | j | j	d¡s2	 | j | j	d¡r)t  d¡ |  ¡  dS )z1
		Controller main loop. The call is blocking.
		zstarting controlleri  zterminating controllerN)ÚlogÚinfoÚstartr    Úget_boolÚconstsÚ
CFG_DAEMONÚCFG_DEF_DAEMONr   r"   r   r#   ÚwaitÚstop)r   Úresr%   r   r   r   Úrun3   s   

ÿ
zController.runc                 C   s   | j  ¡  d S r   )r"   Úsetr   r   r   r   Ú	terminateF   s   zController.terminateÚsbsc                 C   s   d S r   r   )r   Úprofile_nameÚresultÚerrstrr   r   r   Úprofile_changedI   s   zController.profile_changedc                 C   s   t j |¡ | j |¡ d S r   ©ÚtunedÚlogsÚlog_capture_finishr$   r   )r   r   r   r   r   Ú_log_capture_abortR   s   zController._log_capture_abortÚiiÚsNc                 C   sf   |dkrdS t j |¡}|d u rdS |dkr+tj|| j|gd}| j ||¡ | ¡  |d u r1dS |S )NÚ r   )Úargs)	r<   r=   Úlog_capture_startr
   ÚTimerr?   r$   r   r+   )r   Ú	log_levelÚtimeoutÚcallerr   r   r   r   r   rD   V   s   ÿzController.log_capture_startc                 C   s4   |dkrdS t j |¡}| j |¡ |d u rdS |S ©NrB   r;   )r   r   rH   r2   r   r   r   r>   d   s
   zController.log_capture_finishrB   Úbc                 C   sD   |dkrdS | j  tjtj¡r| j ¡ rdS | j ¡ sdS | j ¡ S ©NrB   FT)	r    r,   r-   r.   r/   r   Ú
is_runningÚ
is_enabledr+   ©r   rH   r   r   r   r+   l   s   


zController.startc                 C   s4   |dkrdS | j  ¡ sd}n| j  ¡ }| j ¡  |S rK   )r   rL   r1   r$   r   )r   rH   r2   r   r   r   r1   w   s   


zController.stopc              
   C   sp   |dkrdS | j  ¡ r|  ¡ }|sdS z
| j  ¡  W |  ¡ S  ty7 } zt d| ¡ W Y d }~dS d }~ww )NrB   FzFailed to reload Tuned: %s)r   rL   r1   Úreload_profile_configr   r)   Úerrorr+   )r   rH   Ústop_okÚer   r   r   Úreload‚   s   
ý€þzController.reloadc              
   C   s  | j  ¡ }d}d}d}zdz|r| j jdd | j  ||¡ W n4 tjjyR } z&d}t|ƒ}|rA| j jj	|krAt
 d| ¡ d}nt
 d| ¡ W Y d }~nd }~ww W |rl|r`t
 d| ¡ n|sgt
 d¡ | j  ¡  ||fS |r‰|r|t
 d| ¡ n|sƒt
 d¡ | j  ¡  w w )	NÚOKTF)Úprofile_switchz@Failed to reapply profile '%s'. Did it change on disk and break?zFailed to apply profile '%s'z>Applying previously applied (possibly out-dated) profile '%s'.z$Applying previously applied profile.)r   rL   r1   Úset_profiler<   Ú
exceptionsr   ÚstrÚprofileÚnamer)   rP   Úwarnr*   r+   )r   r7   ÚmanualÚwas_runningÚmsgÚsuccessÚreapplyrR   r   r   r   Ú_switch_profile‘   s@   
€ù€	

ù
ûzController._switch_profilez(bs)c                 C   s   |dkrdS |   |d¡S )NrB   ©FÚUnauthorizedT)ra   ©r   r7   rH   r   r   r   Úswitch_profile¬   ó   zController.switch_profilec                 C   s    |dkrdS |   ¡ }|  |d¡S )NrB   rb   F)Úrecommend_profilera   )r   rH   r7   r   r   r   Úauto_profile²   s   zController.auto_profilec                 C   s&   |dkrdS | j jd ur| j jjS dS rI   )r   rY   rZ   rN   r   r   r   Úactive_profile¹   s
   
zController.active_profilez(ss)c              
   C   s   |dkrdS | j j}|d u r<z| j ¡ \}}|d u r|d u}W n ty; } zd}t|ƒ}||fW  Y d }~S d }~ww |rAtjntj}|dfS )NrB   )Úunknownrc   rj   )	r   r\   r#   Úget_active_profiler   rX   r-   ÚACTIVE_PROFILE_MANUALÚACTIVE_PROFILE_AUTO)r   rH   r\   rY   rR   ÚmoderP   r   r   r   Úprofile_modeÂ   s"   €€ýzController.profile_modec                 C   s   |dkrdS | j jpdS rI   )r   Úpost_loaded_profilerN   r   r   r   rp   Ô   rf   zController.post_loaded_profilec                 C   sB   |dkrdS | j  ¡ r| j  ¡  | j  ¡ r| j jd dd dd dS )NrB   FT)Úsave_instantly)r   rL   r1   rM   Úset_all_profilesrN   r   r   r   ÚdisableÚ   s   


ÿzController.disablec                 C   s   |dkrdS | j  ¡ S ©NrB   F)r   rL   rN   r   r   r   rL   å   s   
zController.is_runningÚasc                 C   ó   |dkrg S | j jj ¡ S rI   )r   Úprofile_loaderÚprofile_locatorÚget_known_namesrN   r   r   r   Úprofilesë   ó   zController.profilesza(ss)c                 C   rv   rI   )r   rw   rx   Úget_known_names_summaryrN   r   r   r   Ú	profiles2ñ   r{   zController.profiles2z(bsss)c                 C   sP   |dkrt ddddƒS |d u s|dkr|  ¡ }t | jjj |tjtjgdg¡ƒS rt   )	Útupleri   r   rw   rx   Úget_profile_attrsr-   ÚPROFILE_ATTR_SUMMARYÚPROFILE_ATTR_DESCRIPTIONrd   r   r   r   Úprofile_info÷   s
   "zController.profile_infoc                 C   s   |dkrdS | j j ¡ S rI   )r   Úprofile_recommenderÚ	recommendrN   r   r   r   rg   ÿ   rf   zController.recommend_profilec                 C   s   |dkrdS | j jddS )NrB   F©Úignore_missing©r   Úverify_profilerN   r   r   r   rˆ     r{   zController.verify_profilec                 C   s   |dkrdS | j jddS )NrB   FTr…   r‡   rN   r   r   r   Úverify_profile_ignore_missing  r{   z(Controller.verify_profile_ignore_missingz	a{sa{ss}}c                 C   sr   |dkrdS i }| j  ¡ D ])}|j d¡d  dd¡d }| ¡ }i ||< | ¡ D ]\}}t|ƒ|| |< q)q|S )zuReturn dictionary with accesible plugins

		Return:
		dictionary -- {plugin_name: {parameter_name: default_value}}
		rB   FÚ.éÿÿÿÿÚ_é   )r   Úget_all_pluginsr   ÚsplitÚ_get_config_optionsÚitemsrX   )r   rH   ÚpluginsÚplugin_classÚplugin_nameÚconf_optionsÚkeyÚvalr   r   r   rŽ     s   ÿzController.get_all_pluginsc                 C   ó   |dkrdS | j  t|ƒ¡S )z"Return docstring of plugin's classrB   F)r   Úget_plugin_documentationrX   ©r   r”   rH   r   r   r   r™   #  s   z#Controller.get_plugin_documentationza{ss}c                 C   r˜   )zžReturn dictionary with plugin's parameters and their hints

		Parameters:
		plugin_name -- name of plugin

		Return:
		dictionary -- {parameter_name: hint}
		rB   F)r   Úget_plugin_hintsrX   rš   r   r   r   r›   *  s   
zController.get_plugin_hintsr   )#r   r   r   Ú__doc__r   r3   r5   r   Úsignalr:   r?   ÚexportrD   r>   r+   r1   rS   ra   re   rh   ri   ro   rp   rs   rL   rz   r}   r‚   rg   rˆ   r‰   rŽ   r™   r›   Ú__classcell__r   r   r'   r   r   %   sf    
























)r<   r   Ú
tuned.logsÚtuned.exceptionsr   r
   Útuned.constsr-   Útuned.utils.commandsr   Ú__all__r=   Úgetr)   Úobjectr   Ú
interfacesÚExportableInterfacer   r   r   r   r   Ú<module>   s    
