Skip to content

Commit 95fd5fd

Browse files
DSpeichertolivielpeau
authored andcommitted
Python2 + Python3 compatibility (#33)
1 parent 74b1fac commit 95fd5fd

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

datadog_callback.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CallbackModule(CallbackBase):
1919
def __init__(self):
2020
if not HAS_MODULES:
2121
self.disabled = True
22-
print 'Datadog callback disabled.\nMake sure you call all required libraries: "datadog" and "yaml".'
22+
print('Datadog callback disabled.\nMake sure you call all required libraries: "datadog" and "yaml".')
2323
else:
2424
self.disabled = False
2525
# Set logger level - datadog api and urllib3
@@ -43,10 +43,10 @@ def _set_logger_level(self, name, level=logging.WARNING):
4343
log = logging.getLogger(name)
4444
log.setLevel(level)
4545
log.propagate = False
46-
except Exception, e:
46+
except Exception as e:
4747
# We don't want Ansible to fail on an API error
48-
print 'Couldn\'t get logger - %s' % name
49-
print e
48+
print("Couldn't get logger - %s" % name)
49+
print(e)
5050

5151
# Load parameters from conf file
5252
def _load_conf(self):
@@ -57,7 +57,7 @@ def _load_conf(self):
5757
with open(file_path, 'r') as conf_file:
5858
conf_dict = yaml.load(conf_file)
5959
else:
60-
print "Could not load configuration, invalid file: {}".format(file_path)
60+
print("Could not load configuration, invalid file: {}".format(file_path))
6161

6262
return os.environ.get('DATADOG_API_KEY', conf_dict.get('api_key', '')), conf_dict.get('url', 'https://app.datadoghq.com')
6363

@@ -80,10 +80,10 @@ def _send_event(self, title, alert_type=None, text=None, tags=None, host=None, e
8080
event_type=event_type,
8181
event_object=event_object,
8282
)
83-
except Exception, e:
83+
except Exception as e:
8484
# We don't want Ansible to fail on an API error
85-
print 'Couldn\'t send event "{0}" to Datadog'.format(title)
86-
print e
85+
print('Couldn\'t send event "{0}" to Datadog'.format(title))
86+
print(e)
8787

8888
# Send event, aggregated with other task-level events from the same host
8989
def send_task_event(self, title, alert_type='info', text='', tags=None, host=None):
@@ -124,10 +124,10 @@ def send_metric(self, metric, value, tags=None, host=None):
124124
tags=tags,
125125
host=host,
126126
)
127-
except Exception, e:
127+
except Exception as e:
128128
# We don't want Ansible to fail on an API error
129-
print 'Couldn\'t send metric "{0}" to Datadog'.format(metric)
130-
print e
129+
print('Couldn\'t send metric "{0}" to Datadog'.format(metric))
130+
print(e)
131131

132132
# Start timer to measure playbook running time
133133
def start_timer(self):
@@ -240,13 +240,13 @@ def v2_playbook_on_play_start(self, play):
240240
hostvars = self.play.get_variable_manager()._hostvars
241241

242242
if not hostvars:
243-
print "No api_key found in the config file and hostvars aren't set: disabling Datadog callback plugin"
243+
print("No api_key found in the config file and hostvars aren't set: disabling Datadog callback plugin")
244244
self.disabled = True
245245
else:
246246
try:
247247
api_key = hostvars['localhost']['datadog_api_key']
248-
except Exception, e:
249-
print 'No "api_key" found in the config file and {0} is not set in the hostvars: disabling Datadog callback plugin'.format(e)
248+
except Exception as e:
249+
print('No "api_key" found in the config file and {0} is not set in the hostvars: disabling Datadog callback plugin'.format(e))
250250
self.disabled = True
251251

252252
# Set up API client and send a start event
@@ -278,7 +278,7 @@ def playbook_on_stats(self, stats):
278278
total_errors += errors
279279

280280
# Send metrics for this host
281-
for metric, value in summary.iteritems():
281+
for metric, value in summary.items():
282282
self.send_metric('task.{0}'.format(metric), value, host=host)
283283

284284
# Send playbook elapsed time

0 commit comments

Comments
 (0)