B
    һd!                 @   sr   d Z ddlZ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j	Z
d	d
 Zdd ZG dd de
ZdS )z?Debugger wrapper session that sends debug data to file:// URLs.    N)common)	frameworkc             C   sH   ddl m} |j|kr@t||}|j| |t |dd |jS |S dS )a  Publish traceback and source code if graph version is new.

  `graph.version` is compared with `old_graph_version`. If the former is higher
  (i.e., newer), the graph traceback and the associated source code is sent to
  the debug server at the specified gRPC URLs.

  Args:
    debug_server_urls: A single gRPC debug server URL as a `str` or a `list` of
      debug server URLs.
    graph: A Python `tf.Graph` object.
    feed_dict: Feed dictionary given to the `Session.run()` call.
    fetches: Fetches from the `Session.run()` call.
    old_graph_version: Old graph version to compare to.

  Returns:
    If `graph.version > old_graph_version`, the new graph version as an `int`.
    Else, the `old_graph_version` is returned.
  r   )source_remoteT)Zsend_sourceN)tensorflow.python.debug.libr   versionr   Zget_run_keyZsend_graph_tracebacks	tracebackextract_stack)Zdebug_server_urlsgraph	feed_dictfetchesZold_graph_versionr   Zrun_key r   _/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/debug/wrappers/grpc_wrapper.pypublish_traceback   s    
r   c               @   s*   e Zd ZdZd
ddZdd Zdd	 ZdS )GrpcDebugWrapperSessionz=Debug Session wrapper that send debug data to gRPC stream(s).NTc             C   s   |rt jj| |||d t|tr2| |g| _n\t|tr~g | _xJ|D ]0}t|tsftdt	| | j
| | qHW ntdt	| dS )a  Constructor of DumpingDebugWrapperSession.

    Args:
      sess: The TensorFlow `Session` object being wrapped.
      grpc_debug_server_addresses: (`str` or `list` of `str`) Single or a list
        of the gRPC debug server addresses, in the format of
        <host:port>, with or without the "grpc://" prefix. For example:
          "localhost:7000",
          ["localhost:7000", "192.168.0.2:8000"]
      watch_fn: (`Callable`) A Callable that can be used to define per-run
        debug ops and watched tensors. See the doc of
        `NonInteractiveDebugWrapperSession.__init__()` for details.
      thread_name_filter: Regular-expression white list for threads on which the
        wrapper session will be active. See doc of `BaseDebugWrapperSession` for
        more details.
      log_usage: (`bool`) whether the usage of this class is to be logged.

    Raises:
       TypeError: If `grpc_debug_server_addresses` is not a `str` or a `list`
         of `str`.
    )watch_fnthread_name_filterzGExpected type str in list grpc_debug_server_addresses, received type %szJExpected type str or list in grpc_debug_server_addresses, received type %sN)r   !NonInteractiveDebugWrapperSession__init__
isinstancestr_normalize_grpc_url_grpc_debug_server_urlslist	TypeErrortypeappend)selfsessgrpc_debug_server_addressesr   r   	log_usageaddressr   r   r   r   B   s     



z GrpcDebugWrapperSession.__init__c             C   s   | j S )a  Implementation of abstract method in superclass.

    See doc of `NonInteractiveDebugWrapperSession.prepare_run_debug_urls()`
    for details.

    Args:
      fetches: Same as the `fetches` argument to `Session.run()`
      feed_dict: Same as the `feed_dict` argument to `Session.run()`

    Returns:
      debug_urls: (`str` or `list` of `str`) file:// debug URLs to be used in
        this `Session.run()` call.
    )r   )r   r   r
   r   r   r   prepare_run_debug_urlst   s    z.GrpcDebugWrapperSession.prepare_run_debug_urlsc             C   s   | tjstj| S |S )N)
startswithr   ZGRPC_URL_PREFIX)r   r    r   r   r   r      s    z+GrpcDebugWrapperSession._normalize_grpc_url)NNT)__name__
__module____qualname____doc__r   r!   r   r   r   r   r   r   ?   s     
-r   c             C   s4   x.t d }|dkr"td q|dkrP qW d S )Nz'
SIGINT received. Quit program? (Y/n): ) Yyr   )Nn)inputstripsysexit)Zunused_signalZunused_frameresponser   r   r   _signal_handler   s    r1   c               C   s,   yt  t jt W n tk
r&   Y nX d S )N)signalSIGINTr1   
ValueErrorr   r   r   r   register_signal_handler   s    r5   c                   s0   e Zd ZdZd fdd	Zd	 fdd	Z  ZS )
TensorBoardDebugWrapperSessiona  A tfdbg Session wrapper that can be used with TensorBoard Debugger Plugin.

  This wrapper is the same as `GrpcDebugWrapperSession`, except that it uses a
    predefined `watch_fn` that
    1) uses `DebugIdentity` debug ops with the `gated_grpc` attribute set to
        `True` to allow the interactive enabling and disabling of tensor
       breakpoints.
    2) watches all tensors in the graph.
  This saves the need for the user to define a `watch_fn`.
  NTc                s4   dd }t  j|||||d || _d| _t  dS )au  Constructor of TensorBoardDebugWrapperSession.

    Args:
      sess: The `tf.compat.v1.Session` instance to be wrapped.
      grpc_debug_server_addresses: gRPC address(es) of debug server(s), as a
        `str` or a `list` of `str`s. E.g., "localhost:2333",
        "grpc://localhost:2333", ["192.168.0.7:2333", "192.168.0.8:2333"].
      thread_name_filter: Optional filter for thread names.
      send_traceback_and_source_code: Whether traceback of graph elements and
        the source code are to be sent to the debug server(s).
      log_usage: Whether the usage of this class is to be logged (if
        applicable).
    c             S   s   ~ ~t jdgdS )NzDebugIdentity(gated_grpc=true))Z	debug_ops)r   ZWatchOptions)r   Zfeedsr   r   r   _gated_grpc_watch_fn   s    zETensorBoardDebugWrapperSession.__init__.<locals>._gated_grpc_watch_fn)r   r   r   N)superr   _send_traceback_and_source_code_sent_graph_versionr5   )r   r   r   r   Zsend_traceback_and_source_coder   r7   )	__class__r   r   r      s    z'TensorBoardDebugWrapperSession.__init__c          	      s8   | j rt| j| j||| j| _t j|||||||dS )N)r
   optionsrun_metadatacallable_runnercallable_runner_argscallable_options)r:   r   r   r	   r;   r9   run)r   r   r
   r=   r>   r?   r@   rA   )r<   r   r   rB      s    
z"TensorBoardDebugWrapperSession.run)NTT)NNNNNN)r#   r$   r%   r&   r   rB   __classcell__r   r   )r<   r   r6      s   
  #     r6   )r&   r2   r.   r   r   r   Z tensorflow.python.debug.wrappersr   r   r   r   r1   r5   r6   r   r   r   r   <module>   s   &K	