/usr/lib/python2.7/dist-packages/oops/createhooks.py is in python-oops 0.0.10-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | # Copyright (c) 2010, 2011, Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, version 3 only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# GNU Lesser General Public License version 3 (see the file LICENSE).
"""Various hooks that can be used to populate OOPS reports.
The default_hooks list contains some innocuous hooks which most reporters will
want.
"""
__all__ = [
'attach_exc_info',
'attach_date',
'attach_hostname',
'copy_reporter',
'copy_topic',
'copy_url',
'default_hooks',
'safe_unicode',
]
__metaclass__ = type
import datetime
import socket
import traceback
from pytz import utc
# Used to detect missing keys.
_sentinel = object()
# (Prep for python3 - should be set conditional on version
strtypes = (basestring,)
def _simple_copy(key):
"""Curry a simple hook that copies a key from context to report."""
def copy_key(report, context):
value = context.get(key, _sentinel)
if value is not _sentinel:
report[key] = value
copy_key.__doc__ = (
"Copy the %s field from context to report, if present." % key)
return copy_key
copy_reporter = _simple_copy('reporter')
copy_topic = _simple_copy('topic')
copy_url = _simple_copy('url')
def safe_unicode(obj):
"""Used to reliably get *a* string for an object.
This is called on objects like exceptions, where bson won't be able to
serialize it, but a representation is needed for the report. It is
exposed a convenience for other on_create hook authors.
"""
if isinstance(obj, unicode):
return obj
# A call to str(obj) could raise anything at all.
# We'll ignore these errors, and print something
# useful instead, but also log the error.
# We disable the pylint warning for the blank except.
try:
value = unicode(obj)
except:
value = u'<unprintable %s object>' % (
unicode(type(obj).__name__))
# Some objects give back bytestrings to __unicode__...
if isinstance(value, str):
value = value.decode('latin-1')
return value
def attach_date(report, context):
"""Set the time key in report to a datetime of now."""
report['time'] = datetime.datetime.now(utc)
def attach_exc_info(report, context):
"""Attach exception info to the report.
This reads the 'exc_info' key from the context and sets the:
* type
* value
* tb_text
keys in the report.
exc_info must be a tuple, but it can contain either live exception
information or simple strings (allowing exceptions that have been
serialised and received over the network to be reported).
"""
info = context.get('exc_info')
if info is None:
return
report['type'] = getattr(info[0], '__name__', info[0])
report['value'] = safe_unicode(info[1])
if isinstance(info[2], strtypes):
tb_text = info[2]
else:
tb_text = u''.join(map(safe_unicode, traceback.format_tb(info[2])))
report['tb_text'] = tb_text
def attach_hostname(report, context):
"""Add the machine's hostname to report in the 'hostname' key."""
report['hostname'] = socket.gethostname()
# hooks that are installed into Config objects by default.
default_hooks = [
attach_exc_info,
attach_date,
copy_reporter,
copy_topic,
copy_url,
attach_hostname,
]
|