forked from dylanninin/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
45 lines (39 loc) · 1.35 KB
/
__init__.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Inotify tools for entries
version 1.0
history:
2013-6-19 [email protected] init
"""
from config import blogconfig as config
from service import EntryService
entryService = EntryService()
# pyinotify is optional when just have a try
try:
import pyinotify
except ImportError:
print 'Could not import pyinotify'
else:
class EntryEventHandler(pyinotify.ProcessEvent):
"""
EntryEventHandler monitor entries added, modified or deleted
"""
def process_default(self, event):
"""
处理监听的业务逻辑
:param event:
:return:
"""
mask_add = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_MOVED_TO | pyinotify.IN_MOVE_SELF
mask_del = pyinotify.IN_DELETE | pyinotify.IN_DELETE_SELF | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVE_SELF
if event.mask & mask_add:
entryService.add_entry(True, event.pathname)
if event.mask & mask_del:
entryService.delete_entry(event.pathname)
wm = pyinotify.WatchManager()
mask = pyinotify.ALL_EVENTS
path = config.entry_dir
notifier = pyinotify.ThreadedNotifier(wm, EntryEventHandler())
wdd = wm.add_watch(path, mask, rec=True)
notifier.start()