forked from KalleHallden/AutoTimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.py
103 lines (86 loc) · 3.07 KB
/
activity.py
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
import datetime
import json
from dateutil import parser
class AcitivyList:
def __init__(self, activities):
self.activities = activities
def initialize_me(self):
activity_list = AcitivyList([])
with open('activities.json', 'r') as f:
data = json.load(f)
activity_list = AcitivyList(
activities = self.get_activities_from_json(data)
)
return activity_list
def get_activities_from_json(self, data):
return_list = []
for activity in data['activities']:
return_list.append(
Activity(
name = activity['name'],
time_entries = self.get_time_entires_from_json(activity),
)
)
self.activities = return_list
return return_list
def get_time_entires_from_json(self, data):
return_list = []
for entry in data['time_entries']:
return_list.append(
TimeEntry(
start_time = parser.parse(entry['start_time']),
end_time = parser.parse(entry['end_time']),
days = entry['days'],
hours = entry['hours'],
minutes = entry['minutes'],
seconds = entry['seconds'],
)
)
self.time_entries = return_list
return return_list
def serialize(self):
return {
'activities' : self.activities_to_json()
}
def activities_to_json(self):
activities_ = []
for activity in self.activities:
activities_.append(activity.serialize())
return activities_
class Activity:
def __init__(self, name, time_entries):
self.name = name
self.time_entries = time_entries
def serialize(self):
return {
'name' : self.name,
'time_entries' : self.make_time_entires_to_json()
}
def make_time_entires_to_json(self):
time_list = []
for time in self.time_entries:
time_list.append(time.serialize())
return time_list
class TimeEntry:
def __init__(self, start_time, end_time, days, hours, minutes, seconds):
self.start_time = start_time
self.end_time = end_time
self.total_time = end_time - start_time
self.days = days
self.hours = hours
self.minutes = minutes
self.seconds = seconds
def _get_specific_times(self):
self.days, self.seconds = self.total_time.days, self.total_time.seconds
self.hours = self.days * 24 + self.seconds // 3600
self.minutes = (self.seconds % 3600) // 60
self.seconds = self.seconds % 60
def serialize(self):
return {
'start_time' : self.start_time.strftime("%Y-%m-%d %H:%M:%S"),
'end_time' : self.end_time.strftime("%Y-%m-%d %H:%M:%S"),
'days' : self.days,
'hours' : self.hours,
'minutes' : self.minutes,
'seconds' : self.seconds
}