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   
 36  try: 
 37      from python_qt_binding.QtCore import QObject, Signal, QFileSystemWatcher 
 38  except: 
 39      pass 
 40   
 41   
42 -class FileWatcher(QObject):
43 ''' 44 A class to watch for file changes. 45 ''' 46 config_changed = Signal(str, list) 47 '''@ivar: a signal to inform the receiver about the changes on 48 launch file or included file. 49 ParameterB{:} (changed file, list of tuples(masteruri, launch file))''' 50 51 binary_changed = Signal(str, list) 52 '''@ivar: a signal to inform the receiver about the changes on 53 binary file or included file. 54 ParameterB{:} (binary file, list of tuples(node name, masteruri, launchfile)) 55 ''' 56
57 - def __init__(self):
58 QObject.__init__(self) 59 self.file_watcher = QFileSystemWatcher() 60 self.file_watcher.fileChanged.connect(self.on_file_changed) 61 self.changed = {} 62 self.launches = {} 63 self.binaries = {}
64
65 - def __del__(self):
66 # Delete to avoid segfault if the LaunchConfig class is destroyed recently 67 # after creation and xmlrpclib.ServerProxy process a method call. 68 del self.file_watcher
69
70 - def on_file_changed(self, filepath):
71 ''' 72 callback method, which is called by U{QtCore.QFileSystemWatcher<https://srinikom.github.io/pyside-docs/PySide/QtCore/QFileSystemWatcher.html>} 73 if one of a launch file, included files or binary are changed. 74 Depend on type of the file a L{FileWatcher.config_changed} or 75 L{FileWatcher.binary_changed} signal will be emitted. 76 ''' 77 # to avoid to handle from QFileSystemWatcher fired the signal two times 78 if (filepath not in self.changed or (filepath in self.changed and self.changed[filepath] + 0.05 < time.time())): 79 self.changed[filepath] = time.time() 80 launch_changes = [] 81 for (uri, lfile, _), files in self.launches.items(): # _:=id 82 if filepath in files: 83 launch_changes.append((uri, lfile)) 84 if launch_changes: 85 self.config_changed.emit(filepath, launch_changes) 86 binaries_changed = [] 87 for node_name, (binary_file, masteruri, launchfile) in self.binaries.items(): 88 if filepath == binary_file: 89 binaries_changed.append((node_name, masteruri, launchfile)) 90 if binaries_changed: 91 self.binary_changed.emit(filepath, binaries_changed)
92
93 - def add_launch(self, masteruri, launch_file, launch_id, files):
94 if (masteruri, launch_file, launch_id) in self.launches: 95 self.launches[(masteruri, launch_file, launch_id)].extend([os.path.normpath(f) for f in files]) 96 else: 97 self.launches[(masteruri, launch_file, launch_id)] = [os.path.normpath(f) for f in files] 98 self.update_files()
99
100 - def rem_launch(self, masteruri, launch_file='', launch_id=''):
101 try: 102 if launch_file: 103 if launch_id: 104 del self.launches[(masteruri, launch_file, launch_id)] 105 else: 106 for (uri, cfgfile, cfgid), _ in self.launches.items(): 107 if uri == masteruri and cfgfile == launch_file: 108 del self.launches[(uri, cfgfile, cfgid)] 109 else: 110 for (uri, cfgfile, cfgid), _ in self.launches.items(): 111 if uri == masteruri: 112 del self.launches[(uri, cfgfile, cfgid)] 113 except: 114 pass 115 self.update_files()
116
117 - def add_binary(self, binary_file, node_name, masteruri, launchfile):
118 self.binaries[node_name] = (binary_file, masteruri, launchfile) 119 self.update_files()
120
121 - def rem_binary(self, node_name):
122 try: 123 del self.binaries[node_name] 124 self.update_files() 125 except: 126 pass
127
128 - def update_files(self):
129 result = set() 130 for files in self.launches.itervalues(): 131 result.update(set(files)) 132 binaries = set() 133 for _, (binary_file, _, _) in self.binaries.items(): 134 binaries.add(binary_file) 135 result.update(binaries) 136 files = set(self.file_watcher.files()) 137 to_remove = list(files - result) 138 if to_remove: 139 self.file_watcher.removePaths(to_remove) 140 to_add = list(result - files) 141 if to_add: 142 self.file_watcher.addPaths(to_add)
143