#!/usr/local/bin/python
import argparse
import bz2
import calendar
import csv
import datetime
import locale
import logging
class EnUSCookedTest:
# A cooked "en_US" locale
cooked_values = {
\'currency_symbol\': \'$\',
\'decimal_point\': \'.\',
\'frac_digits\': 2,
\'grouping\': [3, 3, 0],
\'int_curr_symbol\': \'USD \',
\'int_frac_digits\': 2,
\'mon_decimal_point\': \'.\',
\'mon_grouping\': [3, 3, 0],
\'mon_thousands_sep\': \',\',
\'n_cs_precedes\': 1,
\'n_sep_by_space\': 0,
\'n_sign_posn\': 1,
\'negative_sign\': \'-\',
\'p_cs_precedes\': 1,
\'p_sep_by_space\': 0,
\'p_sign_posn\': 1,
\'positive_sign\': \'\',
\'thousands_sep\': \',\'
}
def from_utc(utcTime,fmt="%Y-%m-%dT%H:%M:%S"):
"""
Convert UTC time string to time.struct_time
"""
# change datetime.datetime to time, return time.struct_time type
return datetime.datetime.strptime(utcTime, fmt)
if __name__ == \'__main__\':
args = argparse.ArgumentParser(description=\'Reads the file put out by the receipts processor put out by procmail.py and gets monthly totals\')
args.add_argument(\'-v\', \'--verbose\', action=\'store_true\', help=\'turn debugging up\')
args.add_argument(\'--month\', type=int, help=\'Month whose total we want\', action=\'store\', default=0)
args.add_argument(\'--day\', type=int, help=\'Billing End date\', action=\'store\', default=22)
args.add_argument(\'--locale\', help="Locale for you, python doesn\'t support currency formatting in the default locale", action=\'store\', default=\'en_US.ISO8859-1\')
parsed = args.parse_args()
if parsed.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.FATAL)
yesterday = datetime.date.today()
yesterday = yesterday.replace(month=yesterday.month-parsed.month, day=parsed.day)
logging.debug(\'Locale is {}\'.format(parsed.locale))
locale._override_localeconv = EnUSCookedTest.cooked_values
monthly_total = 0;
with bz2.BZ2File(\'rcpts.bz2\') as fin:
reader = csv.DictReader(fin, fieldnames = [\'Store\', \'Amount\', \'Timestamp\'])
reader.next()
for transaction in list(reader):
logging.debug(\'Transaction Date is {}\'.format(transaction[\'Timestamp\']))
logging.debug(\'Target Date: {}\'.format(yesterday.strftime(\'%c\')))
logging.debug(type(from_utc(transaction[\'Timestamp\'])))
if yesterday >= from_utc(transaction[\'Timestamp\']).date():
monthly_total = monthly_total + float(transaction[\'Amount\'])
print \'Total for the month of {} is {}\'.format(yesterday.strftime(\'%B\'), locale.currency(monthly_total))
logging.debug(str(yesterday))