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