
    Se.                         d Z ddlZddlmZ  ej                  e      Z G d dej                        Zde	de
fd	Zd
e	de	fdZd
e	de	fdZde	defdZ G d d      Zy)a  
Copyright (c) 2022, Ian Santopietro
All rights reserved.

This file is part of RepoLib.

RepoLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

RepoLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with RepoLib.  If not, see <https://www.gnu.org/licenses/>.
    N   )utilc                   (     e Zd ZdZdd fd
Z xZS )DebParseErrorz) Exceptions related to parsing deb lines.r   )codec                2    t        |   |i | || _        y)zExceptions related to parsing deb lines.

        Arguments:
            code (:obj:`int`, optional, default=1): Exception error code.
    N)super__init__r   )selfr   argskwargs	__class__s       2/usr/lib/python3/dist-packages/repolib/parsedeb.pyr
   zDebParseError.__init__    s     	$)&)	    )__name__
__module____qualname____doc__r
   __classcell__)r   s   @r   r   r      s    3#$  r   r   linereturnc                    | j                         } | j                         }|D ]5  }t        j                  |      st	        |      ||j                  |      <   7 dj                  |      } g }d}d}| D ]G  }|dk(  rd}||z  }|dk(  rd}||z  }|j                         r|s|j                  |       d}C||z  }I t        |      dkD  r|j                  |       |S )z Improved string.split() with support for things like [] options. 
    
    Adapted from python-apt

    Arguments:
        line(str): The line to split up.
      F[T]r   )
stripsplitr   url_validatordecode_bracketsindexjoinisspaceappendlen)r   	line_listipiecestmpp_foundchars          r   debsplitr,   )   s     ::<D

I ?a ,;A,>Iiooa()? 88IDFCG 3;G4KCS[G4KC\\^GMM#C4KC 3x!|cMr   wordc                 N    | j                  dd      } | j                  dd      } | S )a   Encodes any [ and ] brackets into URL-safe form

    Technically we should never be recieving these, and there are other things 
    which should technically be encoded as well. However, square brackets 
    actively break the URL parsing, and must be strictly avoided.

    Arguments:
        word (str): the string to encode brackets in.
    
    Returns:
        `str`: the encoded string.
    r   %5Br   %5Dreplacer-   s    r   encode_bracketsr4   M   s)     <<U#D<<U#DKr   c                 N    | j                  dd      } | j                  dd      } | S )aK   Un-encodes [ and ] from the input

    Since our downstream libraries should also be encoding these correctly, it 
    is better to retain these as the user entered, as that ensures they can 
    recognize it properly.

    Arguments:
        word (str): The string to decode.

    Returns:
        `str`: the decoded string.
    r/   r   r0   r   r1   r3   s    r   r    r    ^   s)     <<s#D<<s#DKr   tailc                 $   t        j                  |       } d| v }t        j                  d|       d| v }t        j                  d|       | j	                         }d}d}d}d}d}|D ]  }	t        j                  d|	       |	j                  d      j                         j                  d      }
|	j                  d      j                         j                  d      }d|	v r	|
s|sd}d}n|
rd	}d}|rd}d	}|r|
s	||	 d
z  }|r|s	||	 z  }d}|r|r|	j                  d      }|| d
z  } |j                         }|j                         }|j                         }|s|r|}|r|st        d| d      |r|st        d| d      |||fS )a   Find a Repolib name within the given comment string.

    The name should be headed with "X-Repolib-Name:" and is not space terminated.
    The ident should be headed with "X-Repolib-ID:" and is space terminated.

    Either field ends at the end of a line, or at a subsequent definition of a
    different field, or at a subsequent ' #' substring. Additionally, the ident
    field ends with a subsequent space.

    Arguments:
        tail (str): The comment to search within.
    
    Returns: tuple(name, ident, comment):
        name (str): The detected name, or None
        ident (str): The detected ident, or None
        comment (str): The string with the name and ident removed
    zX-Repolib-NamezLine name found: %szX-Repolib-IDzLine ident found: %sFr   zChecking line item: %s#Tr   z-Could not parse repository name from comment z;. Make sure you have a space between the colon and the Namez.Could not parse repository ident from comment z<. Make sure you have a space between the colon and the Ident)r   strip_hasheslogdebugr   r   
startswithr   )r6   has_name	has_identparts
name_foundident_foundnameidentcommentitemitem_is_nameitem_is_identcs                r   parse_name_identrI   o   s   $ T"D  4'HII#X.$&III$i0**,EJKDEG 		*D1zz#,,.99:JK

3--/::>J$;|MJKJKJKltfAJDvEKK

3A!AwG=@ ::<DKKMEmmoGD ;G9 E> >
 	
 <WI F? ?
 	

 r   c                   B    e Zd ZdZd
deddfdZdedefdZdedefd	Z	y)ParseDebz] Parsing for source entries. 

    Contains parsing helpers for one-line format sources.
    r;   r   Nc                 J    || _         d| _        d| _        d| _        d| _        y)z
        Arguments:
            debug (bool): In debug mode, the structured data is always returned
                at the end, instead of checking for sanity (default: `False`)
        r   FN)r;   	last_linelast_line_valid	curr_linecurr_line_valid)r   r;   s     r   r
   zParseDeb.__init__   s)     
 %* %*r   optc           	      n   |j                         }|dd j                         }|j                         }i }|D ]Q  }|j                  d      \  }}|j                  d      }dj                  |      }	 t        j                  |   }|||<   S |S # t
        $ r t        d| j                   d| d      w xY w)	a   Parses a string of options into a dictionary that repolib can use.

        Arguments:
            opt(str): The string with options returned from the line parser.
        
        Returns:
            `dict`: The dictionary of options with key:val pairs (may be {})
        r   =,r   zCould not parse line z	: option z; is not a valid debian repository option or is unsupported.)r   r   r"   r   options_inmapKeyErrorr   rO   )r   rQ   optionsparsed_optionspre_keyvaluesvaluekeys           r   parse_optionszParseDeb.parse_options   s     iik!Bioo))+  	(C!iinOGV\\#&F(E,,W5 #(N3	(   #+DNN+;9SE JN N s   2B&B4r   c                 n   | j                   | _        | j                  | _        |j	                         | _         g }| j                   dk(  }| j                   dk(  }|s|rt        d| j                    d      i }d|d<   d|d<   d|d<   g |d	<   d|d
<   d|d<   d|d<   g |d<   i |d<   |j                  d      rJd|d<   t        j                  |      }|j                         }|d   dvrt        d| j                    d      |j                  d      }|dkD  rD||dz   d j	                         }t        |      \  |d<   |d<   }|d	   j                  |       |d| }t        |      }t        |      dk  rt        d| j                    d      |j                  d      }	|	dv rt        j                   |	      |d
<   nt        d| j                    d      d}
d}|D ].  }|j                  d      sd|v rd}|j#                  |      }
-d}
0 |rt        d      |
dk7  r#| j%                  |j                  d            |d<   t        |      dk  rt        d| j                    d      |j                  d      }t        j&                  |      r||d<   nt        d| j                    d|       |j                  d      |d<   g }|D ]  }|d   j                  |        |d
   }|d   }|d   }|r|r|r|j)                         S | j*                  r|j)                         S t        d| j                    d      ) z Parse a deb line into its individual parts.

        Adapted from python-apt

        Arguments:
            line (str): The line input to parse
        
        Returns:
            (dict): a dict containing the requisite data.
        r8   r   zCurrent line "z
" is emptyTenabledrB   rC   comments	repo_typeurisuite
componentsrX   Fr   )debzdeb-srcz" is invalidr   N   z
The line "z(" does not have enough pieces to bevalidz" is of invalid type.r   cdromz-Repolib cannot currently accept CDROM Sources   z" has invalid URI: z	The line zd could not be parsed due to an unknown error (Probably missing the repo type, URI, or a suite/path).)rO   rM   rP   rN   r   r   r<   r   r9   r   findrI   r$   r,   r%   pop
SourceTyper!   r^   r   copyr;   )r   r   r?   line_is_commentline_is_emptyline_parsedcomments_indexraw_commentsra   rb   	uri_indexis_cdrompartline_uriline_componentscomphas_typehas_uri	has_suites                      r   
parse_linezParseDeb.parse_line   s    #33..C/",m.0@
 KLL!%I F!G"$J#%K E!G$&L!!#I??3%*K	"$$T*DJJLE811#nT^^4DL$QRR3A#NQ$6$78>>@L
 !.	F#G$
#**84(Du:>T^^, -  
 		!	**'+y'AK$*T^^,<<Q RSS 	
  	"Ds#d?#H %D 1I !I	"    OPP>%)%7%7		!%EK	"{aT^^, -  
 99Q<h'!)K  T^^,,?zJ   %yy|G! 	3D%,,T2	3 {+e$(	I ##%%::##%%' ( 
 	
r   )F)
r   r   r   r   boolr
   strdictr^   r|    r   r   rK   rK      sC    

+T 
+d 
+  >|
c |
d |
r   rK   )r   loggingr   r   	getLoggerr   r:   	RepoErrorr   r~   listr,   r4   r    tuplerI   rK   r   r   r   <module>r      s   (  g!
DNN 
"# "$ "H  "  "T # T % T nm
 m
r   