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

Source Code for Module node_manager_fkie.file_watcher'

  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   
 33  import os 
 34  import time 
 35  import rospy 
 36  import roslib 
 37   
 38  try: 
 39    from python_qt_binding import QtCore 
 40  except: 
 41    pass 
 42   
43 -class FileWatcher(QtCore.QObject):
44 ''' 45 A class to watch for file changes. 46 ''' 47 file_changed = QtCore.Signal(str, list) 48 '''@ivar: a signal to inform the receiver about the changes on 49 launch file or included file. ParameterB{:} (changed file, list of tuples(masteruri, launch file))''' 50
51 - def __init__(self):
52 QtCore.QObject.__init__(self) 53 self.file_watcher = QtCore.QFileSystemWatcher() 54 self.file_watcher.fileChanged.connect(self.on_file_changed) 55 self.changed = {} 56 self.launches = {}
57
58 - def __del__(self):
59 # Delete to avoid segfault if the LaunchConfig class is destroyed recently 60 # after creation and xmlrpclib.ServerProxy process a method call. 61 del self.file_watcher
62
63 - def on_file_changed(self, file):
64 ''' 65 callback method, which is called by L{QtCore.QFileSystemWatcher} if the 66 launch file or included files are changed. In this case 67 L{FileWatcher.file_changed} signal will be emitted. 68 ''' 69 # to avoid to handle from QFileSystemWatcher fired the signal two times 70 if (not self.changed.has_key(file) or (self.changed.has_key(file) and self.changed[file] + 0.05 < time.time())): 71 self.changed[file] = time.time() 72 changes = [] 73 for (uri, lfile, id), files in self.launches.items(): 74 if file in files: 75 changes.append((uri, lfile)) 76 self.file_changed.emit(file, changes)
77
78 - def add(self, masteruri, launch_file, launch_id, files):
79 if self.launches.has_key((masteruri, launch_file, launch_id)): 80 self.launches[(masteruri, launch_file, launch_id)].extend([os.path.normpath(f) for f in files]) 81 else: 82 self.launches[(masteruri, launch_file, launch_id)] = [os.path.normpath(f) for f in files] 83 self.update_files()
84
85 - def rem(self, masteruri, launch_file='', launch_id=''):
86 try: 87 if launch_file: 88 if launch_id: 89 del self.launches[(masteruri, launch_file, launch_id)] 90 else: 91 for (uri, file, id), files in self.launches.items(): 92 if uri == masteruri and file == launch_file: 93 del self.launches[(uri, file, id)] 94 else: 95 for (uri, file, id), files in self.launches.items(): 96 if uri == masteruri: 97 del self.launches[(uri, file, id)] 98 except: 99 # import traceback 100 # print traceback.format_exc() 101 pass 102 self.update_files()
103
104 - def update_files(self):
105 result = set() 106 for files in self.launches.itervalues(): 107 result.update(set(files)) 108 files = self.file_watcher.files() 109 if files: 110 self.file_watcher.removePaths(files) 111 if list(result): 112 self.file_watcher.addPaths(list(result))
113