/
listener.py
executable file
·88 lines (68 loc) · 2.92 KB
/
listener.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
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf8 -*-
'''
Created on 14 oct. 2013
@author: acordier
'''
from __future__ import unicode_literals
import pyinotify
import logging
from connector import NxConnector
from fileutils import FileUtils
import sys
import functools
import os
class EventHandler(pyinotify.ProcessEvent):
def __init__(self, connector, ocr, mapper):
self.connector=connector
self.ocr=ocr
self.mapper=mapper
self.logger = logging.getLogger('nxpush')
self.logger.debug('init %s' %self.__class__.__name__)
def process_IN_CREATE(self, event):
path = event.pathname
self.logger.debug('File path: '+ path)
if not os.path.isfile(path):
return
if FileUtils.isFilePart(path):
return
index = path.rfind('/')
self.connector.setNxPath(self.mapper[path[:index]])
try:
path = self.ocr.doOcr(path)
except Exception, e:
self.logger.error('OCR subprocess failed: '+ str(e))
path = path.rstrip()
self.connector.upload(path)
self.logger.info('%s processed' %path)
def process_IN_DELETE(self, event):
self.logger.debug("Removing:", event.pathname)
def process_IN_MOVE_SELF(self, event):
self.logger.debug("Move self:", event.pathname)
def process_IN_MODIFY(self, event):
self.logger.debug("Modify:", event.pathname)
def process_IN_OPEN(self, event):
self.logger.debug("Open:", event.pathname)
def process_IN_ACCESS(self, event):
self.logger.debug("Access:", event.pathname)
def process_IN_ATTRIB(self, event):
self.logger.debug("Attrib:", event.pathname)
def process_IN_CLOSE_NOWRITE(self, event):
self.logger.debug("Close no write:", event.pathname)
def process_IN_MASK_ADD(self, event):
self.logger.debug("Mask add:", event.pathname)
def setConnector(self, connector):
self.connector = connector
class Listener:
def __init__(self, paths, eventHandler):
self.paths=paths
self.watchManager = pyinotify.WatchManager() # Watch Manager
self.mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE # watched events
self.watchDescDict = self.watchManager.add_watch(self.paths, self.mask, rec=True, auto_add=True) # should put a list of pathes here
self.notifier = pyinotify.Notifier(self.watchManager, eventHandler)
self.logger = logging.getLogger('nxpush')
def on_loop(self, notifier):
self.logger.debug(' notifier loop') # use in case of improvement >> caching ?
def listen(self):
on_loop_func = functools.partial(self.on_loop)
# self.notifier.loop(daemonize=True, callback=on_loop_func, pid_file='/var/run/nxpush.pid', stdout='stdout.log')
self.notifier.loop(callback=on_loop_func)