reformat examples in README.md and docs/config.py
This commit is contained in:
180
README.md
180
README.md
@@ -31,18 +31,20 @@ With *auto_create* set to True, possibly missing subdirectories in *dst_re* are
|
|||||||
Set the mode of moved/copied files/directories with *filemode* and *dirmode*. Ownership of moved/copied files/directories is set with *user* and *group*. Mode and ownership is also set to automatically created subdirectories.
|
Set the mode of moved/copied files/directories with *filemode* and *dirmode*. Ownership of moved/copied files/directories is set with *user* and *group*. Mode and ownership is also set to automatically created subdirectories.
|
||||||
Use *logname* in log messages.
|
Use *logname* in log messages.
|
||||||
```python
|
```python
|
||||||
rule = Rule(action="move",
|
rule = Rule(
|
||||||
src_re="^/src_path/(?P<path>.*).to_move$",
|
action="move",
|
||||||
dst_re="/dst_path/\g<path>",
|
src_re="^/src_path/(?P<path>.*).to_move$",
|
||||||
auto_create=False,
|
dst_re="/dst_path/\g<path>",
|
||||||
rec=False,
|
auto_create=False,
|
||||||
filemode=None,
|
rec=False,
|
||||||
dirmode=None,
|
filemode=None,
|
||||||
user=None,
|
dirmode=None,
|
||||||
group=None)
|
user=None,
|
||||||
|
group=None)
|
||||||
|
|
||||||
fm = FileManager(rules=[rule],
|
fm = FileManager(
|
||||||
logname="FileManager")
|
rules=[rule],
|
||||||
|
logname="FileManager")
|
||||||
```
|
```
|
||||||
FileManager provides a task **fm.task**.
|
FileManager provides a task **fm.task**.
|
||||||
|
|
||||||
@@ -53,11 +55,12 @@ pyinotifyd has different schedulers to schedule tasks with an optional delay. Th
|
|||||||
TaskScheduler schedules *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.
|
||||||
Use *logname* in log messages. All arguments except for *task* are optional.
|
Use *logname* in log messages. All arguments except for *task* are optional.
|
||||||
```python
|
```python
|
||||||
s = TaskScheduler(task=task,
|
s = TaskScheduler(
|
||||||
files=True,
|
task=task,
|
||||||
dirs=False,
|
files=True,
|
||||||
delay=0,
|
dirs=False,
|
||||||
logname="TaskScheduler")
|
delay=0,
|
||||||
|
logname="TaskScheduler")
|
||||||
```
|
```
|
||||||
TaskScheduler provides two tasks which can be bound to an event in an event map.
|
TaskScheduler provides two tasks which can be bound to an event in an event map.
|
||||||
* **s.schedule**
|
* **s.schedule**
|
||||||
@@ -74,8 +77,9 @@ s1 = ShellScheduler(cmd="/usr/local/bin/task.sh {maskname} {pathname} {src_pathn
|
|||||||
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.
|
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:
|
This is an example:
|
||||||
```python
|
```python
|
||||||
event_map = EventMap({"IN_CLOSE_NOWRITE": [s.schedule, s1.schedule],
|
event_map = EventMap({
|
||||||
"IN_CLOSE_WRITE": s.schedule})
|
"IN_CLOSE_NOWRITE": [s.schedule, s1.schedule],
|
||||||
|
"IN_CLOSE_WRITE": s.schedule})
|
||||||
```
|
```
|
||||||
The following event types are available:
|
The following event types are available:
|
||||||
* **IN_ACCESS**: a file was accessed
|
* **IN_ACCESS**: a file was accessed
|
||||||
@@ -97,18 +101,20 @@ The following event types are available:
|
|||||||
### Watches
|
### Watches
|
||||||
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*.
|
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
|
```python
|
||||||
watch = Watch(path="/tmp",
|
watch = Watch(
|
||||||
event_map=event_map,
|
path="/tmp",
|
||||||
rec=False,
|
event_map=event_map,
|
||||||
auto_add=False)
|
rec=False,
|
||||||
|
auto_add=False)
|
||||||
```
|
```
|
||||||
|
|
||||||
### PyinotifydConfig
|
### PyinotifydConfig
|
||||||
pyinotifyd expects an instance of PyinotifydConfig named **pyinotifyd_config** that holds its config options. The options are a list of *watches*, the *loglevel* (see https://docs.python.org/3/library/logging.html#levels) and the *shutdown_timeout*. pyinotifyd will wait *shutdown_timeout* seconds for pending tasks to complete during shutdown.
|
pyinotifyd expects an instance of PyinotifydConfig named **pyinotifyd_config** that holds its config options. The options are a list of *watches*, the *loglevel* (see https://docs.python.org/3/library/logging.html#levels) and the *shutdown_timeout*. pyinotifyd will wait *shutdown_timeout* seconds for pending tasks to complete during shutdown.
|
||||||
```python
|
```python
|
||||||
pyinotifyd_config = PyinotifydConfig(watches=[watch],
|
pyinotifyd_config = PyinotifydConfig(
|
||||||
loglevel=logging.INFO,
|
watches=[watch],
|
||||||
shutdown_timeout=30)
|
loglevel=logging.INFO,
|
||||||
|
shutdown_timeout=30)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Autostart
|
### Autostart
|
||||||
@@ -128,80 +134,98 @@ systemctl start pyinotifyd.service
|
|||||||
async def task(event, task_id):
|
async def task(event, task_id):
|
||||||
logging.info(f"{task_id}: execute example task: {event}")
|
logging.info(f"{task_id}: execute example task: {event}")
|
||||||
|
|
||||||
s = TaskScheduler(task=task,
|
s = TaskScheduler(
|
||||||
files=True,
|
task=task,
|
||||||
dirs=True)
|
files=True,
|
||||||
|
dirs=True)
|
||||||
|
|
||||||
event_map = EventMap(default_task=s.schedule)
|
event_map = EventMap(
|
||||||
|
default_task=s.schedule)
|
||||||
|
|
||||||
watch = Watch(path="/tmp",
|
watch = Watch(
|
||||||
event_map=event_map,
|
path="/tmp",
|
||||||
rec=True,
|
event_map=event_map,
|
||||||
auto_add=True)
|
rec=True,
|
||||||
|
auto_add=True)
|
||||||
|
|
||||||
pyinotifyd_config = PyinotifydConfig(watches=[watch],
|
pyinotifyd_config = PyinotifydConfig(
|
||||||
loglevel=logging.INFO,
|
watches=[watch],
|
||||||
shutdown_timeout=5)
|
loglevel=logging.INFO,
|
||||||
|
shutdown_timeout=5)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Schedule Shell commands for specific events on files
|
### Schedule Shell commands for specific events on files
|
||||||
```python
|
```python
|
||||||
s = ShellScheduler(cmd="/usr/local/sbin/task.sh {pathname}", files=True, dirs=False)
|
s = ShellScheduler(
|
||||||
|
cmd="/usr/local/sbin/task.sh {pathname}",
|
||||||
|
files=True,
|
||||||
|
dirs=False)
|
||||||
|
|
||||||
event_map = EventMap({"IN_WRITE_CLOSE": s.schedule})
|
event_map = EventMap(
|
||||||
|
{"IN_WRITE_CLOSE": s.schedule})
|
||||||
|
|
||||||
watch = Watch(path="/tmp",
|
watch = Watch(
|
||||||
event_map=event_map,
|
path="/tmp",
|
||||||
rec=True,
|
event_map=event_map,
|
||||||
auto_add=True)
|
rec=True,
|
||||||
|
auto_add=True)
|
||||||
|
|
||||||
pyinotifyd_config = PyinotifydConfig(watches=[watch],
|
pyinotifyd_config = PyinotifydConfig(
|
||||||
loglevel=logging.INFO,
|
watches=[watch],
|
||||||
shutdown_timeout=5)
|
loglevel=logging.INFO,
|
||||||
|
shutdown_timeout=5)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Move, copy or delete newly created files after a delay
|
### Move, copy or delete newly created files after a delay
|
||||||
```python
|
```python
|
||||||
move_rule = Rule(action="move",
|
move_rule = Rule(
|
||||||
src_re="^/src_path/(?P<path>.*)\.to_move$",
|
action="move",
|
||||||
dst_re="/dst_path/\g<path>",
|
src_re="^/src_path/(?P<path>.*)\.to_move$",
|
||||||
auto_create=True,
|
dst_re="/dst_path/\g<path>",
|
||||||
filemode=0o644,
|
auto_create=True,
|
||||||
dirmode=0o755)
|
filemode=0o644,
|
||||||
|
dirmode=0o755)
|
||||||
|
|
||||||
copy_rule = Rule(action="copy",
|
copy_rule = Rule(
|
||||||
src_re="^/src_path/(?P<path>.*)\.to_copy$",
|
action="copy",
|
||||||
dst_re="/dst_path/\g<path>",
|
src_re="^/src_path/(?P<path>.*)\.to_copy$",
|
||||||
auto_create=True,
|
dst_re="/dst_path/\g<path>",
|
||||||
filemode=0o644,
|
auto_create=True,
|
||||||
dirmode=0o755)
|
filemode=0o644,
|
||||||
|
dirmode=0o755)
|
||||||
|
|
||||||
delete_rule = Rule(action="delete",
|
delete_rule = Rule(
|
||||||
src_re="^/src_path/(?P<path>.*)\.to_delete$",
|
action="delete",
|
||||||
rec=False)
|
src_re="^/src_path/(?P<path>.*)\.to_delete$",
|
||||||
|
rec=False)
|
||||||
|
|
||||||
fm = FileManager(rules=[move_rule, copy_rule, delete_rule])
|
fm = FileManager(
|
||||||
|
rules=[move_rule, copy_rule, delete_rule])
|
||||||
|
|
||||||
s = TaskScheduler(task=fm.task,
|
s = TaskScheduler(
|
||||||
delay=30,
|
task=fm.task,
|
||||||
files=True,
|
delay=30,
|
||||||
dirs=False)
|
files=True,
|
||||||
|
dirs=False)
|
||||||
|
|
||||||
event_map = EventMap({"IN_CLOSE_WRITE": s.schedule,
|
event_map = EventMap({
|
||||||
"IN_DELETE": s.cancel,
|
"IN_CLOSE_WRITE": s.schedule,
|
||||||
"IN_DELETE_SELF": s.cancel,
|
"IN_DELETE": s.cancel,
|
||||||
"IN_MODIFY": s.cancel,
|
"IN_DELETE_SELF": s.cancel,
|
||||||
"IN_MOVED_TO": s.schedule,
|
"IN_MODIFY": s.cancel,
|
||||||
"IN_UNMOUNT": s.cancel})
|
"IN_MOVED_TO": s.schedule,
|
||||||
|
"IN_UNMOUNT": s.cancel})
|
||||||
|
|
||||||
watch = Watch(path="/src_path",
|
watch = Watch(
|
||||||
event_map=event_map,
|
path="/src_path",
|
||||||
rec=True,
|
event_map=event_map,
|
||||||
auto_add=True)
|
rec=True,
|
||||||
|
auto_add=True)
|
||||||
|
|
||||||
# note that shutdown_timeout should be greater than the greatest scheduler delay,
|
# note that shutdown_timeout should be greater than the greatest scheduler delay,
|
||||||
# otherwise pending tasks may get cancelled during shutdown.
|
# otherwise pending tasks may get cancelled during shutdown.
|
||||||
pyinotifyd_config = PyinotifydConfig(watches=[watch],
|
pyinotifyd_config = PyinotifydConfig(
|
||||||
loglevel=logging.INFO,
|
watches=[watch],
|
||||||
shutdown_timeout=35)
|
loglevel=logging.INFO,
|
||||||
|
shutdown_timeout=35)
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -7,19 +7,34 @@
|
|||||||
#async def custom_task(event, task_id):
|
#async def custom_task(event, task_id):
|
||||||
# logging.info(f"{task_id}: execute example task: {event}")
|
# logging.info(f"{task_id}: execute example task: {event}")
|
||||||
#
|
#
|
||||||
#s = TaskScheduler(task=custom_task, files=True, dirs=False)
|
#s = TaskScheduler(
|
||||||
|
# task=custom_task,
|
||||||
|
# files=True,
|
||||||
|
# dirs=False)
|
||||||
|
|
||||||
|
|
||||||
#####################################################
|
#####################################################
|
||||||
# Example usage of TaskScheduler with FileManager #
|
# Example usage of TaskScheduler with FileManager #
|
||||||
#####################################################
|
#####################################################
|
||||||
|
|
||||||
#rules=[{"action": "move",
|
#rules=[{
|
||||||
# "src_re": r"^(?P<path>.*)",
|
# "action": "move",
|
||||||
# "dst_re": r"\g<path>.processed",
|
# "src_re": r"^(?P<path>.*)",
|
||||||
# "auto_create": True}]
|
# "dst_re": r"\g<path>.processed",
|
||||||
#fm = FileManager(rules=rules)
|
# "auto_create": True,
|
||||||
#s = TaskScheduler(task=fm.task, delay=10, files=True, dirs=False)
|
# "filemode": 0o755,
|
||||||
|
# "dirmode": 0o644,
|
||||||
|
# "user": "root",
|
||||||
|
# "goup": "root"}]
|
||||||
|
#
|
||||||
|
#fm = FileManager(
|
||||||
|
# rules=rules)
|
||||||
|
#
|
||||||
|
#s = TaskScheduler(
|
||||||
|
# task=fm.task,
|
||||||
|
# delay=10,
|
||||||
|
# files=True,
|
||||||
|
# dirs=False)
|
||||||
|
|
||||||
|
|
||||||
#####################################
|
#####################################
|
||||||
@@ -27,29 +42,36 @@
|
|||||||
#####################################
|
#####################################
|
||||||
|
|
||||||
#cmd = "/usr/local/bin/task.sh {maskname} {pathname} {src_pathname}"
|
#cmd = "/usr/local/bin/task.sh {maskname} {pathname} {src_pathname}"
|
||||||
#s = ShellScheduler(cmd=cmd)
|
#s = ShellScheduler(
|
||||||
|
# cmd=cmd)
|
||||||
|
|
||||||
|
|
||||||
###################
|
###################
|
||||||
# Example watch #
|
# Example watch #
|
||||||
###################
|
###################
|
||||||
|
|
||||||
#event_map = EventMap({"IN_ACCESS": None,
|
#event_map = EventMap({
|
||||||
# "IN_ATTRIB": None,
|
# "IN_ACCESS": None,
|
||||||
# "IN_CLOSE_NOWRITE": None,
|
# "IN_ATTRIB": None,
|
||||||
# "IN_CLOSE_WRITE": s.schedule,
|
# "IN_CLOSE_NOWRITE": None,
|
||||||
# "IN_CREATE": None,
|
# "IN_CLOSE_WRITE": s.schedule,
|
||||||
# "IN_DELETE": s.cancel,
|
# "IN_CREATE": None,
|
||||||
# "IN_DELETE_SELF": s.cancel,
|
# "IN_DELETE": s.cancel,
|
||||||
# "IN_IGNORED": None,
|
# "IN_DELETE_SELF": s.cancel,
|
||||||
# "IN_MODIFY": s.cancel,
|
# "IN_IGNORED": None,
|
||||||
# "IN_MOVE_SELF": None,
|
# "IN_MODIFY": s.cancel,
|
||||||
# "IN_MOVED_FROM": s.cancel,
|
# "IN_MOVE_SELF": None,
|
||||||
# "IN_MOVED_TO": s.schedule,
|
# "IN_MOVED_FROM": s.cancel,
|
||||||
# "IN_OPEN": None,
|
# "IN_MOVED_TO": s.schedule,
|
||||||
# "IN_Q_OVERFLOW": None,
|
# "IN_OPEN": None,
|
||||||
# "IN_UNMOUNT": s.cancel})
|
# "IN_Q_OVERFLOW": None,
|
||||||
#watch = Watch(path="/tmp", event_map=event_map, rec=True, auto_add=True)
|
# "IN_UNMOUNT": s.cancel})
|
||||||
|
#
|
||||||
|
#watch = Watch(
|
||||||
|
# path="/tmp",
|
||||||
|
# event_map=event_map,
|
||||||
|
# rec=True,
|
||||||
|
# auto_add=True)
|
||||||
|
|
||||||
|
|
||||||
###############################
|
###############################
|
||||||
@@ -57,4 +79,6 @@
|
|||||||
###############################
|
###############################
|
||||||
|
|
||||||
#pyinotifyd_config = PyinotifydConfig(
|
#pyinotifyd_config = PyinotifydConfig(
|
||||||
# watches=[watch], loglevel=logging.INFO, shutdown_timeout=30)
|
# watches=[watch],
|
||||||
|
# loglevel=logging.INFO,
|
||||||
|
# shutdown_timeout=30)
|
||||||
|
|||||||
Reference in New Issue
Block a user