Skip to content

Commit

Permalink
Updating pytoad library to accept additional debug info, and to no lo…
Browse files Browse the repository at this point in the history
…nger rely completely on an environment file.
  • Loading branch information
bcoe committed May 16, 2012
1 parent 6cc4180 commit 8cd8ac4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ def foo():
h2. Usage Without Decorator

bc. from pytoad import Connection
connection = Connection()
connection.send_to_hoptoad(Exception('banana'))
connection = Connection(name="Project Name", version="Software Version", environment_name="production", api_key="API-KEY")
connection.send_to_hoptoad(Exception('banana'), additional_information="additional debug info.")
44 changes: 31 additions & 13 deletions pytoad/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@
from xmlbuilder import XMLBuilder

class Connection(object):

def __init__(self, use_ssl=True):
self.use_ssl = use_ssl
self.environment = Environment()

INSTANCE_VARIABLES = ['use_ssl', 'api_key', 'environment_name', 'name', 'version', 'url']

def __init__(self, **kwargs):
self.use_ssl = True
self._load_instance_variables(kwargs)
self._load_environment_variables()
self.hoptoad_url = self._get_hoptoad_url()

def _load_instance_variables(self, kwargs):
for k, v in kwargs.items():
if k in self.INSTANCE_VARIABLES:
self.__dict__[k] = v

def _load_environment_variables(self):
self.environment = Environment()
for k in self.INSTANCE_VARIABLES:
if not self.__dict__.get(k, None) and self.environment.__dict__.get(k, None):
self.__dict__[k] = self.environment.__dict__[k]

def _get_hoptoad_url(self):
url_suffix = 'airbrake.io/notifier_api/v2/notices'
Expand All @@ -16,9 +30,9 @@ def _get_hoptoad_url(self):
else:
return "http://%s" % url_suffix

def send_to_hoptoad(self, exception):
def send_to_hoptoad(self, exception, additional_information=None):
headers = { 'Content-Type': 'text/xml' }
request = urllib2.Request(self.hoptoad_url, self._generate_xml(exception), headers)
request = urllib2.Request(self.hoptoad_url, self._generate_xml(exception, additional_information), headers)
response = urllib2.urlopen(request)
status = response.getcode()
if status == 200:
Expand All @@ -30,7 +44,7 @@ def send_to_hoptoad(self, exception):
if status == 500:
raise Exception("Hoptoad has hopped the toad")

def _generate_xml(self, exception):
def _generate_xml(self, exception, additional_information=None):
_,_,trace = sys.exc_info()

xml = XMLBuilder()
Expand All @@ -44,17 +58,21 @@ def _generate_xml(self, exception):
tb_dict['line_number'] = tb[1]
tb_dict['function_name'] = tb[2]

message = str(exception)
if additional_information:
message = 'error: %s, additional info: %s' % (message, additional_information)

with xml.notice(version = 2.0):
xml << ('api-key', self.environment.api_key)
xml << ('api-key', self.api_key)
with xml.notifier:
xml << ('name', self.environment.name)
xml << ('version', self.environment.version)
xml << ('url', self.environment.url)
xml << ('name', self.name)
xml << ('version', self.version)
xml << ('url', self.url)
with xml('server-environment'):
xml << ('environment-name', self.environment.environment_name)
xml << ('environment-name', self.environment_name)
with xml.error:
xml << ('class', exception.__class__.__name__)
xml << ('message', str(exception))
xml << ('message', message)
with xml.backtrace:
xml << ('line', {
'file':tb_dict.get('filename', 'unknown'),
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup, find_packages

setup(name="Pytoad",
version="1.0.5",
version="1.0.6",
description="Pytoad is a lightweight Hoptoad notifier for Python.",
author="Benjamin Coe",
author_email="ben@attachments.me",
Expand Down

0 comments on commit 8cd8ac4

Please sign in to comment.