
    \e"                     N    d Z ddlZddlZddlZddlmZ ddlmZ  G d de      Z	y)zq
Helper methods to deal with images.

.. versionadded:: 3.1

.. versionchanged:: 3.14.0
   add paramter format.

    N)_)strutilsc                       e Zd ZdZ ej
                  dej                        Z ej
                  d      Z ej
                  dej                        Z	ddZ
d Zd Zd	 Zd
 Zd Zy)QemuImgInfoa.  Parse Qemu image information from command `qemu-img info`'s output.

    The instance of :class:`QemuImgInfo` has properties: `image`,
    `backing_file`, `file_format`, `virtual_size`, `cluster_size`,
    `disk_size`, `snapshots` and `encrypted`.

    The parameter format can be set to 'json' or 'human'. With 'json' format
    output, qemu image information will be parsed more easily and readable.
    However 'human' format support will be dropped in next cycle and only
    'json' format will be supported. Prefer to use 'json' instead of 'human'.
    z*^(.*?)\s*\(actual\s+path\s*:\s+(.*?)\)\s*$z^([\w\d\s\_\-]+):(.*)$zF([0-9]+[eE][-+][0-9]+|\d*\.?\d+)\s*(\w+)?(\s*\(\s*(\d+)\s+bytes\s*\))?Nc                    |dk(  rt        j                  |xs d      }|j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d	      | _	        |j                  d
g       | _
        |j                  d      rdnd | _        |j                  d      | _        y |t        j                  ddt               | j!                  |xs d      }|j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _	        |j                  dg       | _
        |j                  d      | _        d | _        y )Njsonz{}filenamezbacking-filenamezbacking-filename-formatformatzvirtual-sizezcluster-sizezactual-size	snapshots	encryptedyeszformat-specificzdThe human format is deprecated and the format parameter will be removed. Use explicitly json insteadxena)versioncategory imagebacking_filebacking_file_formatfile_formatvirtual_sizecluster_size	disk_sizesnapshot_list)r   loadsgetr   r   r   r   r   r   r   r   r   format_specificdebtcollector	deprecateFutureWarning_parse)self
cmd_outputr
   detailss       7/usr/lib/python3/dist-packages/oslo_utils/imageutils.py__init__zQemuImgInfo.__init__8   s   Vjj!3t4G Z0DJ ',> ?D'.{{3L'MD$&{{84D 'N ;D 'N ;D$[[7DN$[[b9DN&-kk+&>UDDN#*;;/@#AD %''C"*	,
 kk*"23G W-DJ 'N ;D'.{{3H'ID$&{{=9D 'N ;D 'N ;D$[[5DN$[["=DN$[[5DN#'D     c                    d| j                   z  d| j                  z  d| j                  z  d| j                  z  d| j                  z  d| j
                  z  d| j                  z  g}| j                  r|j                  d| j                  z         | j                  r|j                  d	| j                  z         | j                  r|j                  d
| j                  z         dj                  |      S )Nz	image: %szfile_format: %szvirtual_size: %szdisk_size: %szcluster_size: %szbacking_file: %szbacking_file_format: %szsnapshots: %szencrypted: %szformat_specific: %s
)r   r   r   r   r   r   r   r   appendr   r   join)r!   liness     r$   __str__zQemuImgInfo.__str__X   s    $**$ 0 00!2!22dnn,!2!22!2!22%(@(@@
 >>LL4>>9:>>LL4>>9:LL.1E1EEFyyr&   c                 t    |j                         j                         }dD ]  }|j                  |d      } |S )N) -r   )lowerstripreplace)r!   fieldcs      r$   _canonicalizezQemuImgInfo._canonicalizej   s<    
 ##% 	*AMM!S)E	*r&   c                    | j                   j                  |      }|st        t        d      |z        |j	                  d      }d|j                         v r$t        t        |j	                  d            d      }|j	                  d      }|j	                  d      }|rt        |j	                  d            S |st        |      S t        |      dk(  r
|dk7  r|dz  }t        j                  ||d	
      S )NzInvalid input value "%s".   ez.0f         BT)
return_int)SIZE_REsearch
ValueErrorr   groupr0   r
   floatintlenr   string_to_bytes)r!   r#   	real_size	magnitudeunit_of_measure
bytes_infos         r$   _extract_byteszQemuImgInfo._extract_bytest   s    LL''0	Q:;gEFFOOA&	)//##uY__Q%78%@I#//!,__Q'
yq)** y>!1$C)?s"O'')_(M379 	9r&   c           	         |}|dk(  r>| j                   j                  |      }|r|j                  d      j                         }|S |dv r|dv rd}|S | j	                  |      }|S |dk(  r |j                         j                         }|S |dk(  r|r |j                  d      j                  d      st        d	      }t        |      g }|r|d   }|j                         }t        |      d
k7  r	 |S |d   j                  d      }	t        |	      dk7  r	 |S |j                  d       |j                  |d   |d   |d   |d   |d   dz   |d   z   d       |r|S )Nr   r9   )r   r   r   )Noneunavailabler   r   r   IDz.Snapshot list encountered but no header found!      :r:   r7   r;   r.   )idtagvm_sizedatevm_clock)BACKING_FILE_REmatchrA   r1   rJ   r0   pop
startswithr   r@   splitrD   r)   )
r!   root_cmdroot_detailslines_afterreal_detailsbacking_matchmsglineline_piecesdate_piecess
             r$   _extract_detailszQemuImgInfo._extract_details   s   #~% 0066|DM,2215;;=H G FF66 @ =  $22<@< ; &'--/557L8 7 (kooa&8&C&CD&IHI o%L "1~"jjl{#q(  *!n2237{#q(  "##%a.&q>*1~'N +A 4{1~ E%  $ r&   c                    i }|j                         D cg c]  }|j                         s| }}|r|j                  d      }| j                  j	                  |      }|rZ| j                  |j                  d            }|sS|j                  d      j                         }| j                  |||      }	|	||<   |r|S c c}w )Nr   r7   r9   )
splitlinesr1   rY   TOP_LEVEL_RErX   r5   rA   re   )
r!   r"   contentsxr+   rb   	top_levelrootr]   r#   s
             r$   r    zQemuImgInfo._parse   s     &113AqqwwyAA99Q<D))//5I)))//!*<=(q1779//lEJ!(   Bs
   C C )Nhuman)__name__
__module____qualname____doc__recompileIrW   rh   r>   r%   r,   r5   rJ   re   r     r&   r$   r   r   %   ss    
 !bjj #468dd<O2::78Lbjj CG(@ $9(*Xr&   r   )
rq   r   rr   r   oslo_utils._i18nr   
oslo_utilsr   objectr   ru   r&   r$   <module>ry      s)   $  	   d& dr&   