Permalink
Browse files

Merge branch 'cdman/use-logging-module'

fixes #42
  • Loading branch information...
2 parents fab30b0 + 95aa2a9 commit 7d65da59879e392678b75f14756a14cba87fde53 @willnorris willnorris committed Aug 24, 2012
@@ -189,6 +189,7 @@
import cgi
import copy
+import logging
from urlparse import urlparse, urldefrag
from openid import fetchers
@@ -662,7 +663,7 @@ def _checkReturnTo(self, message, return_to):
try:
self._verifyReturnToArgs(message.toPostArgs())
except ProtocolError, why:
- oidutil.log("Verifying return_to arguments: %s" % (why[0],))
+ logging.exception("Verifying return_to arguments: %s" % (why[0],))
return False
# Check the return_to base URL against the one in the message.
@@ -728,7 +729,7 @@ def _doIdRes(self, message, endpoint, return_to):
# Verify discovery information:
endpoint = self._verifyDiscoveryResults(message, endpoint)
- oidutil.log("Received id_res response from %s using association %s" %
+ logging.info("Received id_res response from %s using association %s" %
(endpoint.server_url,
message.getArg(OPENID_NS, 'assoc_handle')))
@@ -922,7 +923,7 @@ def _verifyDiscoveryResultsOpenID2(self, resp_msg, endpoint):
# endpoints and responses that didn't match the original
# request.
if not endpoint:
- oidutil.log('No pre-discovered information supplied.')
+ logging.info('No pre-discovered information supplied.')
endpoint = self._discoverAndVerify(to_match.claimed_id, [to_match])
else:
# The claimed ID matches, so we use the endpoint that we
@@ -931,10 +932,10 @@ def _verifyDiscoveryResultsOpenID2(self, resp_msg, endpoint):
try:
self._verifyDiscoverySingle(endpoint, to_match)
except ProtocolError, e:
- oidutil.log(
+ logging.exception(
"Error attempting to use stored discovery information: " +
str(e))
- oidutil.log("Attempting discovery to verify endpoint")
+ logging.info("Attempting discovery to verify endpoint")
endpoint = self._discoverAndVerify(
to_match.claimed_id, [to_match])
@@ -976,9 +977,9 @@ def _verifyDiscoveryResultsOpenID1(self, resp_msg, endpoint):
except TypeURIMismatch:
self._verifyDiscoverySingle(endpoint, to_match_1_0)
except ProtocolError, e:
- oidutil.log("Error attempting to use stored discovery information: " +
+ logging.exception("Error attempting to use stored discovery information: " +
str(e))
- oidutil.log("Attempting discovery to verify endpoint")
+ logging.info("Attempting discovery to verify endpoint")
else:
return endpoint
@@ -1048,7 +1049,7 @@ def _discoverAndVerify(self, claimed_id, to_match_endpoints):
@raises DiscoveryFailure: when discovery fails.
"""
- oidutil.log('Performing discovery on %s' % (claimed_id,))
+ logging.info('Performing discovery on %s' % (claimed_id,))
_, services = self._discover(claimed_id)
if not services:
raise DiscoveryFailure('No OpenID information found at %s' %
@@ -1075,10 +1076,10 @@ def _verifyDiscoveredServices(self, claimed_id, services, to_match_endpoints):
# succeeded. Return this endpoint.
return endpoint
else:
- oidutil.log('Discovery verification failure for %s' %
+ logging.error('Discovery verification failure for %s' %
(claimed_id,))
for failure_message in failure_messages:
- oidutil.log(' * Endpoint mismatch: ' + failure_message)
+ logging.error(' * Endpoint mismatch: ' + failure_message)
raise DiscoveryFailure(
'No matching endpoint found after discovering %s'
@@ -1090,14 +1091,14 @@ def _checkAuth(self, message, server_url):
@returns: True if the request is valid.
@rtype: bool
"""
- oidutil.log('Using OpenID check_authentication')
+ logging.info('Using OpenID check_authentication')
request = self._createCheckAuthRequest(message)
if request is None:
return False
try:
response = self._makeKVPost(request, server_url)
except (fetchers.HTTPFetchingError, ServerError), e:
- oidutil.log('check_authentication failed: %s' % (e[0],))
+ logging.exception('check_authentication failed: %s' % (e[0],))
return False
else:
return self._processCheckAuthResponse(response, server_url)
@@ -1109,12 +1110,12 @@ def _createCheckAuthRequest(self, message):
signed = message.getArg(OPENID_NS, 'signed')
if signed:
for k in signed.split(','):
- oidutil.log(k)
+ logging.info(k)
val = message.getAliasedArg(k)
# Signed value is missing
if val is None:
- oidutil.log('Missing signed field %r' % (k,))
+ logging.info('Missing signed field %r' % (k,))
return None
check_auth_message = message.copy()
@@ -1129,18 +1130,18 @@ def _processCheckAuthResponse(self, response, server_url):
invalidate_handle = response.getArg(OPENID_NS, 'invalidate_handle')
if invalidate_handle is not None:
- oidutil.log(
+ logging.info(
'Received "invalidate_handle" from server %s' % (server_url,))
if self.store is None:
- oidutil.log('Unexpectedly got invalidate_handle without '
+ logging.error('Unexpectedly got invalidate_handle without '
'a store!')
else:
self.store.removeAssociation(server_url, invalidate_handle)
if is_valid == 'true':
return True
else:
- oidutil.log('Server responds that checkAuth call is not valid')
+ logging.error('Server responds that checkAuth call is not valid')
return False
def _getAssociation(self, endpoint):
@@ -1193,7 +1194,7 @@ def _negotiateAssociation(self, endpoint):
except ServerError, why:
# Do not keep trying, since it rejected the
# association type that it told us to use.
- oidutil.log('Server %s refused its suggested association '
+ logging.error('Server %s refused its suggested association '
'type: session_type=%s, assoc_type=%s'
% (endpoint.server_url, session_type,
assoc_type))
@@ -1216,15 +1217,15 @@ def _extractSupportedAssociationType(self, server_error, endpoint,
# should be considered a total failure.
if server_error.error_code != 'unsupported-type' or \
server_error.message.isOpenID1():
- oidutil.log(
+ logging.error(
'Server error when requesting an association from %r: %s'
% (endpoint.server_url, server_error.error_text))
return None
# The server didn't like the association/session type
# that we sent, and it sent us back a message that
# might tell us how to handle it.
- oidutil.log(
+ logging.error(
'Unsupported association type %s: %s' % (assoc_type,
server_error.error_text,))
@@ -1234,13 +1235,13 @@ def _extractSupportedAssociationType(self, server_error, endpoint,
session_type = server_error.message.getArg(OPENID_NS, 'session_type')
if assoc_type is None or session_type is None:
- oidutil.log('Server responded with unsupported association '
+ logging.error('Server responded with unsupported association '
'session but did not supply a fallback.')
return None
elif not self.negotiator.isAllowed(assoc_type, session_type):
fmt = ('Server sent unsupported session/association type: '
'session_type=%s, assoc_type=%s')
- oidutil.log(fmt % (session_type, assoc_type))
+ logging.error(fmt % (session_type, assoc_type))
return None
else:
return assoc_type, session_type
@@ -1261,17 +1262,17 @@ def _requestAssociation(self, endpoint, assoc_type, session_type):
try:
response = self._makeKVPost(args, endpoint.server_url)
except fetchers.HTTPFetchingError, why:
- oidutil.log('openid.associate request failed: %s' % (why[0],))
+ logging.exception('openid.associate request failed: %s' % (why[0],))
return None
try:
assoc = self._extractAssociation(response, assoc_session)
except KeyError, why:
- oidutil.log('Missing required parameter in response from %s: %s'
+ logging.exception('Missing required parameter in response from %s: %s'
% (endpoint.server_url, why[0]))
return None
except ProtocolError, why:
- oidutil.log('Protocol error parsing response from %s: %s' % (
+ logging.exception('Protocol error parsing response from %s: %s' % (
endpoint.server_url, why[0]))
return None
else:
@@ -1348,7 +1349,7 @@ def _getOpenID1SessionType(self, assoc_response):
# OpenID 1, but we'll accept it anyway, while issuing a
# warning.
if session_type == 'no-encryption':
- oidutil.log('WARNING: OpenID server sent "no-encryption"'
+ logging.warn('OpenID server sent "no-encryption"'
'for OpenID 1.X')
# Missing or empty session type is the way to flag a
@@ -1599,7 +1600,7 @@ def getMessage(self, realm, return_to=None, immediate=False):
else:
assoc_log_msg = 'using stateless mode.'
- oidutil.log("Generated %s request to %s %s" %
+ logging.info("Generated %s request to %s %s" %
(mode, self.endpoint.server_url, assoc_log_msg))
return message
@@ -1779,7 +1780,7 @@ def getSignedNS(self, ns_uri):
for key in msg_args.iterkeys():
if not self.isSigned(ns_uri, key):
- oidutil.log("SuccessResponse.getSignedNS: (%s, %s) not signed."
+ logging.info("SuccessResponse.getSignedNS: (%s, %s) not signed."
% (ns_uri, key))
return None
@@ -14,8 +14,9 @@
]
import urlparse
+import logging
-from openid import oidutil, fetchers, urinorm
+from openid import fetchers, urinorm
from openid import yadis
from openid.yadis.etxrd import nsTag, XRDSError, XRD_NS_2_0
@@ -425,7 +426,7 @@ def discoverXRI(iname):
for service_element in services:
endpoints.extend(flt.getServiceEndpoints(iname, service_element))
except XRDSError:
- oidutil.log('xrds error on ' + iname)
+ logging.exception('xrds error on ' + iname)
for endpoint in endpoints:
# Is there a way to pass this through the filter to the endpoint
@@ -38,7 +38,7 @@
from openid.message import registerNamespaceAlias, \
NamespaceAliasRegistrationError
from openid.extension import Extension
-from openid import oidutil
+import logging
try:
basestring #pylint:disable-msg=W0104
@@ -95,7 +95,7 @@ def checkFieldName(field_name):
try:
registerNamespaceAlias(ns_uri_1_1, 'sreg')
except NamespaceAliasRegistrationError, e:
- oidutil.log('registerNamespaceAlias(%r, %r) failed: %s' % (ns_uri_1_1,
+ logging.exception('registerNamespaceAlias(%r, %r) failed: %s' % (ns_uri_1_1,
'sreg', str(e),))
def supportsSReg(endpoint):
View
@@ -1,8 +1,7 @@
__all__ = ['seqToKV', 'kvToSeq', 'dictToKV', 'kvToDict']
-from openid import oidutil
-
import types
+import logging
class KVFormError(ValueError):
pass
@@ -22,7 +21,7 @@ def err(msg):
if strict:
raise KVFormError(formatted)
else:
- oidutil.log(formatted)
+ logging.warn(formatted)
lines = []
for k, v in seq:
@@ -73,7 +72,7 @@ def err(msg):
if strict:
raise KVFormError(formatted)
else:
- oidutil.log(formatted)
+ logging.warn(formatted)
lines = data.split('\n')
if lines[-1]:
View
@@ -10,6 +10,7 @@
import binascii
import sys
import urlparse
+import logging
from urllib import urlencode
@@ -77,9 +78,8 @@ def importElementTree(module_names=None):
except (SystemExit, MemoryError, AssertionError):
raise
except:
- why = sys.exc_info()[1]
- log('Not using ElementTree library %r because it failed to '
- 'parse a trivial document: %s' % (mod_name, why))
+ logging.exception('Not using ElementTree library %r because it failed to '
+ 'parse a trivial document: %s' % mod_name)
else:
return ElementTree
else:
@@ -91,20 +91,8 @@ def importElementTree(module_names=None):
def log(message, level=0):
"""Handle a log message from the OpenID library.
- This implementation writes the string it to C{sys.stderr},
- followed by a newline.
-
- Currently, the library does not use the second parameter to this
- function, but that may change in the future.
-
- To install your own logging hook::
-
- from openid import oidutil
-
- def myLoggingFunction(message, level):
- ...
-
- oidutil.log = myLoggingFunction
+ This is a legacy function which redirects to logging.error.
+ The logging module should be used instead of this
@param message: A string containing a debugging message from the
OpenID library
@@ -118,8 +106,8 @@ def myLoggingFunction(message, level):
@returns: Nothing.
"""
- sys.stderr.write(message)
- sys.stderr.write('\n')
+ logging.error("This is a legacy log message, please use the "
+ "logging module. Message: %s", message)
def appendArgs(url, args):
"""Append query arguments to a HTTP(s) URL. If the URL already has
@@ -117,6 +117,7 @@
"""
import time, warnings
+import logging
from copy import deepcopy
from openid import cryptutil
@@ -420,7 +421,7 @@ def fromMessage(klass, message, op_endpoint=UNUSED):
if message.isOpenID1():
session_type = message.getArg(OPENID_NS, 'session_type')
if session_type == 'no-encryption':
- oidutil.log('Received OpenID 1 request with a no-encryption '
+ logging.warn('Received OpenID 1 request with a no-encryption '
'assocaition session type. Continuing anyway.')
elif not session_type:
session_type = 'no-encryption'
@@ -1170,15 +1171,15 @@ def verify(self, assoc_handle, message):
"""
assoc = self.getAssociation(assoc_handle, dumb=True)
if not assoc:
- oidutil.log("failed to get assoc with handle %r to verify "
+ logging.error("failed to get assoc with handle %r to verify "
"message %r"
% (assoc_handle, message))
return False
try:
valid = assoc.checkMessageSignature(message)
except ValueError, ex:
- oidutil.log("Error in verifying %s with %s: %s" % (message,
+ logging.exception("Error in verifying %s with %s: %s" % (message,
assoc,
ex))
return False
@@ -1285,7 +1286,7 @@ def getAssociation(self, assoc_handle, dumb, checkExpiration=True):
key = self._normal_key
assoc = self.store.getAssociation(key, assoc_handle)
if assoc is not None and assoc.expiresIn <= 0:
- oidutil.log("requested %sdumb key %r is expired (by %s seconds)" %
+ logging.info("requested %sdumb key %r is expired (by %s seconds)" %
((not dumb) and 'not-' or '',
assoc_handle, assoc.expiresIn))
if checkExpiration:
Oops, something went wrong.

0 comments on commit 7d65da5

Please sign in to comment.