1
0

debug.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # File: debug.py
  2. # Library: DOPAL - DO Python Azureus Library
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; version 2 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details ( see the COPYING file ).
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. '''
  17. Contains functions and objects useful for debugging DOPAL.
  18. '''
  19. class DebugObject(object):
  20. pass
  21. class LinkDebugObject(DebugObject):
  22. pass
  23. class ErrorLinkDebug(LinkDebugObject):
  24. def __init__(self, cgi_path, error):
  25. self.cgi_path = cgi_path
  26. self.error = error
  27. class OutgoingExchangeDebug(LinkDebugObject):
  28. def __init__(self, cgi_path, data_to_send):
  29. self.cgi_path = cgi_path
  30. self.data_to_send = data_to_send
  31. class ConnectionExchangeDebug(LinkDebugObject):
  32. def __init__(self, cgi_path, data_sent, data_received):
  33. self.cgi_path = cgi_path
  34. self.data_sent = data_sent
  35. self.data_received = data_received
  36. class ConnectionDebugObject(DebugObject):
  37. pass
  38. class MethodRequestDebug(ConnectionDebugObject):
  39. def __init__(self, object_id, request_method):
  40. self.object_id = object_id
  41. self.request_method = request_method
  42. class MethodResponseDebug(ConnectionDebugObject):
  43. def __init__(self, object_id, request_method, response):
  44. self.object_id = object_id
  45. self.request_method = request_method
  46. self.response = response
  47. def print_everything(debug_object):
  48. if not isinstance(debug_object, LinkDebugObject):
  49. return
  50. print
  51. print '---------------'
  52. print
  53. if isinstance(debug_object, OutgoingExchangeDebug):
  54. print 'Sending to "%s"' % debug_object.cgi_path
  55. print
  56. print debug_object.data_to_send
  57. elif isinstance(debug_object, ConnectionExchangeDebug):
  58. print 'Recieved from "%s"' % debug_object.cgi_path
  59. print
  60. print debug_object.data_received
  61. elif isinstance(debug_object, ErrorLinkDebug):
  62. error = debug_object.error
  63. print 'Error from "%s"' % debug_object.cgi_path
  64. print
  65. print '%s: %s' % (error.__class__.__name__, error)
  66. print
  67. print '---------------'
  68. print
  69. def print_everything_with_stack(debug_object):
  70. if isinstance(debug_object, OutgoingExchangeDebug):
  71. import traceback
  72. print
  73. print '---------------'
  74. print
  75. print 'Stack trace of request:'
  76. traceback.print_stack()
  77. print
  78. print '---------------'
  79. print
  80. print_everything(debug_object)
  81. def print_method(debug_object):
  82. from dopal.utils import make_short_object_id as _sid
  83. if isinstance(debug_object, MethodRequestDebug):
  84. print
  85. print '---------------'
  86. print ' Object:', debug_object.object_id,
  87. if debug_object.object_id is not None:
  88. print "[sid=%s]" % _sid(debug_object.object_id),
  89. print
  90. print ' Method:', debug_object.request_method
  91. print
  92. elif isinstance(debug_object, MethodResponseDebug):
  93. import dopal.core
  94. if isinstance(debug_object.response, dopal.core.ErrorResponse):
  95. print ' Response Type: ERROR'
  96. print ' Response Data:', debug_object.response.response_data
  97. elif isinstance(debug_object.response, dopal.core.AtomicResponse):
  98. print ' Response Type: VALUE'
  99. print ' Response Data:', debug_object.response.response_data
  100. elif isinstance(debug_object.response, dopal.core.NullResponse):
  101. print ' Response Type: NULL / EMPTY'
  102. print ' Response Data: None'
  103. elif isinstance(debug_object.response, dopal.core.StructuredResponse):
  104. print ' Response Type: STRUCTURE'
  105. print ' Response Data:',
  106. obj_id = debug_object.response.get_object_id()
  107. if obj_id is not None:
  108. print 'Object [id=%s, sid=%s]' % (obj_id, _sid(obj_id))
  109. else:
  110. print 'Non-object value'
  111. print '---------------'
  112. print
  113. def print_method_with_stack(debug_object):
  114. if isinstance(debug_object, MethodResponseDebug):
  115. import traceback
  116. print
  117. print '---------------'
  118. print
  119. print 'Stack trace of request:'
  120. traceback.print_stack()
  121. print
  122. print '---------------'
  123. print
  124. print_method(debug_object)
  125. class DebugGrabber(object):
  126. debug_object = None
  127. def get_in(self):
  128. if self.debug_object is None:
  129. raise Exception, "not captured any data yet"
  130. return self.debug_object.data_sent
  131. def get_out(self):
  132. if self.debug_object is None:
  133. raise Exception, "not captured any data yet"
  134. return self.debug_object.data_received
  135. def __call__(self, debug_object):
  136. if isinstance(debug_object, ConnectionExchangeDebug):
  137. self.debug_object = debug_object