Package web2py :: Package gluon :: Module debug |
[hide private]
[frames] | no frames]

# Source Code for Module web2py.gluon.debug

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3
     4  """
     5  This file is part of the web2py Web Framework
     6  Developed by Massimo Di Pierro <mdipierro@cs.depaul.edu>,
     7  limodou <limodou@gmail.com> and srackham <srackham@gmail.com>.
     8  License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
     9
    10  """
    11
    12  import logging
    13  import pdb
    14  import Queue
    15  import sys
    16
    17  logger = logging.getLogger("web2py")
    18
    19 -class Pipe(Queue.Queue):
    20 -    def __init__(self, name, mode='r', *args, **kwargs):
    21          self.__name = name
    22          Queue.Queue.__init__(self, *args, **kwargs)
    23
    24 -    def write(self, data):
    25          logger.debug("debug %s writting %s" % (self.__name, data))
    26          self.put(data)
    27
    28 -    def flush(self):
    29          # mark checkpoint (complete message)
    30          logger.debug("debug %s flushing..." % self.__name)
    31          self.put(None)
    32          # wait until it is processed
    33          self.join()
    34          logger.debug("debug %s flush done" % self.__name)
    35
    36 -    def read(self, count=None, timeout=None):
    37          logger.debug("debug %s reading..." % (self.__name, ))
    38          data = self.get(block=True, timeout=timeout)
    39          # signal that we are ready
    40          self.task_done()
    41          logger.debug("debug %s read %s" % (self.__name, data))
    42          return data
    43
    44 -    def readline(self):
    45          logger.debug("debug %s readline..." % (self.__name, ))
    46          return self.read()
    47
    48
    49  pipe_in = Pipe('in')
    50  pipe_out = Pipe('out')
    51
    52  debugger = pdb.Pdb(completekey=None, stdin=pipe_in, stdout=pipe_out,)
    53
    54 -def set_trace():
    55      "breakpoint shortcut (like pdb)"
    56      logger.info("DEBUG: set_trace!")
    57      debugger.set_trace(sys._getframe().f_back)
    58
    59
    60 -def stop_trace():
    61      "stop waiting for the debugger (called atexit)"
    62      # this should prevent communicate is wait forever a command result
    63      # and the main thread has finished
    64      logger.info("DEBUG: stop_trace!")
    65      pipe_out.write("debug finished!")
    66      pipe_out.write(None)
    67      #pipe_out.flush()
    68
    69 -def communicate(command=None):
    70      "send command to debbuger, wait result"
    71      if command is not None:
    72          logger.info("DEBUG: sending command %s" % command)
    73          pipe_in.write(command)
    74          #pipe_in.flush()
    75      result = []
    76      while True:
    77          data = pipe_out.read()
    78          if data is None:
    79              break
    80          result.append(data)
    81      logger.info("DEBUG: result %s" % repr(result))
    82      return ''.join(result)
    83