File: print.py

package info (click to toggle)
aionotify 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 200 kB
  • sloc: python: 452; makefile: 21
file content (75 lines) | stat: -rwxr-xr-x 2,008 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# Copyright (c) 2016 The aionotify project
# This code is distributed under the two-clause BSD License.


import aionotify
import argparse
import asyncio
import logging
import signal


class Example:
    def __init__(self):
        self.loop = None
        self.watcher = None
        self.task = None

    def prepare(self, path):
        self.watcher = aionotify.Watcher()
        self.watcher.watch(path, aionotify.Flags.MODIFY | aionotify.Flags.CREATE | aionotify.Flags.DELETE)

    async def _run(self, max_events):
        await self.watcher.setup(self.loop)
        for _i in range(max_events):
            event = await self.watcher.get_event()
            print(event.name, aionotify.Flags.parse(event.flags))
        self.shutdown()

    def run(self, loop, max_events):
        self.loop = loop
        self.task = loop.create_task(self._run(max_events))

    def shutdown(self):
        self.watcher.close()
        if self.task is not None:
            self.task.cancel()
        self.loop.stop()


def setup_signal_handlers(loop, example):
    for sig in [signal.SIGINT, signal.SIGTERM]:
        loop.add_signal_handler(sig, example.shutdown)


def main(args):
    if args.debug:
        logger = logging.getLogger('asyncio')
        logger.setLevel(logging.DEBUG)
        logger.addHandler(logging.StreamHandler())

    example = Example()
    example.prepare(args.path)

    loop = asyncio.get_event_loop()
    if args.debug:
        loop.set_debug(True)

    setup_signal_handlers(loop, example)
    example.run(loop, args.events)

    try:
        loop.run_forever()
    finally:
        loop.close()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('path', help="Path to watch")
    parser.add_argument('--events', default=10, type=int, help="Number of arguments before shutdown")
    parser.add_argument('-d', '--debug', action='store_true', help="Enable asyncio debugging.")

    args = parser.parse_args()
    main(args)