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 from python_qt_binding.QtCore import QObject
33 import os
34
35 import rospy
36
37 import node_manager_fkie as nm
38
39
40 -class History(QObject):
41
42 PARAM_CACHE = dict()
43 '''
44 the cache is used to store and recover the value for last entered parameter in parameter dialog.
45 '''
46
50
53
54 - def cachedParamValues(self, key):
55 try:
56 return list(self.PARAM_CACHE[key])
57 except:
58 result = []
59 return result
60
61 - def addParamCache(self, key, value):
62 self._add2Cache(self.PARAM_CACHE, key, value)
63
64 - def removeParamCache(self, key, value):
65 self._removeFromCache(self.PARAM_CACHE, key, value)
66
67 - def loadCache(self, history_file):
68 '''
69 Loads the content of the given file and return it as cache.
70 @param history_file: the name of the history file
71 @type history_file: C{str}
72 @return: the dictionary with arguments
73 @rtype: C{dict(str(name):[str(value), ...], ...)}
74 '''
75 result = {}
76 historyFile = os.path.join(nm.settings().cfg_path, history_file)
77 if os.path.isfile(historyFile):
78 with open(historyFile, 'r') as f:
79 line = f.readline()
80 while line:
81 if line:
82 line = line.strip()
83 if line:
84 key, sep, value = line.partition(':=')
85 if sep:
86 if key not in result.keys():
87 result[key] = [value]
88 elif len(result[key]) <= nm.settings().param_history_length:
89 result[key].append(value)
90 line = f.readline()
91 return result
92
93 - def storeCache(self, history_file, cache, history_len):
94 '''
95 Stores the cache to a file.
96 @param history_file: the name of the history file
97 @type history_file: C{str}
98 @param cache: the dictionary with values
99 @type cache: C{dict}
100 @param history_len: the maximal count of value for a key
101 @type history_len: C{int}
102 '''
103 ignored = dict()
104 with open(os.path.join(nm.settings().cfg_path, history_file), 'w') as f:
105 for key in cache.keys():
106 count = 0
107 for value in cache[key]:
108 if count < history_len:
109 try:
110 f.write(''.join([key, ':=', value, '\n']))
111 except UnicodeEncodeError, e:
112 ignored[key] = (value, str(e))
113 except:
114 import traceback
115 rospy.logwarn("Storing history aborted: %s", traceback.format_exc(1))
116 count += 1
117 else:
118 break
119 if ignored:
120 rospy.logwarn("Error while storing follow keys: %s", str(ignored))
121
122 - def _add2Cache(self, cache, key, value):
123 uvalue = unicode(value)
124 if key and uvalue:
125 if key not in cache:
126 cache[key] = [uvalue]
127 elif uvalue not in cache[key]:
128 cache[key].insert(0, uvalue)
129 if len(cache[key]) >= nm.settings().param_history_length:
130 cache[key].pop()
131 else:
132 cache[key].remove(uvalue)
133 cache[key].insert(0, uvalue)
134
135 - def _removeFromCache(self, cache, key, value):
136 uvalue = unicode(value)
137 if key and uvalue:
138 if key in cache:
139 value_list = cache[key]
140 try:
141 value_list.remove(uvalue)
142 except:
143 pass
144 if len(value_list) == 0:
145 del cache[key]
146