o
    <cp                     @   s   d dl Z G dd dZdS )    Nc                   @   sP   e Zd Z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S )Templatea   Template object encapsulates a dictionary of template options.
        Given a dataset.template file, this object creates a dictionary of options keyed on the option name. This
        will allow for accessing of various template parameters in constant time (as opposed to O(n) time) and includes
        a simple, user friendly syntax.
        Use as follows:
            template = Template(file_name)                                  # create a template object
            options = template.get_options()                                # access the options dictionary
            dataset = options['dataset']                                    # access a specific option
            options = template.update_options_from_file(default_template_file)        # access a specific option
    c                 C   s   |  || _dS )a   Initializes Template object with a custom template file.
            The provided template file is parsed line by line, and each option is added to the options dictionary.
            :param custom_template_file: file, the template file to be accessed
        N)read_optionsoptions)selfcustom_template_file r   S/home/exouser/operations/rsmas_insar/tools/MiaplPy/miaplpy/objects/read_template.py__init__   s   zTemplate.__init__c                 C   s   d| dd  dd i}t|5}|D ]*}d|v r?|ds?| d}|d  }|d	   d
d d}||t|< qW d   n1 sJw   Y  | |}|S )z Read template options.

            :param template_file: file, the template file to be read and stored in a dictionary
        dataset/.r   =%#z =    r    N)splitopen
startswithrstripstripstrcorrect_keyvalue_quotes)r   template_filer   templatelinepartskeyvaluer   r   r   r      s   


zTemplate.read_optionsc              	   C   s2  t j|}| |}|d }t|dl}t|dD ]^}dd | ddD }|dsst|dkrs|d	 }t	
|d d
ddd	  }	|| j v rs| j| |	krs|
|	| j| d}| j| ||< td||	| j|  || qW d   n1 sw   Y  d||}
t |
 || _| jS )z Update default template file with the options from custom template file read initially.

            :param default_template_file: file, the template file to be updated
        z.tmpwrc                 S   s   g | ]}|  qS r   )r   ).0ir   r   r   
<listcomp>E   s    z+Template.update_options.<locals>.<listcomp>r   r   r   r   
 r   z"From custom_template {}: {} --> {}Nzmv {} {})ospathabspathr   r   r   r   r   lenr   replacer   keysprintformatwritesystem)r   Zdefault_template_file_tmpZdefault_template_filedefault_optionstmp_filef_tmpr   cr   r    ZmvCmdr   r   r   update_options9   s(   
 
zTemplate.update_optionsc                 C   s   |   }|||< dS )z Updates the options dictionary key with the specified value.
            :param key   : the dictionary key to update
            :param value : the value to replace the key with
        N)get_options)r   r   r    r   r   r   r   update_optionU   s   zTemplate.update_optionc                 C   s   | j S )z Provides direct access to the options dictionary.
            This should be used in lieu of directly accessing the options dictionary via Template().options
        r   r   r   r   r   r7   ]   s   zTemplate.get_optionsc                 C   s
   | j d S )z Provides quick access to the dataset property of the options dictionary.
            Should be used to quickly access the dataset name when directories require the dataset name
        r
   r9   r:   r   r   r   get_dataset_namec   s   
zTemplate.get_dataset_namec                 C   sD   dD ]}||  v r|| }| |}|||< t|d |  q|S )z quote-independent reformatting of sentinel.subswath key-value:
             1 2 ---> '1 2'
             1  ---> 1
            '1' ---> 1
            -1 0.15 -91.6 -90.9 ---> '-1 0.15 -91.6 -90.9'
        )zmintpy.subset.yxzmintpy.subset.laloz-->)r-   check_correct_quotesr.   )r   Z
options_initemZ
value_origZ	value_newr   r   r   r   i   s   
z Template.correct_keyvalue_quotesc                 C   sl   |dd dkr| d}t|dkr|dd }|S |}|S | d}t|dkr2d| d }|S |}|S )zO checks weather quotes are as required and corrects/replaces if needed
        r   r   'r      )r   r+   )r   stringZnum_itemZ
out_stringr   r   r   r<   y   s   


zTemplate.check_correct_quotesN)__name__
__module____qualname____doc__r	   r   r6   r8   r7   r;   r   r<   r   r   r   r   r      s    r   )r(   r   r   r   r   r   <module>   s   