add Rule and FileManager to README.md

This commit is contained in:
2020-11-03 15:40:12 +01:00
parent f11bea36ec
commit 0ac935cf8a

View File

@@ -21,13 +21,23 @@ This is a very basic example task that just logs each event and task_id:
async def task(event, task_id):
logging.info(f"{task_id}: execute example task: {event}")
```
### FileManager
FileManager moves, copy or deletes files and/or directories following a list of *rules*.
A rule holds an *action* (move, copy or delete) and a regular expression *src_re*. The FileManager task will be executed if *src_re* matches the path of an event. If the action is copy or move, the destination path *dst_re* is mandatory. If *auto_create* is True, possibly missing subdirectories in *dst_re* are automatically created. If *action* is delete and *rec* is True, non-empty directories will be deleted recursively. It is possible to use Regex subgroups or named-subgroups in *src_re* and *dst_re*.
```python
rule = Rule(
action="move", src_re="^/src_path/(?P<path>.*).to_move$",
dst_re="/dst_path/\g<path>", auto_create=False, rec=False)
fm = FileManager(rules=[rule])
```
## Schedulers
pyinotifyd has different schedulers to schedule tasks with an optional delay. The advantages of using a scheduler are consistent logging and the possibility to cancel delayed tasks. Furthermore, schedulers have the ability to differentiate between files and directories.
### TaskScheduler
TaskScheduler to schedule *task* with an optional *delay* in seconds. Use the *files* and *dirs* arguments to schedule tasks only for files and/or directories.
TaskScheduler schedules *task* with an optional *delay* in seconds. Use the *files* and *dirs* arguments to schedule tasks only for files and/or directories.
The *logname* argument is used to set a custom name for log messages. All arguments except for *task* are optional.
```python
s = TaskScheduler(task=task, files=True, dirs=False, delay=0, logname="TaskScheduler")
@@ -39,12 +49,12 @@ TaskScheduler provides two tasks which can be bound to an event in an event map.
Cancel a scheduled task.
### ShellScheduler
ShellScheduler to schedule Shell command *cmd*. The placeholders **{maskname}**, **{pathname}** and **{src_pathname}** are replaced with the actual values of the event. ShellScheduler has the same optional arguments as TaskScheduler and provides the same tasks.
ShellScheduler schedules Shell command *cmd*. The placeholders **{maskname}**, **{pathname}** and **{src_pathname}** are replaced with the actual values of the event. ShellScheduler has the same optional arguments as TaskScheduler and provides the same tasks.
```python
s1 = ShellScheduler(cmd="/usr/local/bin/task.sh {maskname} {pathname} {src_pathname}")
```
## Event maps
Event maps are used to map event types to tasks. It is possible to set a list of tasks to run multiple tasks on a single event. If the task of an event type is set to None, it is ignored.
EventMap maps event types to tasks. It is possible to set a list of tasks to run multiple tasks on a single event. If the task of an event type is set to None, it is ignored.
This is an example:
```python
event_map = EventMap({"IN_CLOSE_NOWRITE": [s.schedule, s1.schedule],
@@ -52,7 +62,7 @@ event_map = EventMap({"IN_CLOSE_NOWRITE": [s.schedule, s1.schedule],
```
## Watches
Watches *path* for event types in *event_map* and execute the corresponding task(s). If *rec* is True, a watch will be added on each subdirectory in *path*. If *auto_add* is True, a watch will be added automatically on newly created subdirectories in *path*.
Watch watches *path* for event types in *event_map* and execute the corresponding task(s). If *rec* is True, a watch will be added on each subdirectory in *path*. If *auto_add* is True, a watch will be added automatically on newly created subdirectories in *path*.
```python
watch = Watch(path="/tmp", event_map=event_map, rec=False, auto_add=False)
```
@@ -122,7 +132,6 @@ watch = Watch(path="/src_path", event_map=event_map, rec=True, auto_add=True)
# note that shutdown_timeout should be greater than the greatest scheduler delay,
# otherwise pending tasks may get cancelled during shutdown.
pyinotifyd_config = PyinotifydConfig(
watches=[watch], loglevel=logging.INFO, shutdown_timeout=35)
```