
    >g                     ~    d Z ddlZ G d de      Z G d de      Z G d de      Z G d	 d
e      Z G d de      Zy)a0  
.. module:: config
   :synopsis: Parse Asterisk configuration files.

This module provides parsing functionality for asterisk config files.

Example
----------

.. code-block:: python

   import asterisk.config
   import sys
   
   # load and parse the config file
   try:
      config = asterisk.config.Config('/etc/asterisk/extensions.conf')
   except asterisk.config.ParseError as e:
      print "Parse Error line: %s: %s" % (e.line, e.strerror)
      sys.exit(1)
   except IOError as e:
      print "Error opening file: %s" % e.strerror
      sys.exit(1)
   
   # print our parsed output
   for category in config.categories:
      print '[%s]' % category.name   # print the current category

      for item in category.items:
         print '   %s = %s' % (item.name, item.value)


Specification
-------------
    Nc                       e Zd Zy)
ParseErrorN)__name__
__module____qualname__     B/opt/asterisk_venv/lib/python3.12/site-packages/asterisk/config.pyr   r   *   s    r	   r   c                       e Zd Zd Zd Zd Zy)Linec                     d| _         d| _        |j                         }|| _        |j	                  d      }t        |      dk\  r2|d   j                         | _         dj                  |dd        | _        y || _         y )N ;   r      )linecommentstripnumbersplitlenjoin)selfr   r   partss       r
   __init__zLine.__init__/   sn    	zz|

3u:?a(DI88ab	DL DIr	   c                 "    | j                         S N)get_liner   s    r
   __str__zLine.__str__<   s    }}r	   c                     | j                   r'| j                  r| j                  d| j                   S | j                   r| j                  sd| j                   z  S | j                  S )N	;z;%s)r   r   r   s    r
   r   zLine.get_line?   sH    <<DII $		4<<88\\$))4<<''yyr	   N)r   r   r   r   r    r   r   r	   r
   r   r   .   s    r	   r   c                   4    e Zd ZddZd Zd Zd Zd	dZd Zy)
CategoryNc                 .   t         j                  | ||       | j                  rO| j                  d   dk7  s| j                  d   dk7  rt        | j                  d      | j                  dd | _        n|r|| _        nt        d      g | _        g | _        y )Nr   []z)Missing '[' or ']' in category definitionr   z1Must provide name or line representing a category)	r   r   r   r   r   name	Exceptionitemscomments)r   r   numr)   s       r
   r   zCategory.__init__H   s    dD#&99		!#tyy}'; KK!LN N		!BDIDICE E 
r	   c                 p    | j                   rd| j                  d| j                   S d| j                  z  S )Nr&   z]	;z[%s])r   r)   r   s    r
   r   zCategory.get_lineX   s+    <<"&))T\\::		!!r	   c                 :    | j                   j                  |       y r   )r+   appendr   items     r
   r0   zCategory.append]       

$r	   c                 <    | j                   j                  ||       y r   )r+   insert)r   indexr2   s      r
   r5   zCategory.insert`   s    

%&r	   c                 :    | j                   j                  |       y r   )r+   pop)r   r6   s     r
   r8   zCategory.popc   s    

ur	   c                 :    | j                   j                  |       y r   )r+   remover1   s     r
   r:   zCategory.removef   r3   r	   )r   r'   N)r'   )	r   r   r   r   r   r0   r5   r8   r:   r   r	   r
   r$   r$   G   s      "
 ' r	   r$   c                        e Zd ZddZd Zd Zy)ItemNc                     t         j                  | ||       d| _        | j                  r| j	                          y |r|r|| _        || _        y t        d      )Nr   z/Must provide name or value representing an item)r   r   styler   parser)   valuer*   )r   r   r-   r)   r@   s        r
   r   zItem.__init__k   sH    dD#&
99JJLuDIDJMNNr	   c                 v   	 | j                   j                  dd      \  }}|r"|d   dk(  rd| _        |dd  j                         }|j                         | _        || _        y # t        $ rM | j                   j                         d   dk(  rt	        | j
                  d      t	        | j
                  d      w xY w)	N=r   r'   r(   zCategory name missing '['z"Item must be in name = value pairsr   >)	r   r   
ValueErrorr   r   r   r>   r)   r@   )r   r)   r@   s      r
   r?   z
Item.parsev   s    	G))//#q1KD% U1X_DJ!"IOO%EJJL	
  	Gyy $+ .IJJ KK!EG G		Gs   A" "AB8c                     | j                   r5| j                  d| j                  d| j                  d| j                   S | j                  d| j                  d| j                  S )Nz = r"   )r   r)   r>   r@   r   s    r
   r   zItem.get_line   sA    <<'+yy$**djj$,,WW"iiTZZ@@r	   )r   r'   NN)r   r   r   r   r?   r   r   r	   r
   r<   r<   j   s    	O Ar	   r<   c                       e Zd Zd Zd Zd Zy)Configc                 |    || _         g | _        g | _        g | _        | j	                          | j                          y r   )filename	raw_lineslines
categoriesloadr?   )r   rJ   s     r
   r   zConfig.__init__   s2     
 			

r	   c                 T    t        | j                        j                         | _        y r   )openrJ   	readlinesrK   r   s    r
   rN   zConfig.load   s    dmm,668r	   c                    d }d}| j                   D ]  }|dz  }|j                         }|r|d   dk(  rIt        |xs d|      }| j                  j	                  |       |r|j
                  j	                  |       k|d   dk(  rCt        ||      }| j                  j	                  |       | j                  j	                  |       t        ||      }| j                  j	                  |       |r|j	                  |        y )Nr   r   r   r   r&   )	rK   r   r   rL   r0   r,   r$   rM   r<   )r   catr-   r   r2   s        r
   r?   zConfig.parse   s    NN 	D1HC::<D47c>DJB,

!!$'LL''-aCtS)

!!#&&&s+D#

!!$'JJt$'	r	   N)r   r   r   r   rN   r?   r   r	   r
   rH   rH      s    9r	   rH   )	__doc__sysr*   r   objectr   r$   r<   rH   r   r	   r
   <module>rW      sP   "H 	 	6 2  t   FA4 AD)V )r	   