<s> """<STR_LIT>""" <EOL> import re <EOL> from rack . openstack . common . gettextutils import _ <EOL> from rack . openstack . common import strutils <EOL> class QemuImgInfo ( object ) : <EOL> BACKING_FILE_RE = re . compile ( ( r"<STR_LIT>" <EOL> r"<STR_LIT>" ) , re . I ) <EOL> TOP_LEVEL_RE = re . compile ( r"<STR_LIT>" ) <EOL> SIZE_RE = re . compile ( r"<STR_LIT>" , re . I ) <EOL> def __init__ ( self , cmd_output = None ) : <EOL> details = self . _parse ( cmd_output or '<STR_LIT>' ) <EOL> self . image = details . get ( '<STR_LIT:image>' ) <EOL> self . backing_file = details . get ( '<STR_LIT>' ) <EOL> self . file_format = details . get ( '<STR_LIT>' ) <EOL> self . virtual_size = details . get ( '<STR_LIT>' ) <EOL> self . cluster_size = details . get ( '<STR_LIT>' ) <EOL> self . disk_size = details . get ( '<STR_LIT>' ) <EOL> self . snapshots = details . get ( '<STR_LIT>' , [ ] ) <EOL> self . encryption = details . get ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> lines = [ <EOL> '<STR_LIT>' % self . image , <EOL> '<STR_LIT>' % self . file_format , <EOL> '<STR_LIT>' % self . virtual_size , <EOL> '<STR_LIT>' % self . disk_size , <EOL> '<STR_LIT>' % self . cluster_size , <EOL> '<STR_LIT>' % self . backing_file , <EOL> ] <EOL> if self . snapshots : <EOL> lines . append ( "<STR_LIT>" % self . snapshots ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <EOL> def _canonicalize ( self , field ) : <EOL> field = field . lower ( ) . strip ( ) <EOL> for c in ( "<STR_LIT:U+0020>" , "<STR_LIT:->" ) : <EOL> field = field . replace ( c , '<STR_LIT:_>' ) <EOL> return field <EOL> def _extract_bytes ( self , details ) : <EOL> real_size = self . SIZE_RE . search ( details ) <EOL> if real_size : <EOL> details = real_size . group ( <NUM_LIT:1> ) <EOL> try : <EOL> details = strutils . to_bytes ( details ) <EOL> except TypeError : <EOL> pass <EOL> return details <EOL> def _extract_details ( self , root_cmd , root_details , lines_after ) : <EOL> real_details = root_details <EOL> if root_cmd == '<STR_LIT>' : <EOL> backing_match = self . BACKING_FILE_RE . match ( root_details ) <EOL> if backing_match : <EOL> real_details = backing_match . group ( <NUM_LIT:2> ) . strip ( ) <EOL> elif root_cmd in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> real_details = self . _extract_bytes ( root_details ) <EOL> elif root_cmd == '<STR_LIT>' : <EOL> real_details = real_details . strip ( ) . lower ( ) <EOL> elif root_cmd == '<STR_LIT>' : <EOL> if not lines_after or not lines_after [ <NUM_LIT:0> ] . startswith ( "<STR_LIT>" ) : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise ValueError ( msg ) <EOL> del lines_after [ <NUM_LIT:0> ] <EOL> real_details = [ ] <EOL> while lines_after : <EOL> line = lines_after [ <NUM_LIT:0> ] <EOL> line_pieces = line . split ( ) <EOL> if len ( line_pieces ) != <NUM_LIT:6> : <EOL> break <EOL> date_pieces = line_pieces [ <NUM_LIT:5> ] . split ( "<STR_LIT::>" ) <EOL> if len ( date_pieces ) != <NUM_LIT:3> : <EOL> break <EOL> real_details . append ( { <EOL> '<STR_LIT:id>' : line_pieces [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : line_pieces [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : line_pieces [ <NUM_LIT:2> ] , <EOL> '<STR_LIT:date>' : line_pieces [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : line_pieces [ <NUM_LIT:4> ] + "<STR_LIT:U+0020>" + line_pieces [ <NUM_LIT:5> ] , <EOL> } ) <EOL> del lines_after [ <NUM_LIT:0> ] <EOL> return real_details <EOL> def _parse ( self , cmd_output ) : <EOL> contents = { } <EOL> lines = [ x for x in cmd_output . splitlines ( ) if x . strip ( ) ] <EOL> while lines : <EOL> line = lines . pop ( <NUM_LIT:0> ) <EOL> top_level = self . TOP_LEVEL_RE . match ( line ) <EOL> if top_level : <EOL> root = self . _canonicalize ( top_level . group ( <NUM_LIT:1> ) ) <EOL> if not root : <EOL> continue <EOL> root_details = top_level . group ( <NUM_LIT:2> ) . strip ( ) <EOL> details = self . _extract_details ( root , root_details , lines ) <EOL> contents [ root ] = details <EOL> return contents </s>
