-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoffeethon.py
More file actions
executable file
·143 lines (119 loc) · 3.68 KB
/
coffeethon.py
File metadata and controls
executable file
·143 lines (119 loc) · 3.68 KB
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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
"""Coffeethon.py: Help you keep track of the number of cup of coffee drank."""
import sys
import getopt
from datetime import datetime
__author__ = "Tom Celestin"
__copyright__ = "Copyright 2018, Planet Earth"
def print_cups(watch, amount, operation=""):
"""Display coffee values"""
message = ""
if not watch:
message = "You " + operation + " "
else:
message = "Today you drank "
message += str(amount) + " cups of coffee"
if amount < 2:
message = message.replace("cups", "cup")
print(message)
def manage_main_file(filename, sum, number):
"""Change value of the coffee counter."""
if not number.isdigit():
print("Please enter a valid number")
return False
number = int(number)
file = open(filename, "r")
current = int(file.read())
if sum is False and number > current:
print("You drank less than this !")
return False
if sum is False:
current -= number
print_cups(False, number, "removed")
else:
current += number
print_cups(False, number, "added")
file.close()
file = open(filename, "w")
file.write(str(current))
file.close()
def manage_stats_file(filename, number):
"""Change value of the coffee counter."""
file = open(filename, "r")
date = datetime.now().date()
timestamp = date.strftime("%Y%m%d")
current = timestamp + ":" + number
file.close()
file = open(filename, "a")
file.write(str(current))
file.write("\n")
file.close()
def clear(filename):
"""Reset the counter to 0."""
file = open(filename, "w")
file.write("0")
file.close()
def watch(filename):
"""Display the counter in cool way."""
file = open(filename, "r")
current = int(file.read())
print_cups(True, current)
for i in range(current):
print("☕️", end=" ")
file.close()
def main(argv):
"""Main function."""
main_filename = "/tmp/coffeethon.txt"
stats_filename = "./logs/stats.txt"
try:
main_file = open(main_filename)
stats_file = open(stats_filename)
except IOError:
# If not exists, create the files
main_file = open(main_filename, 'w+')
main_file.write("0")
main_file.close()
stats_file = open(stats_filename, 'w+')
stats_file.write("")
stats_file.close()
try:
opts, args = getopt.getopt(
argv,
"harcw",
[
"add",
"remove",
"clear",
"watch"
]
)
except getopt.GetoptError as e:
print(str(e))
print('coffeethon.py --add [number]')
print('coffeethon.py --remove [number]')
print('coffeethon.py --clear')
print('coffeethon.py --watch')
sys.exit(2)
for opt, arg in opts:
number = "1"
if len(sys.argv) > 2:
number = sys.argv[2]
if opt == '-h':
print('coffeethon.py --add [number]')
print('coffeethon.py --remove [number]')
print('coffeethon.py --clear')
print('coffeethon.py --watch')
sys.exit()
elif opt in ("-a", "--add"):
manage_main_file(main_filename, True, number)
manage_stats_file(stats_filename, number)
elif opt in ("-r", "--remove"):
manage_main_file(main_filename, False, number)
# manage_stats_file(stats_filename, False, number)
elif opt in ("-c", "--clear"):
clear(main_filename)
elif opt in ("-w", "--watch"):
watch(main_filename)
if __name__ == "__main__":
main(sys.argv[1:])