o
    /€__   ã                   @   s"   d dl Z dd„ Z		d
dd	„ZdS )é    Nc                 C   sD   d}|dur z| |ƒdurd}W |S W |S  t jy   Y |S w |S )z¡
    Run the passed collision function with val as the only argument:
    If libvirtError is raised, return False
    If no libvirtError raised, return True
    FNT)ÚlibvirtÚlibvirtError)Úcollision_cbÚvalÚcheck© r   ú0/usr/share/virt-manager/virtinst/generatename.pyÚcheck_libvirt_collision   s   üþþr	   Ú é   ú-Fc           
      C   sv   t | ƒ} tt||d ƒƒ}|sdg| }d}|D ]}| }	|dur(|	d||f 7 }	|	|7 }	||	ƒs4|	} nq|s9J ‚|S )aI  
    Generate a new name from the passed base string, verifying it doesn't
    collide with the collision callback.

    This can be used to generate disk path names from the parent VM or pool
    name. Names generated look like 'base-#suffix', ex:

    If foobar, and foobar-1.img already exist, and:
    base   = "foobar"
    suffix = ".img"

    output = "foobar-2.img"

    :param base: The base string to use for the name (e.g. "my-orig-vm-clone")
    :param collision_cb: A callback function to check for collision,
        receives the generated name as its only arg
    :param start_num: The number to start at for generating non colliding names
    :param sep: The separator to use between the basename and the
        generated number (default is "-")
    :param force_num: Force the generated name to always end with a number
    i † Nz%s%d)ÚstrÚlistÚrange)
Úbaser   ÚsuffixÚ	start_numÚsepÚ	force_numÚnumrangeÚretÚiÚtrynamer   r   r   Úgenerate_name   s    
þr   )r
   r   r   F)r   r	   r   r   r   r   r   Ú<module>   s
   ÿ