Package node_manager_fkie :: Module history'
[frames] | no frames]

Source Code for Module node_manager_fkie.history'

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Fraunhofer nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 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
49 - def __init__(self):
50 QObject.__init__(self) 51 self.PARAM_CACHE = self.loadCache(nm.settings().PARAM_HISTORY_FILE)
52
53 - def storeAll(self):
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