-
Notifications
You must be signed in to change notification settings - Fork 72
Eliminate code duplicates in adapters.py #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| import logging | ||
| import select | ||
| import os | ||
| import pathlib | ||
| import struct | ||
| import collections | ||
| import time | ||
|
|
||
| from errno import EINTR | ||
| from typing import List | ||
|
|
||
| import inotify.constants | ||
| import inotify.calls | ||
|
|
@@ -296,7 +298,6 @@ def event_gen(self, ignore_missing_new_folders=False, **kwargs): | |
| "adding a watch on it (because we're " | ||
| "being recursive): [%s]", full_path) | ||
|
|
||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary. |
||
| self._i.add_watch(full_path, self._mask) | ||
|
|
||
| if header.mask & inotify.constants.IN_DELETE: | ||
|
|
@@ -323,6 +324,14 @@ def event_gen(self, ignore_missing_new_folders=False, **kwargs): | |
|
|
||
| yield event | ||
|
|
||
| def _load_trees(self, path_list: List[pathlib.Path]): | ||
| for entry_path in path_list: | ||
| if entry_path.is_dir(): | ||
| self._i.add_watch(str(entry_path), self._mask) | ||
| for sub_entry_path in entry_path.rglob('*'): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just stick with Also, it'd be better to do (for example): ..which will reduce nesting. |
||
| if sub_entry_path.is_dir(): | ||
| self._i.add_watch(str(sub_entry_path), self._mask) | ||
|
|
||
| @property | ||
| def inotify(self): | ||
| return self._i | ||
|
|
@@ -335,31 +344,8 @@ def __init__(self, path, mask=inotify.constants.IN_ALL_EVENTS, | |
| block_duration_s=_DEFAULT_EPOLL_BLOCK_DURATION_S): | ||
| super(InotifyTree, self).__init__(mask=mask, block_duration_s=block_duration_s) | ||
|
|
||
| self.__root_path = path | ||
|
|
||
| self.__load_tree(path) | ||
|
|
||
| def __load_tree(self, path): | ||
| _LOGGER.debug("Adding initial watches on tree: [%s]", path) | ||
|
|
||
| paths = [] | ||
|
|
||
| q = [path] | ||
| while q: | ||
| current_path = q[0] | ||
| del q[0] | ||
|
|
||
| paths.append(current_path) | ||
|
|
||
| for filename in os.listdir(current_path): | ||
| entry_filepath = os.path.join(current_path, filename) | ||
| if os.path.isdir(entry_filepath) is False: | ||
| continue | ||
|
|
||
| q.append(entry_filepath) | ||
|
|
||
| for path in paths: | ||
| self._i.add_watch(path, self._mask) | ||
| super()._load_trees([pathlib.Path(path)]) | ||
|
|
||
|
|
||
| class InotifyTrees(_BaseTree): | ||
|
|
@@ -369,27 +355,5 @@ def __init__(self, paths, mask=inotify.constants.IN_ALL_EVENTS, | |
| block_duration_s=_DEFAULT_EPOLL_BLOCK_DURATION_S): | ||
| super(InotifyTrees, self).__init__(mask=mask, block_duration_s=block_duration_s) | ||
|
|
||
| self.__load_trees(paths) | ||
|
|
||
| def __load_trees(self, paths): | ||
| _LOGGER.debug("Adding initial watches on trees: [%s]", ",".join(map(str, paths))) | ||
|
|
||
| found = [] | ||
|
|
||
| q = paths | ||
| while q: | ||
| current_path = q[0] | ||
| del q[0] | ||
|
|
||
| found.append(current_path) | ||
|
|
||
| for filename in os.listdir(current_path): | ||
| entry_filepath = os.path.join(current_path, filename) | ||
| if os.path.isdir(entry_filepath) is False: | ||
| continue | ||
|
|
||
| q.append(entry_filepath) | ||
|
|
||
|
|
||
| for path in found: | ||
| self._i.add_watch(path, self._mask) | ||
| self._load_trees([pathlib.Path(entry) for entry in paths]) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably fine to use native lists for the type hints.