-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_edge200.py
64 lines (56 loc) · 2.25 KB
/
device_edge200.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
#!/usr/bin/env python3
import os
import psutil
import logging
class DeviceEdge200:
DEVICE_LABEL="GARMIN"
ACTIVITIES_PATH="Garmin/Activities"
def __init__(self):
self.logger= logging.getLogger('device_edge200')
self.logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
self.logger.addHandler(handler)
hard_drive_label =self.get_hard_drive_label()
if hard_drive_label == None:
raise OSError
mount_point = self.get_device_mount_point(hard_drive_label)
if mount_point == None:
raise OSError
else:
self.activities_path = mount_point + "/" + self.ACTIVITIES_PATH
def get_hard_drive_label(self):
path = "/dev/disk/by-label/" + self.DEVICE_LABEL
self.logger.debug("Searching for disk path {}".format(path))
if os.path.exists(path) == False:
self.logger.warning("Path not found")
return None
else:
self.logger.debug("Path found")
return os.path.realpath(os.path.join(os.path.dirname(path), os.readlink(path)))
def get_device_mount_point(self, device):
partitions = psutil.disk_partitions()
result = None
self.logger.debug("Searching for mount point of device {}".format(device))
for partition in partitions:
if partition.device == device:
result = partition.mountpoint
if result != None:
self.logger.debug("Device found mounted at {}".format(result))
else:
self.logger.warning("Mount point not found")
return result
def list_all_activities(self):
activities = []
for (dirpath, dirnames, filenames) in os.walk(self.activities_path):
for filename in filenames:
activities.append(os.path.join(dirpath, filename))
# Sort files by newest, assuming the name embed the activity date
activities.reverse()
return activities
def list_nth_activities(self, number):
returned_number = number
activities = self.list_all_activities()
if len(activities) < number:
returned_number = len(activities)
return activities[:returned_number]