Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

''' 

CLI interface for the Mint app. 

''' 

import sys 

import datetime 

import calendar 

import convert 

import charts 

import reports 

import data 

 

 

def print_menu(): 

''' 

Prints a list of all commands and parameters. 

''' 

print('\n') 

print('a - to get a list of accounts being tracked by your Mint account.') 

print('cat [category name] - to see spending by category. If a category name is passed then the details for that category will be shown.') 

print('cp - to compare the current month to the previous month.') 

print('day [date] - to show daily spend for the current date range. If a date is passed then the details for that day will be shown.') 

print('dr [start date] [end date] - to change the date range for which pricing data is loaded.') 

print('help - to show this help menu') 

print('income - to see income for the current date range.') 

print('lf - to load the transaction file.') 

print('pie - to see a pie chart of spending by category.') 

print('spending - to see spending for the current date range.') 

print('quit - to quit this program') 

print('\n') 

 

 

def get_user_requests(): 

''' 

User request loop. Reads the next command from the user and then calls 

the appropriate function. 

''' 

 

transactions = data.Transactions() 

print(str(transactions.count) + ' transactions loaded.') 

 

# User request loop 

while True: 

 

prompt = str(transactions.start_date) + ' - ' + str(transactions.end_date) 

command = input(prompt + ' --> ') 

 

if command[0:2] == 'a': 

accounts = transactions.get_accounts() 

reports.print_accounts(accounts) 

continue 

 

if command[0:3] == 'cat': 

params = command[3:].strip() 

params_list = params.split(' ') 

search_name = params_list[0] 

debits = transactions.get_transactions_by_type('debit') 

 

if len(search_name) == 0: 

categories = transactions.get_categories(debits) 

reports.print_category_totals(categories) 

else: 

category_transactions = transactions.get_category_by_name(search_name, debits) 

if category_transactions is None: 

print('No transactions found for category: ' + search_name + '.') 

else: 

reports.print_transactions(search_name, category_transactions) 

continue 

 

if command == 'cp': 

now = datetime.datetime.now() 

year = now.year 

month = now.month 

# returns a touple which is the day of the week of the first day of the month and 

# the number of days in the month. 

last_day = calendar.monthrange(year, month)[1] 

start_date = datetime.date(year, month, 1) 

end_date = datetime.date(year, month, last_day) 

debits = transactions.get_transactions_by_type('debit', start_date, end_date) 

current_month = transactions.get_categories(debits) 

current_month_totals = transactions.get_category_totals(current_month) 

 

# Get previous month. 

if month == 1: 

year -= 1 

month = 12 

else: 

month -= 1 

 

last_day = calendar.monthrange(year, month)[1] 

start_date = datetime.date(year, month, 1) 

end_date = datetime.date(year, month, last_day) 

debits = transactions.get_transactions_by_type('debit', start_date, end_date) 

previous_month = transactions.get_categories(debits) 

previous_month_totals = transactions.get_category_totals(previous_month) 

 

reports.print_category_comparison(previous_month_totals, current_month_totals) 

continue 

 

if command[0:3] == 'day': 

report_date = command[3:].strip() 

if len(report_date) > 0: 

report_date = report_date.split(' ') 

report_date = convert.to_date(report_date) 

else: 

days = transactions.get_daily_spending() 

reports.print_daily_spending(days) 

continue 

 

if command[0:2] == 'dr': 

params = command[2:].strip() 

params_list = params.split(' ') 

start_date = params_list[0] 

end_date = params_list[1] 

transactions.start_date = convert.to_date(start_date) 

transactions.end_date = convert.to_date(end_date) 

continue 

 

if command == 'help': 

print_menu() 

continue 

 

if command == 'income': 

credit_transactions = transactions.get_transactions_by_type('credit') 

reports.print_transactions('Credits', credit_transactions) 

reports.print_transaction_totals('Credits', credit_transactions) 

continue 

 

if command == 'spending': 

debit_transactions = transactions.get_transactions_by_type('debit') 

reports.print_transactions('Debits', debit_transactions) 

reports.print_transaction_totals('Debits', debit_transactions) 

continue 

 

if command == 'lf': 

print('Reloading transaction file ...') 

transactions = transactions.Transactions() 

continue 

 

if command == 'pie': 

debits = transactions.get_transactions_by_type('debit') 

categories = transactions.get_categories(debits) 

charts.category_pie_chart(categories) 

continue 

 

if command == 'quit' or command == 'q': 

break 

 

print('*** Unrecognized command ***') 

 

 

if __name__ == '__main__': 

# Main execution 

# Passed arguments 

print(sys.argv) 

 

# This function is a user request loop. 

get_user_requests()