o
    _                     @   sv   d dl Zd dlZd dlmZmZ d dlmZ d dlZ	d dl
Z
d dlZd dlZd dlmZ ej ZG dd deZdS )    N)	ConfigObjConfigObjError)InvalidProfileExceptionc                   @   sp   e Zd ZdZg dZdd Zdd Zedd Ze	d	d
 Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )Loaderz
	Profiles loader.
	)_profile_locator_profile_merger_profile_factory_global_config
_variablesc                 C   s"   || _ || _|| _|| _|| _d S N)r   r   r   r	   r
   )selfprofile_locatorprofile_factoryprofile_mergerglobal_config	variables r   7/usr/lib/python3/dist-packages/tuned/profiles/loader.py__init__   s
   
zLoader.__init__c                 C   s   t jj||S r   )tunedprofilesprofileProfile)r   profile_nameconfigr   r   r   _create_profile   s   zLoader._create_profilec                 C   s   t d|S )Nz^[a-zA-Z0-9_.-]+$)rematch)clsr   r   r   r   	safe_name   s   zLoader.safe_namec                 C   s   | j S r   )r   )r   r   r   r   r   "   s   zLoader.profile_locatorc                 C   s   t |tur
| }tt| j|}t|dkrtdt|dkr-tdd	|  n	td|d   g }g }| 
||| t|dkrN| j|}n|d }d	||_d|jv rk| j|jd j |jd= | | | | |S )	Nr   z.No profile or invalid profiles were specified.   zloading profiles: %sz, zloading profile: %s r   )typelistsplitfilterr   lenr   loginfojoin_load_profiler   mergenameunitsr
   add_from_cfgoptions_expand_vars_in_devices_expand_vars_in_regexes)r   profile_namesr   processed_filesfinal_profiler   r   r   load&   s*   


zLoader.loadc                 C   s,   |j D ]}| j|j | j|j | _qd S r   )r-   r
   expanddevicesr   r   unitr   r   r   r0   D   s   
zLoader._expand_vars_in_devicesc                 C   sH   |j D ]}| j|j | j|j | _| j|j | j|j | _qd S r   )r-   r
   r6   cpuinfo_regexuname_regexr8   r   r   r   r1   H   s   
zLoader._expand_vars_in_regexesc           	   	   C   s   |D ]K}| j ||}|d u rtd|tt| j jf || | |}| j	||}d|j
v rHtd| j|j
d}| ||| || qd S )Nz!Cannot find profile '%s' in '%s'.includez\b\s*[,;]\s*)r   
get_configr   r#   reversed_load_directoriesappend_load_config_datar   creater/   r   r$   r
   r6   popr*   )	r   r2   r   r3   r,   filenamer   r   include_namesr   r   r   r*   M   s   


zLoader._load_profilec                 C   s   t d||S )Nz(?<!\\)\$\{i:PROFILE_DIR\})r   sub)r   profile_dirstringr   r   r   _expand_profile_dir\   s   zLoader._expand_profile_dirc              
   C   sN  z
t |dddd}W n ty } ztd| |d }~ww t }t| D ]1}t ||< z
t||  }W n tyI   td||f w |D ]}|| | || |< qLq(tj	
|}|D ]}|| D ]}| ||| | || |< qhqb|D ](}	d||	 v r||	 dd d urtj	|||	 d }
tj	|
g||	 d< q||S )NTF)raise_errorslist_valuesinterpolationzCannot parse '%s'.z(Error parsing section '%s' in file '%s'.script)r   r   r   collectionsOrderedDictr#   keysAttributeErrorospathdirnamerI   getr)   normpath)r   	file_name
config_objer   sectionrP   optiondir_name	unit_namescript_pathr   r   r   rA   _   s8    zLoader._load_config_dataN)__name__
__module____qualname____doc__	__slots__r   r   classmethodr   propertyr   r5   r0   r1   r*   rI   rA   r   r   r   r   r      s    

r   )tuned.profiles.profiler   tuned.profiles.variables	configobjr   r   tuned.constsconstsos.pathrR   rN   
tuned.logsr   tuned.profiles.exceptionsr   logsrU   r'   objectr   r   r   r   r   <module>   s    
