o
    ïì¿`@  ã                   @   sT   d dl Z d dlZddlmZ ddlmZ dd„ ZG dd„ deƒZ	G d	d
„ d
eƒZ
dS )é    Né   )Úlog)Úxmlutilc                 C   sj   | }t  dd| ¡} t  dd| ¡} t  dd| ¡} t  dd| ¡} t  d	d
| ¡} t || ¡}|r3t d|¡ | S )Nz	arch=".*"zarch="i686"zdomain type=".*"zdomain type="test"zmachine type=".*"Ú z>exe<z>hvm<z>linux<z>xen<z!virtinst test sanitizing diff
:%s)ÚreÚsubr   Údiffr   Údebug)ÚxmlÚorigr   © r   ú'/usr/share/virt-manager/virtinst/uri.pyÚsanitize_xml_for_test_define   s   r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚURIz:
    Parse an arbitrary URI into its individual parts
    c                 C   s
  || _ |  |¡}|d | _ttjj|dd … ƒ\| _| _| _	| _
| _d| _d| jv r5| j dd¡\| _| _d| _d| _| j d¡rid| jv rid| jv rV| j d	d¡\| _| _d | jdd …  dd¡¡| _d
| _nd	| jv ry| j d	d¡\| _| _tt d| j¡ƒ| _d S )Nr   r   r   ú+Fú[ú]z]:ú:Tz	^[0-9.]+$)ÚuriÚ_splitÚschemeÚmapÚurllibÚparseÚunquoteÚusernameÚhostnameÚpathÚqueryÚfragmentÚ	transportÚrsplitÚportÚis_ipv6Ú
startswithÚjoinÚsplitÚboolr   ÚmatchÚhost_is_ipv4_string)Úselfr   Ú	split_urir   r   r   Ú__init__!   s&   

ÿ


zURI.__init__c           
      C   sÞ   ddd„}d } } } }}|  d¡}|dkrg|d |…  ¡ ||d d … }}|d d… dkrO||dƒ\}}|  d	¡}	|	dkrO|d|	… }||	d d … }d
|v r[| d
d¡\}}d|v rg| dd¡\}}||||||fS )Nr   c                 S   sB   dD ]}|   ||¡}|dkr nqt| ƒ}| ||… | |d … fS )Nz/?#r   )ÚfindÚlen)ÚurlÚstartÚcÚdelimr   r   r   Úsplitnetloc?   s   ÿzURI._split.<locals>.splitnetlocr   r   r   é   z//ú@ú#ú?©r   )r-   Úlowerr&   )
r*   r   r3   r   r   Únetlocr   r   ÚiÚoffsetr   r   r   r   >   s    
	
"
z
URI._splitN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   r   r   r   r   r   r      s    r   c                   @   s8   e Zd ZdZdZedd„ ƒZdd„ Zdd„ Zd	d
„ Z	dS )ÚMagicURIaX  
    Handle magic virtinst URIs we use for the test suite and UI testing.
    This allows a special URI to override various features like capabilities
    XML, reported connection and libvirt versions, that enable testing
    different code paths.

    A magic URI has 3 parts:

        1) Magic prefix __virtinst_test__
        2) Actual openable URI, usually a test:/// URI
        3) Comma separated options

    The available options are:

        * 'predictable': Generate predictable UUIDs, MAC addresses, and
            temporary file names.
        * 'fakeuri': The URI to advertise as the actual connection URI
        * 'connver=%d': Override the connection (hv) version
        * 'libver=%d': Override the libvirt version
        * 'caps=%s': Points to a file with capabilities XML, that will
                     be returned in conn.getCapabilities. Ex.
                     files in test/capabilities-xml/
        * 'domcaps=%s': Points to a file with domain capabilities XML, that
                        will be returned in conn.getDomainCapabilities

    See tests/utils.py for example URLs
    Ú__virtinst_test__c                 C   s   |   tj¡S ©N)r$   rA   ÚVIRTINST_URI_MAGIC_PREFIX)r   r   r   r   Úuri_is_magicw   s   zMagicURI.uri_is_magicc                    s  |   |¡sJ ‚ddlm} | | jd¡}| dd¡}|d | _t|t|ƒdkr+|d p,dƒƒ‰ ‡ fdd„}|dƒ| _	ˆ  
d	d ¡| _ˆ  
d
d ¡| _ˆ  
dd ¡| _ˆ  
dd ¡| _| jr`t| jƒ| _n| jrfd| _ˆ  
dd ¡| _| jrvt| jƒ| _d | _ˆ r‚dˆ  | _d S d S )Nr   )Úparse_optstr_tuplesr   ú,r   c                    s   | ˆ v }ˆ   | d ¡ |S rC   )Úpop)ÚfieldÚret©Úoptsr   r   Úpop_bool…   s   z#MagicURI.__init__.<locals>.pop_boolÚpredictableÚfakeuriÚcapsÚdomcapsÚconnverl    d(	 ÚlibverzMagicURI has unhandled opts=%s)rE   ÚclirF   ÚreplacerD   r&   Úopen_uriÚdictr.   rN   rH   rO   ÚcapsfileÚdomcapsfileÚconn_versionÚintÚlibvirt_versionÚ_err)r*   r   rF   rJ   rM   r   rK   r   r,   {   s.   
 
ÿzMagicURI.__init__c                 C   s   | j rt| j ƒ‚d S rC   )r]   ÚRuntimeError)r*   r   r   r   Úvalidate¢   s   
ÿzMagicURI.validatec                    sˆ   | j rt| j ƒ ¡ ‰ ‡ fdd„|_| jr%t| jƒ ¡ ‰d‡fdd„	}||_| jrB|j‰|j‰‡fdd„}‡fdd	„}||_||_d
S d
S )z˜
        After the connection is open, we need to stub out various functions
        depending on what magic bits the user specified in the URI
        c                      s   ˆ S rC   r   r   )Úcapsxmlr   r   Ú<lambda>®   s    z3MagicURI.overwrite_conn_functions.<locals>.<lambda>r   c                    s.   | }|}|}|}ˆ }|rt  dd| |¡}|S )Nzarch>.+</archzarch>%s</arch)r   r   )ÚemulatorÚarchÚmachineÚvirttypeÚflagsÚignorerJ   )Ú
domcapsxmlr   r   Úfake_domcapsµ   s   z7MagicURI.overwrite_conn_functions.<locals>.fake_domcapsc                    s   t | ƒ} ˆ | |ƒS rC   ©r   )r
   rf   )Ú
origcreater   r   Ú	newcreateÅ   s   
z4MagicURI.overwrite_conn_functions.<locals>.newcreatec                    s   t | ƒ} ˆ | ƒS rC   rj   )r
   )Ú
origdefiner   r   Ú	newdefineÈ   s   z4MagicURI.overwrite_conn_functions.<locals>.newdefineNr8   )	rX   ÚopenÚreadÚgetCapabilitiesrY   ÚgetDomainCapabilitiesrO   Ú	createXMLÚ	defineXML)r*   Úconnri   rl   rn   r   )r`   rh   rk   rm   r   Úoverwrite_conn_functions¦   s   
öz!MagicURI.overwrite_conn_functionsN)
r=   r>   r?   r@   rD   ÚstaticmethodrE   r,   r_   rv   r   r   r   r   rA   Y   s    
'rA   )r   Úurllib.parser   Úloggerr   r   r   r   Úobjectr   rA   r   r   r   r   Ú<module>   s   <