-
Notifications
You must be signed in to change notification settings - Fork 5
/
profileHelper.py
101 lines (90 loc) · 3.5 KB
/
profileHelper.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
import os
from copy import deepcopy
class ProfileHelper():
'''
ProfileHelper is a helper class to load and save the profiles to a file and load them from that file.
Args:
logger (Logger): initialized Logger class used for logging
config (dict): The path to the config file.
'''
def __init__(self, logger, config):
logger.info(self, 'Initialisating ProfileHelper...')
self.logger = logger
self.__config = config
self.foldername = self.__config['profileFolderName']
self._data = {}
self.logger.info(self, 'Done initialisating ProfileHelper...')
self.loadProfiles()
def __repr__(self):
return 'TODO'
def __str__(self):
return 'ProfileHelper'
def loadProfiles(self):
'''
Loads the data from all the Profiles stored on the brick.
'''
self.logger.info(self, 'Loading profiles...')
try:
if not self.foldername in os.listdir():
os.mkdir(self.foldername)
os.chdir(self.foldername)
for k in self.__config['profileNames']:
if not '%s.dat' % k in os.listdir():
open('%s.dat' % k, 'a').close()
self._data[k] = []
with open('%s.dat' % k, 'r') as dat:
for line in dat:
l = line.split(', ')
intList = [float(s) for s in l]
self._data[k].append(intList)
self.logger.info(self, 'Loaded profiles sucessfully')
except Exception as exception:
self.logger.error(self, 'Failed to load Profiles', exception)
os.chdir('..')
def _saveProfiles(self):
'''
Saves the data of all the Profiles to the brick.
'''
self.logger.info(self, 'Saving profiles....')
try:
if not self.foldername in os.listdir():
os.mkdir(self.foldername)
os.chdir(self.foldername)
for k in self.__config['profileNames']:
if not '%s.dat' % k in os.listdir():
open('%s.dat' % k, 'a').close()
ffile = open('%s.dat' % k, 'w')
strArr = ''
for l in self._data[k]:
for i in l:
strArr += str(i) + ', '
strArr = strArr[:-2]
ffile.write(strArr + '\n')
strArr = ''
ffile.close()
except Exception as exception:
self.logger.error(self, 'Failed to save Profiles', exception)
os.chdir('..')
def getProfileData(self, profile):
'''
Function to get the data of a given profile. WIP
Args:
profile (str): The name of the profile
Returns:
array: The array of number code arrays frome the given profile
'''
try:
x = deepcopy(self._data[profile])
return x
except KeyError as exception:
self.logger.warn(self, 'Couldn\'t get profile data for %s: No such profile in data' % str(exception))
def setProfileData(self, profile, data):
'''
Function to set the data of a given profile. WIP
Args:
profile (str): The name of the profile
data (array): The array with the data
'''
self.logger.debug(self, 'Setting Profile data for profile "%s"' % profile)
self._data[profile] = deepcopy(data)
self._saveProfiles()