diff options
| author | Ultrabug <ultrabug@gentoo.org> | 2013-08-29 13:03:29 +0200 |
|---|---|---|
| committer | Ultrabug <ultrabug@gentoo.org> | 2013-08-29 13:03:29 +0200 |
| commit | 59f36cf1433f0b5602d6d3f150afcaf18b70e2d3 (patch) | |
| tree | 055c7572fdfcc90be6698f4538f173d154a2b9c5 | |
| parent | e8c35a25f04316c622739bf527c4c53059b0e28f (diff) | |
make sure all examples are PEP8 compatible
| -rw-r--r-- | examples/glpi.py | 56 | ||||
| -rw-r--r-- | examples/pingdom.py | 75 |
2 files changed, 71 insertions, 60 deletions
diff --git a/examples/glpi.py b/examples/glpi.py index 30e148e..929749d 100644 --- a/examples/glpi.py +++ b/examples/glpi.py @@ -1,3 +1,7 @@ +# You need MySQL-python from http://pypi.python.org/pypi/MySQL-python +import MySQLdb + + class Py3status: """ This example class demonstrates how to display the current total number of @@ -10,7 +14,7 @@ class Py3status: py3status automagically. """ def count_glpi_open_tickets(self, json, i3status_config): - response = {'full_text' : '', 'name' : 'glpi_tickets'} + response = {'full_text': '', 'name': 'glpi_tickets'} # user-defined variables CRIT_THRESHOLD = 20 @@ -21,30 +25,28 @@ class Py3status: MYSQL_USER = '' POSITION = 0 - try: - # You need MySQL-python from http://pypi.python.org/pypi/MySQL-python - import MySQLdb + mydb = MySQLdb.connect( + host=MYSQL_HOST, + user=MYSQL_USER, + passwd=MYSQL_PASSWD, + db=MYSQL_DB, + connect_timeout=5, + ) + mycr = mydb.cursor() + mycr.execute('''select count(*) + from glpi_tickets + where closedate is NULL and solvedate is NULL;''') + row = mycr.fetchone() + if row: + open_tickets = int(row[0]) + if i3status_config['colors']: + if open_tickets > CRIT_THRESHOLD: + response.update({'color': i3status_config['color_bad']}) + elif open_tickets > WARN_THRESHOLD: + response.update( + {'color': i3status_config['color_degraded']} + ) + response['full_text'] = '%s tickets' % open_tickets + mydb.close() - mydb = MySQLdb.connect( - host=MYSQL_HOST, - user=MYSQL_USER, - passwd=MYSQL_PASSWD, - db=MYSQL_DB, - connect_timeout=5, - ) - mycr = mydb.cursor() - mycr.execute('select count(*) from glpi_tickets where closedate is NULL and solvedate is NULL;') - row = mycr.fetchone() - if row: - open_tickets = int(row[0]) - if i3status_config['colors']: - if open_tickets > CRIT_THRESHOLD: - response.update({'color': i3status_config['color_bad']}) - elif open_tickets > WARN_THRESHOLD: - response.update({'color': i3status_config['color_degraded']}) - response['full_text'] = '%s tickets' % open_tickets - mydb.close() - except Exception as e: - pass - finally: - return (POSITION, response) + return (POSITION, response) diff --git a/examples/pingdom.py b/examples/pingdom.py index 6a079eb..cddf1a9 100644 --- a/examples/pingdom.py +++ b/examples/pingdom.py @@ -1,5 +1,9 @@ # -*- coding: utf-8 -*- +import requests +from time import time + + class Py3status: """ Dynamically display the latest response time of the configured checks using @@ -11,40 +15,45 @@ class Py3status: https://pypi.python.org/pypi/requests """ def pingdom_checks(self, json, i3status_config): - response = {'full_text' : '', 'name' : 'pingdom_checks'} + response = {'full_text': '', 'name': 'pingdom_checks'} #NOTE: configure me ! - APP_KEY = '' # create an APP KEY on pingdom first - CACHE_TIMEOUT = 600 # update every 10 mins - CHECKS = [] # list of the checks' names you want added to your bar - LATENCY_THRESHOLD = 500 # when to colorize the output - LOGIN = '' # pingdom login - PASSWORD = '' # pingdom password - TIMEOUT = 5 + APP_KEY = '' # create an APP KEY on pingdom first + CACHE_TIMEOUT = 600 # recheck every 10 mins + CHECKS = [] # checks' names you want added to your bar + LATENCY_THRESHOLD = 500 # when to colorize the output + LOGIN = '' # pingdom login + PASSWORD = '' # pingdom password + TIMEOUT = 15 POSITION = 0 - try: - import requests - from time import time - r = requests.get( - 'https://api.pingdom.com/api/2.0/checks', - auth=(LOGIN, PASSWORD), - headers={'App-Key': APP_KEY}, - timeout=TIMEOUT, - ) - result = r.json() - if 'checks' in result: - for check in [ck for ck in result['checks'] if ck['name'] in CHECKS]: - if check['status'] == 'up': - response['full_text'] += '%s: %sms, ' % (check['name'], check['lastresponsetime']) - if check['lastresponsetime'] > LATENCY_THRESHOLD: - response.update({'color': i3status_config['color_degraded']}) - else: - response['full_text'] += '%s: DOWN' % (check['name'], check['lastresponsetime']) - response.update({'color': i3status_config['color_bad']}) - response['full_text'] = response['full_text'].strip(', ') - response['cached_until'] = time() + CACHE_TIMEOUT - except Exception as e: - pass - finally: - return (POSITION, response) + r = requests.get( + 'https://api.pingdom.com/api/2.0/checks', + auth=(LOGIN, PASSWORD), + headers={'App-Key': APP_KEY}, + timeout=TIMEOUT, + ) + result = r.json() + if 'checks' in result: + for check in [ + ck for ck in result['checks'] if ck['name'] in CHECKS + ]: + if check['status'] == 'up': + response['full_text'] += '{}: {}ms, '.format( + check['name'], + check['lastresponsetime'] + ) + if check['lastresponsetime'] > LATENCY_THRESHOLD: + response.update( + {'color': i3status_config['color_degraded']} + ) + else: + response['full_text'] += '{}: DOWN'.format( + check['name'], + check['lastresponsetime'] + ) + response.update({'color': i3status_config['color_bad']}) + response['full_text'] = response['full_text'].strip(', ') + response['cached_until'] = time() + CACHE_TIMEOUT + + return (POSITION, response) |
