-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfollower.py
75 lines (61 loc) · 2.21 KB
/
follower.py
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
# Copyright 2021 kloeckner.i GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from queue import Queue
from watchdog.observers import Observer
from watchdog.events import (
EVENT_TYPE_DELETED,
EVENT_TYPE_MODIFIED,
FileSystemEventHandler,
)
class Follower:
def __init__(self, filename):
self.filename = filename
self.file = open(filename, "r")
self.fs_events = Queue()
self.fs_event_observer = Observer()
self.fs_event_observer.schedule(QueuedEventHandler(self), filename)
def __enter__(self):
self.fs_event_observer.start()
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
self.fs_event_observer.stop()
self.file.close()
def lines(self):
# Read all existing lines
while True:
line = self.file.readline()
if not line:
break
yield line.rstrip("\n")
# Wait for new lines
self.fs_events.empty()
while True:
event = self.fs_events.get()
if event.event_type == EVENT_TYPE_DELETED:
break
elif event.event_type == EVENT_TYPE_MODIFIED:
line = self.file.readline().rstrip("\n")
if not line:
# On some platforms watchdog mistakenly sends a modified event on deletions.
if not os.path.exists(self.filename):
break
continue
yield line
class QueuedEventHandler(FileSystemEventHandler):
def __init__(self, parent):
self._parent = parent
def on_any_event(self, event):
if not event.is_directory:
self._parent.fs_events.put(event)