class AsyncFileSearcher: (source)
Constructor: AsyncFileSearcher(paths, out_queue, recursive, find_undetected_files, ...)
This class detects files in one, or several specified directories.
Directories that are to be excluded from the search can be specified. A specific file pattern can be supplied, default is all files. File patterns that should be ignored can be supplied. A recursive search can also be activated.
The file is only reported when it's available (not when it's initially detected since it might be in a copying or moving phase).
Due to antivirus programs are scanning new files as well, there's sometimes
a conflict of interest. The consequences when this happens are that when
the file is detected, it's not available, and therefore it will be ignored.
Another related issue is that files might already exist in the monitored
path(s) when the program starts, and they will not be detected either.
This problem is handled when you let the find_undetected_files input
parameter use its default value. When True A check runs every two minutes
that touches files that weren't detected earlier.
- The workflow is as follows:
- Start the file detection by calling method start().
- Receive detection messages on supplied out_queue.
- Stop the detection by calling method stop().
Example data:
{"msgType": "FileFound",
"file": "D:\prod\kundin\cust3\DDDD.231008.txt"}| Method | __init__ |
The class constructor. |
| Async Method | notify |
Send msgType messages to the broker. |
| Async Method | start |
Start the used resources in a controlled way. |
| Async Method | stop |
Stop the used resources in a controlled way. |
| Instance Variable | file |
Put a JSON message on the report queue when a file is detected and available. |
| Instance Variable | find |
Touch undetected files in search path(s) (default is True). |
| Instance Variable | lock |
A locking mechanism that protects the suppressed cache object. |
| Instance Variable | observer |
Observer thread that schedules watching directories and dispatches calls to event handlers. |
| Instance Variable | out |
File detection report queue. |
| Instance Variable | recursive |
Search path(s) recursively (default is False). |
| Instance Variable | root |
List of path(s) to search. |
| Instance Variable | suppress |
Suppress reporting of already detected files. |
| Instance Variable | watchers |
Keep track of which paths(s) that is observed. |
| Instance Variable | work |
Internal message broker queue. |
| Async Method | _handle |
Handle multiple detections of "small" files (only report once). |
| Async Method | _handle |
Touch undetected files to make them detectable again. |
| Async Method | _message |
Broker messages between interested parties using a queue. |
| Async Method | _process |
Process received FileFound message. |
| Async Method | _process |
Update the list of paths used in the search. |
| Async Method | _process |
Process health status request. |
list, out_queue: Queue, recursive: bool = False, find_undetected_files: bool = True, case_sensitive: bool = False, excluded_paths: Optional[ list] = None, patterns: Optional[ list] = None, ignored_patterns: Optional[ list] = None):
(source)
¶
The class constructor.
| Parameters | |
paths:list | List of path(s) to search. |
outQueue | Search response queue. |
recursive:bool | Search path(s) recursively (default is False). |
findbool | Touch undetected files in search path(s) (default is True). |
casebool | Use case-sensitive file matching (default is False). |
excludedOptional[ | List of path(s) to exclude in search. |
patterns:Optional[ | A list of file search patterns to use (implicitly ['*'] is used if None is specified). |
ignoredOptional[ | A list of file search patterns to ignore. |
| Raises | |
RuntimeError | When an invalid path is specified. |
AssertionError | When path parameter is not a list. |
AssertionError | When pattern parameter is not None or not a list. |
AssertionError | When excluded_paths parameter is not None or not a list. |
AssertionError | When ignored_patterns parameter is not None or not a list. |
Handle multiple detections of "small" files (only report once).
The duplicate detections only occur for "small" files, mostly within a few milliseconds, so a 2-second prune timeout should be ok...
This method is run by the scheduler every 10 seconds.
Broker messages between interested parties using a queue.
- Handled message types are:
- Stop
- FileFound
- StatusRequest
- UpdateSearchPaths
Update the list of paths used in the search.
Example data:
{'msgType': 'UpdateSearchPaths', 'data': [<root Paths>]}| Parameters | |
msg:dict | A UpdateSearchPaths message. |