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
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
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
64
69
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
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():
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):
120
122 try:
123 del self.binaries[node_name]
124 self.update_files()
125 except:
126 pass
127
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