From 0adf9e189fd73080f4458f8594ebc9daab33d9fe Mon Sep 17 00:00:00 2001 From: Anthony Brodard Date: Wed, 29 Apr 2015 15:41:37 +0200 Subject: Add aws_bill module --- py3status/modules/aws_bill.py | 96 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 py3status/modules/aws_bill.py (limited to 'py3status') diff --git a/py3status/modules/aws_bill.py b/py3status/modules/aws_bill.py new file mode 100755 index 0000000..6336da4 --- /dev/null +++ b/py3status/modules/aws_bill.py @@ -0,0 +1,96 @@ +# -*- coding: utf-8 -*- +""" +Display the current AWS bill. + +##### WARNING ##### +This module generate some costs on the AWS bill. Take care about the cache_timout to limit these fees ! +##### WARNING ##### + +Configuration parameters: + - aws_access_key_id : Your AWS access key + - aws_secret_access_key : Your AWS secret key + - aws_account_id : the root ID of the AWS account. Can be find here https://console.aws.amazon.com/billing/home#/account + - s3_bucket_name : the bucket where billing files are sent by AWS. Follow this article to activate this feature : http://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/detailed-billing-reports.html + - billing_file : where the billing file must be stored + - cache_timeout : how often we refresh this module in seconds + +@author nawadanp +""" + +import csv +import datetime +import boto + +from boto.s3.connection import Key +from time import time + +class Py3status: + + # available configuration parameters + aws_access_key_id = '' + aws_secret_access_key = '' + aws_account_id = '' + s3_bucket_name = '' + billing_file = '/tmp/.aws_billing.csv' + cache_timeout = 3600 + + def _get_bill_amount(self): + # Billing file name, generated by Amazon itself + # Format : 123456789012-aws-billing-csv-yyyy-mm.csv + s3_file_key = self.aws_account_id + '-aws-billing-csv-' + datetime.datetime.now().strftime("%Y") + '-' + datetime.datetime.now().strftime("%m") + '.csv' + i = 0 + + # Connect to the bucket + conn = boto.connect_s3(self.aws_access_key_id, self.aws_secret_access_key) + bucket = conn.get_bucket(self.s3_bucket_name) + + # Fetch the objects keys and get the billing file + k = Key(bucket) + k.key = s3_file_key + try: + k.get_contents_to_filename(self.billing_file) + except: + return False + k.close + + # Parse the file and get the InvoiceTotal amount + with open(self.billing_file,'rb') as f: + reader = csv.reader(f) + for row in reader: + if ''.join(row).find('InvoiceTotal') == -1: + continue + i = i+1 + return row[-1] + + + def aws_bill(self, i3s_output_list, i3s_config): + response = { + 'cached_until': time() + self.cache_timeout, + 'full_text': '' + } + + bill_amount = self._get_bill_amount() + + if bill_amount: + response['full_text'] = str(bill_amount) + "$" + response['color'] = i3s_config['color_good'] + else: + response['full_text'] = "Billing file not found in the bucket" + response['color'] = i3s_config['color_bad'] + + return response + + +if __name__ == "__main__": + """ + Test this module by calling it directly. + """ + from time import sleep + x = Py3status() + config = { + 'color_good': '#00FF00', + 'color_bad': '#FF0000', + } + while True: + print(x.aws_bill([], config)) + sleep(1) -- cgit v1.3 From 5bc8eea74fd7b54be5a2d473728648b8bb775d09 Mon Sep 17 00:00:00 2001 From: Anthony Brodard Date: Wed, 29 Apr 2015 15:46:52 +0200 Subject: Fix PEP8 compliance --- py3status/modules/aws_bill.py | 133 +++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 65 deletions(-) (limited to 'py3status') diff --git a/py3status/modules/aws_bill.py b/py3status/modules/aws_bill.py index 6336da4..d10af4b 100755 --- a/py3status/modules/aws_bill.py +++ b/py3status/modules/aws_bill.py @@ -24,73 +24,76 @@ import boto from boto.s3.connection import Key from time import time + class Py3status: - # available configuration parameters - aws_access_key_id = '' - aws_secret_access_key = '' - aws_account_id = '' - s3_bucket_name = '' - billing_file = '/tmp/.aws_billing.csv' - cache_timeout = 3600 - - def _get_bill_amount(self): - # Billing file name, generated by Amazon itself - # Format : 123456789012-aws-billing-csv-yyyy-mm.csv - s3_file_key = self.aws_account_id + '-aws-billing-csv-' + datetime.datetime.now().strftime("%Y") + '-' + datetime.datetime.now().strftime("%m") + '.csv' - i = 0 - - # Connect to the bucket - conn = boto.connect_s3(self.aws_access_key_id, self.aws_secret_access_key) - bucket = conn.get_bucket(self.s3_bucket_name) - - # Fetch the objects keys and get the billing file - k = Key(bucket) - k.key = s3_file_key - try: - k.get_contents_to_filename(self.billing_file) - except: - return False - k.close - - # Parse the file and get the InvoiceTotal amount - with open(self.billing_file,'rb') as f: - reader = csv.reader(f) - for row in reader: - if ''.join(row).find('InvoiceTotal') == -1: - continue - i = i+1 - return row[-1] - - - def aws_bill(self, i3s_output_list, i3s_config): - response = { - 'cached_until': time() + self.cache_timeout, - 'full_text': '' - } - - bill_amount = self._get_bill_amount() - - if bill_amount: - response['full_text'] = str(bill_amount) + "$" - response['color'] = i3s_config['color_good'] - else: - response['full_text'] = "Billing file not found in the bucket" - response['color'] = i3s_config['color_bad'] - - return response + # available configuration parameters + aws_access_key_id = '' + aws_secret_access_key = '' + aws_account_id = '' + s3_bucket_name = '' + billing_file = '/tmp/.aws_billing.csv' + cache_timeout = 3600 + + def _get_bill_amount(self): + # Billing file name, generated by Amazon itself + # Format : 123456789012-aws-billing-csv-yyyy-mm.csv + s3_file_key = self.aws_account_id + '-aws-billing-csv-' + \ + datetime.datetime.now().strftime( + "%Y") + '-' + datetime.datetime.now().strftime("%m") + '.csv' + i = 0 + + # Connect to the bucket + conn = boto.connect_s3( + self.aws_access_key_id, self.aws_secret_access_key) + bucket = conn.get_bucket(self.s3_bucket_name) + + # Fetch the objects keys and get the billing file + k = Key(bucket) + k.key = s3_file_key + try: + k.get_contents_to_filename(self.billing_file) + except: + return False + k.close + + # Parse the file and get the InvoiceTotal amount + with open(self.billing_file, 'rb') as f: + reader = csv.reader(f) + for row in reader: + if ''.join(row).find('InvoiceTotal') == -1: + continue + i = i + 1 + return row[-1] + + def aws_bill(self, i3s_output_list, i3s_config): + response = { + 'cached_until': time() + self.cache_timeout, + 'full_text': '' + } + + bill_amount = self._get_bill_amount() + + if bill_amount: + response['full_text'] = str(bill_amount) + "$" + response['color'] = i3s_config['color_good'] + else: + response['full_text'] = "Billing file not found in the bucket" + response['color'] = i3s_config['color_bad'] + + return response if __name__ == "__main__": - """ - Test this module by calling it directly. - """ - from time import sleep - x = Py3status() - config = { - 'color_good': '#00FF00', - 'color_bad': '#FF0000', - } - while True: - print(x.aws_bill([], config)) - sleep(1) + """ + Test this module by calling it directly. + """ + from time import sleep + x = Py3status() + config = { + 'color_good': '#00FF00', + 'color_bad': '#FF0000', + } + while True: + print(x.aws_bill([], config)) + sleep(1) -- cgit v1.3 From 30cc4aef4729a65ca9b72669572cca30672cdf41 Mon Sep 17 00:00:00 2001 From: Anthony Brodard Date: Wed, 29 Apr 2015 17:14:38 +0200 Subject: Add exceptions --- py3status/modules/aws_bill.py | 59 +++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 19 deletions(-) (limited to 'py3status') diff --git a/py3status/modules/aws_bill.py b/py3status/modules/aws_bill.py index d10af4b..6d5c036 100755 --- a/py3status/modules/aws_bill.py +++ b/py3status/modules/aws_bill.py @@ -40,46 +40,67 @@ class Py3status: # Format : 123456789012-aws-billing-csv-yyyy-mm.csv s3_file_key = self.aws_account_id + '-aws-billing-csv-' + \ datetime.datetime.now().strftime( - "%Y") + '-' + datetime.datetime.now().strftime("%m") + '.csv' + '%Y') + '-' + datetime.datetime.now().strftime('%m') + '.csv' i = 0 - # Connect to the bucket - conn = boto.connect_s3( - self.aws_access_key_id, self.aws_secret_access_key) - bucket = conn.get_bucket(self.s3_bucket_name) + # Connection to s3 service + try: + conn = boto.connect_s3( + self.aws_access_key_id, self.aws_secret_access_key) + except: + return 'conn_error' + + # Connection to the bucket + try: + bucket = conn.get_bucket(self.s3_bucket_name) + except: + return 'bucket_error' # Fetch the objects keys and get the billing file - k = Key(bucket) - k.key = s3_file_key try: + k = Key(bucket) + k.key = s3_file_key k.get_contents_to_filename(self.billing_file) + k.close except: - return False - k.close + return 'key_error' # Parse the file and get the InvoiceTotal amount - with open(self.billing_file, 'rb') as f: - reader = csv.reader(f) - for row in reader: - if ''.join(row).find('InvoiceTotal') == -1: - continue - i = i + 1 - return row[-1] + try: + with open(self.billing_file, 'rb') as f: + reader = csv.reader(f) + for row in reader: + if ''.join(row).find('InvoiceTotal') == -1: + continue + i = i + 1 + return row[-1] + except: + return 'csv_error' + + return False def aws_bill(self, i3s_output_list, i3s_config): response = { 'cached_until': time() + self.cache_timeout, - 'full_text': '' + 'full_text': '', + 'color': i3s_config['color_bad'] } bill_amount = self._get_bill_amount() - if bill_amount: + if bill_amount == 'csv_error': + response['full_text'] = 'Check your csv file' + elif bill_amount == 'key_error': + response['full_text'] = 'Key not found in the bucket' + elif bill_amount == 'bucket_error': + response['full_text'] = 'Check the bucket name or your AWS keys' + elif bill_amount == 'conn_error': + response['full_text'] = 'Check your internet access' + elif bill_amount is not False: response['full_text'] = str(bill_amount) + "$" response['color'] = i3s_config['color_good'] else: response['full_text'] = "Billing file not found in the bucket" - response['color'] = i3s_config['color_bad'] return response -- cgit v1.3 From e3d7c80941dd5a9e0a4e7d51d9cbdb36933c7bcc Mon Sep 17 00:00:00 2001 From: Anthony Brodard Date: Wed, 29 Apr 2015 17:19:37 +0200 Subject: Typo fix --- py3status/modules/aws_bill.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'py3status') diff --git a/py3status/modules/aws_bill.py b/py3status/modules/aws_bill.py index 6d5c036..ebe762f 100755 --- a/py3status/modules/aws_bill.py +++ b/py3status/modules/aws_bill.py @@ -89,7 +89,7 @@ class Py3status: bill_amount = self._get_bill_amount() if bill_amount == 'csv_error': - response['full_text'] = 'Check your csv file' + response['full_text'] = 'Bad CSV file' elif bill_amount == 'key_error': response['full_text'] = 'Key not found in the bucket' elif bill_amount == 'bucket_error': @@ -97,10 +97,10 @@ class Py3status: elif bill_amount == 'conn_error': response['full_text'] = 'Check your internet access' elif bill_amount is not False: - response['full_text'] = str(bill_amount) + "$" + response['full_text'] = str(bill_amount) + '$' response['color'] = i3s_config['color_good'] else: - response['full_text'] = "Billing file not found in the bucket" + response['full_text'] = 'Global error - WTF exception' return response -- cgit v1.3 From 2291364d1691e52cf1c3662d58ef87808d2477b5 Mon Sep 17 00:00:00 2001 From: Anthony Brodard Date: Wed, 29 Apr 2015 17:21:28 +0200 Subject: Change param desc --- py3status/modules/aws_bill.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'py3status') diff --git a/py3status/modules/aws_bill.py b/py3status/modules/aws_bill.py index ebe762f..8ca5f42 100755 --- a/py3status/modules/aws_bill.py +++ b/py3status/modules/aws_bill.py @@ -11,7 +11,7 @@ Configuration parameters: - aws_secret_access_key : Your AWS secret key - aws_account_id : the root ID of the AWS account. Can be find here https://console.aws.amazon.com/billing/home#/account - s3_bucket_name : the bucket where billing files are sent by AWS. Follow this article to activate this feature : http://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/detailed-billing-reports.html - - billing_file : where the billing file must be stored + - billing_file : csv file location - cache_timeout : how often we refresh this module in seconds @author nawadanp -- cgit v1.3 From cfa9966aa6f881277fbb8680d44ce30862aa233b Mon Sep 17 00:00:00 2001 From: Anthony Brodard Date: Wed, 29 Apr 2015 17:31:41 +0200 Subject: Typo fix + yapf compliance --- py3status/modules/aws_bill.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'py3status') diff --git a/py3status/modules/aws_bill.py b/py3status/modules/aws_bill.py index 8ca5f42..f2dd5dc 100755 --- a/py3status/modules/aws_bill.py +++ b/py3status/modules/aws_bill.py @@ -3,23 +3,23 @@ Display the current AWS bill. ##### WARNING ##### -This module generate some costs on the AWS bill. Take care about the cache_timout to limit these fees ! +This module generate some costs on the AWS bill. Take care about the cache_timeout to limit these fees ! ##### WARNING ##### Configuration parameters: - aws_access_key_id : Your AWS access key - - aws_secret_access_key : Your AWS secret key - aws_account_id : the root ID of the AWS account. Can be find here https://console.aws.amazon.com/billing/home#/account - - s3_bucket_name : the bucket where billing files are sent by AWS. Follow this article to activate this feature : http://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/detailed-billing-reports.html + - aws_secret_access_key : Your AWS secret key - billing_file : csv file location - cache_timeout : how often we refresh this module in seconds + - s3_bucket_name : the bucket where billing files are sent by AWS. Follow this article to activate this feature : http://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/detailed-billing-reports.html @author nawadanp """ +import boto import csv import datetime -import boto from boto.s3.connection import Key from time import time @@ -29,24 +29,24 @@ class Py3status: # available configuration parameters aws_access_key_id = '' - aws_secret_access_key = '' aws_account_id = '' - s3_bucket_name = '' + aws_secret_access_key = '' billing_file = '/tmp/.aws_billing.csv' cache_timeout = 3600 + s3_bucket_name = '' def _get_bill_amount(self): # Billing file name, generated by Amazon itself # Format : 123456789012-aws-billing-csv-yyyy-mm.csv s3_file_key = self.aws_account_id + '-aws-billing-csv-' + \ datetime.datetime.now().strftime( - '%Y') + '-' + datetime.datetime.now().strftime('%m') + '.csv' + '%Y') + '-' + datetime.datetime.now().strftime('%m') + '.csv' i = 0 # Connection to s3 service try: - conn = boto.connect_s3( - self.aws_access_key_id, self.aws_secret_access_key) + conn = boto.connect_s3(self.aws_access_key_id, + self.aws_secret_access_key) except: return 'conn_error' @@ -111,10 +111,7 @@ if __name__ == "__main__": """ from time import sleep x = Py3status() - config = { - 'color_good': '#00FF00', - 'color_bad': '#FF0000', - } + config = {'color_good': '#00FF00', 'color_bad': '#FF0000', } while True: print(x.aws_bill([], config)) sleep(1) -- cgit v1.3