Helper to watch a single path.
| 73 | |
| 74 | |
| 75 | class _PathWatcher(object): |
| 76 | """ |
| 77 | Helper to watch a single path. |
| 78 | """ |
| 79 | |
| 80 | def __init__(self, root_path, accept_directory, accept_file, single_visit_info, max_recursion_level, sleep_time=0.0): |
| 81 | """ |
| 82 | :type root_path: str |
| 83 | :type accept_directory: Callback[str, bool] |
| 84 | :type accept_file: Callback[str, bool] |
| 85 | :type max_recursion_level: int |
| 86 | :type sleep_time: float |
| 87 | """ |
| 88 | self.accept_directory = accept_directory |
| 89 | self.accept_file = accept_file |
| 90 | self._max_recursion_level = max_recursion_level |
| 91 | |
| 92 | self._root_path = root_path |
| 93 | |
| 94 | # Initial sleep value for throttling, it'll be auto-updated based on the |
| 95 | # Watcher.target_time_for_single_scan. |
| 96 | self.sleep_time = sleep_time |
| 97 | |
| 98 | self.sleep_at_elapsed = 1.0 / 30.0 |
| 99 | |
| 100 | # When created, do the initial snapshot right away! |
| 101 | old_file_to_mtime = {} |
| 102 | self._check(single_visit_info, lambda _change: None, old_file_to_mtime) |
| 103 | |
| 104 | def __eq__(self, o): |
| 105 | if isinstance(o, _PathWatcher): |
| 106 | return self._root_path == o._root_path |
| 107 | |
| 108 | return False |
| 109 | |
| 110 | def __ne__(self, o): |
| 111 | return not self == o |
| 112 | |
| 113 | def __hash__(self): |
| 114 | return hash(self._root_path) |
| 115 | |
| 116 | def _check_dir(self, dir_path, single_visit_info, append_change, old_file_to_mtime, level): |
| 117 | # This is the actual poll loop |
| 118 | if dir_path in single_visit_info.visited_dirs or level > self._max_recursion_level: |
| 119 | return |
| 120 | single_visit_info.visited_dirs.add(dir_path) |
| 121 | try: |
| 122 | if isinstance(dir_path, bytes): |
| 123 | try: |
| 124 | dir_path = dir_path.decode(sys.getfilesystemencoding()) |
| 125 | except UnicodeDecodeError: |
| 126 | try: |
| 127 | dir_path = dir_path.decode("utf-8") |
| 128 | except UnicodeDecodeError: |
| 129 | return # Ignore if we can't deal with the path. |
| 130 | |
| 131 | new_files = single_visit_info.file_to_mtime |
| 132 |