
    1                     p    d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZ G d de      Z	 G d d	e      Z
y)
zGeneric debug tag accumulator.    )absolute_import)division)unicode_literalsNc                   F    e Zd ZdZd Zed        Zd Zd Zd Z	d Z
d
d	Zy)Taga  A debug tag object.

  Attributes:
    _name: The display name.
    _count: The number of times count() was called.
    _start: Most recent start() time in floating point seconds.
    _text: text() value.
    _intervals: The list or stop()-start() intervals in floating point seconds.
  c                 J    || _         d| _        d| _        d | _        g | _        y )Nr   )_name_count_start_text
_intervals)selfnames     3lib/googlecloudsdk/command_lib/interactive/debug.py__init__zTag.__init__'   s%    DJDKDKDJDO    c                 \    dddddd}|dk(  ryd	|vry
|j                  |d         |d   k7  S )zAReturns True if text "needs" quotes for pretty printing contents."'}]))r   r   {[( T Fr   )get)clstextpairss      r   needs_quoteszTag.needs_quotes.   sD     Cc#>Erz
$99T!Wb))r   c                    t        j                         }|j                  dj                  | j                               | j
                  r*|j                  dj                  | j
                               | j                  rLt        | j                        }|j                  dj                  |t        | j                        |z               | j                  _| j                  }t        |t        j                        r| j                  |      rd|z   dz   }|j                  dj                  |             |j                         S )z%Returns the tag/value display string.z{}z:{}z
:{}:{:.6f}r   )ioStringIOwriteformatr	   r
   r   lensumr   
isinstancesixstring_typesr#   getvalue)r   bufnr!   s       r   contentszTag.contents9   s    
++-CIIdkk$**%&{{	iiT[[)*
doo
a	ii##As4??';a'?@AzzZZd	D#**	+0A0A$0GTzC	iiT"#<<>r   c                 0    | xj                   dz  c_         | S )zIncrements the tag count.   )r
   r   s    r   countz	Tag.countI   s    KK1KKr   c                 8    t        j                          | _        | S )zStarts the tag timing interval.)timer   r4   s    r   startz	Tag.startN   s    ))+DKKr   c                 z    | j                   j                  t        j                         | j                  z
         | S )zStops the tag timing interval.)r   appendr7   r   r4   s    r   stopzTag.stopS   s(    OO499;45Kr   Nc                     || _         | S )z4Sets the tag text value, omit the text arg to unset.)r   )r   ts     r   r!   zTag.textX   s    DJKr   )N)__name__
__module____qualname____doc__r   classmethodr#   r1   r5   r8   r;   r!    r   r   r   r      s9     * * 


r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)Debuga  The controlling debug object.

  Debug "logger". Object tags (attributes) are created on the fly to preserve
  the feel of printf debugging.

  Usage:

    debug.foo.count().text(some_object)
    debug.bar.text('some state')
    debug.tag(some_string).count()
    debug.time.start()
    ...
    debug.time.stop()
    ...
    Display(debug.contents())

  Attributes:
    _changed: If _contents may have changed.
    _contents: The cached sorted list of tag/value strings.
    _tags: The list of debug tags.
  c                 .    d| _         g | _        i | _        y )NF)_changed	_contents_tagsr4   s    r   r   zDebug.__init__u   s    DMDNDJr   c                     	 | j                   |   }d| _        |S # t        $ r t        |      }|| j                   |<   Y .w xY w)z;Returns a tag by key name, creates tag in _tags on the fly.T)rI   KeyErrorr   rG   )r   keytags      r   rM   z	Debug.tagz   sI    JJsOc DMJ	  Hcdjjos    #A A c                 $    | j                  |      S )z>Called when __getattribute__ fails => creates tags on the fly.)rM   )r   rL   s     r   __getattr__zDebug.__getattr__   s    88C=r   c                     | j                   rUd| _         t        t        j                  | j                              D cg c]  \  }}|j                          c}}| _        | j                  S c c}}w )z5Returns the sorted list of tag/value display strings.F)rG   sortedr,   	iteritemsrI   r1   rH   )r   _rM   s      r   r1   zDebug.contents   s]    }}dm&,S]]4::-F&GI&GFAs &GIdn>>Is   A.N)r>   r?   r@   rA   r   rM   rO   r1   rC   r   r   rE   rE   ^   s    ,
r   rE   )rA   
__future__r   r   r   r%   r7   r,   objectr   rE   rC   r   r   <module>rV      s5     % &  ' 	  
?& ?D0F 0r   