comparison libervia/server/server.py @ 1152:1c23252958ed

server: iNotify support: a new FileWatcher class allows to check modifications in a directory.
author Goffi <goffi@goffi.org>
date Fri, 22 Feb 2019 16:57:37 +0100
parents 49ff1330c9d0
children 94f9d81a475e
comparison
equal deleted inserted replaced
1151:49ff1330c9d0 1152:1c23252958ed
26 import uuid 26 import uuid
27 import urlparse 27 import urlparse
28 import urllib 28 import urllib
29 import time 29 import time
30 from twisted.application import service 30 from twisted.application import service
31 from twisted.internet import reactor, defer 31 from twisted.internet import reactor, defer, inotify
32 from twisted.web import server 32 from twisted.web import server
33 from twisted.web import static 33 from twisted.web import static
34 from twisted.web import resource as web_resource 34 from twisted.web import resource as web_resource
35 from twisted.web import util as web_util 35 from twisted.web import util as web_util
36 from twisted.web import http 36 from twisted.web import http
37 from twisted.web import vhost 37 from twisted.web import vhost
38 from twisted.python.components import registerAdapter 38 from twisted.python.components import registerAdapter
39 from twisted.python import failure 39 from twisted.python import failure
40 from twisted.python import filepath
40 from twisted.words.protocols.jabber import jid 41 from twisted.words.protocols.jabber import jid
41 42
42 from txjsonrpc.web import jsonrpc 43 from txjsonrpc.web import jsonrpc
43 from txjsonrpc import jsonrpclib 44 from txjsonrpc import jsonrpclib
44 45
78 79
79 80
80 # following value are set from twisted.plugins.libervia_server initialise 81 # following value are set from twisted.plugins.libervia_server initialise
81 # (see the comment there) 82 # (see the comment there)
82 DATA_DIR_DEFAULT = OPT_PARAMETERS_BOTH = OPT_PARAMETERS_CFG = coerceDataDir = None 83 DATA_DIR_DEFAULT = OPT_PARAMETERS_BOTH = OPT_PARAMETERS_CFG = coerceDataDir = None
84 DEFAULT_MASK = (inotify.IN_CREATE | inotify.IN_MODIFY | inotify.IN_MOVE_SELF
85 | inotify.IN_MOVED_TO)
86
87
88 class FilesWatcher(object):
89 """Class to check files modifications using iNotify"""
90 _notifier = None
91
92 def __init__(self, host):
93 self.host = host
94
95 @property
96 def notifier(self):
97 if self._notifier == None:
98 notifier = self.__class__._notifier = inotify.INotify()
99 notifier.startReading()
100 return self._notifier
101
102 def watchDir(self, dir_path, callback, mask=DEFAULT_MASK, auto_add=False,
103 recursive=False, **kwargs):
104 log.info(_(u"Watching directory {dir_path}").format(dir_path=dir_path))
105 callbacks = [lambda __, filepath, mask: callback(self.host, filepath,
106 inotify.humanReadableMask(mask), **kwargs)]
107 self.notifier.watch(
108 filepath.FilePath(dir_path), mask=mask, autoAdd=auto_add, recursive=recursive,
109 callbacks=callbacks)
83 110
84 111
85 class LiberviaSession(server.Session): 112 class LiberviaSession(server.Session):
86 sessionTimeout = C.SESSION_TIMEOUT 113 sessionTimeout = C.SESSION_TIMEOUT
87 114
1706 def __init__(self, options): 1733 def __init__(self, options):
1707 self.options = options 1734 self.options = options
1708 self.initialised = defer.Deferred() 1735 self.initialised = defer.Deferred()
1709 self.waiting_profiles = WaitingRequests() # FIXME: should be removed 1736 self.waiting_profiles = WaitingRequests() # FIXME: should be removed
1710 self._main_conf = None 1737 self._main_conf = None
1738 self.files_watcher = FilesWatcher(self)
1711 1739
1712 if self.options["base_url_ext"]: 1740 if self.options["base_url_ext"]:
1713 self.base_url_ext = self.options.pop("base_url_ext") 1741 self.base_url_ext = self.options.pop("base_url_ext")
1714 if self.base_url_ext[-1] != "/": 1742 if self.base_url_ext[-1] != "/":
1715 self.base_url_ext += "/" 1743 self.base_url_ext += "/"