Add debug / test mechanism

This commit is contained in:
AKFish
2013-08-21 19:25:05 +08:00
parent 912bec79d2
commit 1d18ea75cc
2 changed files with 74 additions and 0 deletions
+11
View File
@@ -39,6 +39,12 @@ class GithubObject(object):
"""
Base class for all classes representing objects returned by the API.
"""
'''
A global debug flag to enable header validation by requester for all objects
'''
CHECK_AFTER_INIT_FLAG = False
def __init__(self, requester, headers, attributes, completed):
self._requester = requester
# Make sure headers are signed before any operations on attributes
@@ -47,6 +53,11 @@ class GithubObject(object):
self._initAttributes()
self._storeAndUseAttributes(attributes)
# Ask requester to do some checking, for debug and test purpose
# Since it's most handy to access and kinda all-knowing
if (GithubObject.CHECK_AFTER_INIT_FLAG):
requester.check_me(self);
def _storeAndUseAttributes(self, attributes):
self._useAttributes(attributes)
self._rawData = attributes
+63
View File
@@ -11,6 +11,7 @@
# Copyright 2012 Zearin <zearin@gonk.net> #
# Copyright 2013 Jonathan J Hunt <hunt@braincorporation.com> #
# Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net> #
# Copyright 2013 akfish <akfish@gmail.com> #
# #
# This file is part of PyGithub. http://jacquev6.github.com/PyGithub/ #
# #
@@ -60,8 +61,65 @@ class Requester:
def resetConnectionClasses(cls):
cls.__httpConnectionClass = httplib.HTTPConnection
cls.__httpsConnectionClass = httplib.HTTPSConnection
#############################################################
# For Debug
DEBUG_FLAG = False
DEBUG_FRAME_BUFFER_SIZE = 1024
DEBUG_HEADER_KEY = "DEBUG_FRAME"
def NEW_DEBUG_FRAME(self, requestHeader):
'''
Initialize a debug frame with requestHeader
Frame count is updated and will be attached to respond header
The structure of a frame: [requestHeader, statusCode, responseHeader, raw_data]
Some of them may be None
'''
if not self.DEBUG_FLAG:
return
new_frame = [requestHeader, None, None, None]
if self._frameCount < self.DEBUG_FRAME_BUFFER_SIZE - 1:
self._frameBuffer.append(new_frame)
else:
self._frameBuffer[0] = new_frame
self._frameCount = len(self._frameBuffer) - 1
def DEBUG_ON_RESPONSE(self, statusCode, responseHeader, data):
'''
Update current frame with response
Current frame index will be attached to responseHeader
'''
if not self.DEBUG_FLAG:
return
self._frameBuffer[self._frameCount][1:4] = [statusCode, responseHeader, data]
responseHeader[self.DEBUG_HEADER_KEY] = self._frameCount
def check_me(self, obj):
if self.DEBUG_FLAG and self.onCheckMe is not None:
frame = None
if self.DEBUG_HEADER_KEY in obj._headers:
frame_index = obj._headers[self.DEBUG_HEADER_KEY]
frame = self._frameBuffer[frame_index]
onCheckMe(self, obj, frame)
def _initializeDebugFeature(self):
if not self.DEBUG_FLAG:
return
self.onCheckMe = None
self._frameCount = 0
self._frameBuffer = []
#############################################################
def __init__(self, login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page):
self._initializeDebugFeature()
if password is not None:
login = login_or_token
if atLeastPython3:
@@ -108,6 +166,9 @@ class Requester:
def __check(self, status, responseHeaders, output):
output = self.__structuredFromJson(output)
# Log frame
self.DEBUG_ON_RESPONSE(status, responseHeaders, output)
if status >= 400:
raise self.__createException(status, output)
return responseHeaders, output
@@ -171,6 +232,8 @@ class Requester:
if input is not None:
requestHeaders["Content-Type"], encoded_input = encode(input)
self.NEW_DEBUG_FRAME(requestHeaders)
status, responseHeaders, output = self.__requestRaw(verb, url, requestHeaders, encoded_input)
if "x-ratelimit-remaining" in responseHeaders and "x-ratelimit-limit" in responseHeaders: