change README.md
This commit is contained in:
29
README.md
29
README.md
@@ -60,3 +60,32 @@ pyinotifyd expects an instance of PyinotifydConfig named **pyinotifyd_config** t
|
|||||||
```python
|
```python
|
||||||
pyinotifyd_config = PyinotifydConfig(watches=[watch], loglevel=logging.INFO, shutdown_timeout=30)
|
pyinotifyd_config = PyinotifydConfig(watches=[watch], loglevel=logging.INFO, shutdown_timeout=30)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
## Schedule Python task for all events
|
||||||
|
```python
|
||||||
|
async def custom_task(event, task_id):
|
||||||
|
logging.info(f"{task_id}: execute example task: {event}")
|
||||||
|
|
||||||
|
s = TaskScheduler(task=custom_task, files=True, dirs=True)
|
||||||
|
event_map = EventMap(default_task=custom_task)
|
||||||
|
watch = Watch(path="/tmp", event_map=event_map, rec=True, auto_add=True)
|
||||||
|
pyinotifyd_config = PyinotifydConfig(
|
||||||
|
watches=[watch], loglevel=logging.INFO, shutdown_timeout=5)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Schedule Shell commands for specific events on files
|
||||||
|
```python
|
||||||
|
s = ShellScheduler(
|
||||||
|
cmd="/usr/local/sbin/task.sh {pathname}", files=True, dirs=False)
|
||||||
|
event_map = EventMap({"IN_WRITE_CLOSE": s.schedule,
|
||||||
|
"IN_DELETE": s.schedule})
|
||||||
|
watch = Watch(path="/tmp", event_map=event_map, rec=True, auto_add=True)
|
||||||
|
pyinotifyd_config = PyinotifydConfig(
|
||||||
|
watches=[watch], loglevel=logging.INFO, shutdown_timeout=5)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Keep copy of new files
|
||||||
|
```python
|
||||||
|
```
|
||||||
|
|||||||
@@ -174,13 +174,19 @@ class EventMap:
|
|||||||
flags = {**pyinotify.EventsCodes.OP_FLAGS,
|
flags = {**pyinotify.EventsCodes.OP_FLAGS,
|
||||||
**pyinotify.EventsCodes.EVENT_FLAGS}
|
**pyinotify.EventsCodes.EVENT_FLAGS}
|
||||||
|
|
||||||
def __init__(self, event_map=None):
|
def __init__(self, event_map=None, default_task=None):
|
||||||
self._map = {}
|
self._map = {}
|
||||||
|
if default_task is not None:
|
||||||
|
assert callable(default_task), \
|
||||||
|
f"default_task: expected callable, got {type(default_task)}"
|
||||||
|
for flag in EventMap.flags:
|
||||||
|
self.set(flag, default_task)
|
||||||
|
|
||||||
if event_map is not None:
|
if event_map is not None:
|
||||||
assert isinstance(event_map, dict), \
|
assert isinstance(event_map, dict), \
|
||||||
f"event_map: expected {type(dict)}, got {type(event_map)}"
|
f"event_map: expected {type(dict)}, got {type(event_map)}"
|
||||||
for flag, func in event_map.items():
|
for flag, task in event_map.items():
|
||||||
self.set(flag, func)
|
self.set(flag, task)
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
return self._map
|
return self._map
|
||||||
|
|||||||
Reference in New Issue
Block a user