forked from platypush/platypush
parent
1843ab224b
commit
5823dd0e21
8 changed files with 62 additions and 56 deletions
|
@ -18,7 +18,6 @@ Backends
|
||||||
platypush/backend/gps.rst
|
platypush/backend/gps.rst
|
||||||
platypush/backend/http.rst
|
platypush/backend/http.rst
|
||||||
platypush/backend/kafka.rst
|
platypush/backend/kafka.rst
|
||||||
platypush/backend/log.http.rst
|
|
||||||
platypush/backend/mail.rst
|
platypush/backend/mail.rst
|
||||||
platypush/backend/midi.rst
|
platypush/backend/midi.rst
|
||||||
platypush/backend/music.mopidy.rst
|
platypush/backend/music.mopidy.rst
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
``log.http``
|
|
||||||
==============================
|
|
||||||
|
|
||||||
.. automodule:: platypush.backend.log.http
|
|
||||||
:members:
|
|
5
docs/source/platypush/plugins/log.http.rst
Normal file
5
docs/source/platypush/plugins/log.http.rst
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
``log.http``
|
||||||
|
============
|
||||||
|
|
||||||
|
.. automodule:: platypush.plugins.log.http
|
||||||
|
:members:
|
|
@ -58,6 +58,7 @@ Plugins
|
||||||
platypush/plugins/lcd.i2c.rst
|
platypush/plugins/lcd.i2c.rst
|
||||||
platypush/plugins/light.hue.rst
|
platypush/plugins/light.hue.rst
|
||||||
platypush/plugins/linode.rst
|
platypush/plugins/linode.rst
|
||||||
|
platypush/plugins/log.http.rst
|
||||||
platypush/plugins/logger.rst
|
platypush/plugins/logger.rst
|
||||||
platypush/plugins/luma.oled.rst
|
platypush/plugins/luma.oled.rst
|
||||||
platypush/plugins/mail.imap.rst
|
platypush/plugins/mail.imap.rst
|
||||||
|
|
|
@ -8,33 +8,6 @@ from .entities.handlers import EventHandler
|
||||||
from .entities.resources import MonitoredResource, MonitoredPattern, MonitoredRegex
|
from .entities.resources import MonitoredResource, MonitoredPattern, MonitoredRegex
|
||||||
|
|
||||||
|
|
||||||
def event_handler_from_resource(
|
|
||||||
resource: Union[str, Dict[str, Any], MonitoredResource]
|
|
||||||
) -> Optional[EventHandler]:
|
|
||||||
"""
|
|
||||||
Create a file system event handler from a string, dictionary or
|
|
||||||
``MonitoredResource`` resource.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if isinstance(resource, str):
|
|
||||||
res = MonitoredResource(resource)
|
|
||||||
elif isinstance(resource, dict):
|
|
||||||
if 'regexes' in resource or 'ignore_regexes' in resource:
|
|
||||||
res = MonitoredRegex(**resource)
|
|
||||||
elif (
|
|
||||||
'patterns' in resource
|
|
||||||
or 'ignore_patterns' in resource
|
|
||||||
or 'ignore_directories' in resource
|
|
||||||
):
|
|
||||||
res = MonitoredPattern(**resource)
|
|
||||||
else:
|
|
||||||
res = MonitoredResource(**resource)
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
return EventHandler.from_resource(res)
|
|
||||||
|
|
||||||
|
|
||||||
class FileMonitorPlugin(RunnablePlugin):
|
class FileMonitorPlugin(RunnablePlugin):
|
||||||
"""
|
"""
|
||||||
A plugin to monitor changes to files and directories.
|
A plugin to monitor changes to files and directories.
|
||||||
|
@ -120,16 +93,45 @@ class FileMonitorPlugin(RunnablePlugin):
|
||||||
|
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self._observer = Observer()
|
self._observer = Observer()
|
||||||
|
self.paths = set()
|
||||||
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
handler = event_handler_from_resource(path)
|
handler = self.event_handler_from_resource(path)
|
||||||
if not handler:
|
if not handler:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
self.paths.add(handler.resource.path)
|
||||||
self._observer.schedule(
|
self._observer.schedule(
|
||||||
handler, handler.resource.path, recursive=handler.resource.recursive
|
handler, handler.resource.path, recursive=handler.resource.recursive
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def event_handler_from_resource(
|
||||||
|
resource: Union[str, Dict[str, Any], MonitoredResource]
|
||||||
|
) -> Optional[EventHandler]:
|
||||||
|
"""
|
||||||
|
Create a file system event handler from a string, dictionary or
|
||||||
|
``MonitoredResource`` resource.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if isinstance(resource, str):
|
||||||
|
res = MonitoredResource(resource)
|
||||||
|
elif isinstance(resource, dict):
|
||||||
|
if 'regexes' in resource or 'ignore_regexes' in resource:
|
||||||
|
res = MonitoredRegex(**resource)
|
||||||
|
elif (
|
||||||
|
'patterns' in resource
|
||||||
|
or 'ignore_patterns' in resource
|
||||||
|
or 'ignore_directories' in resource
|
||||||
|
):
|
||||||
|
res = MonitoredPattern(**resource)
|
||||||
|
else:
|
||||||
|
res = MonitoredResource(**resource)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return EventHandler.from_resource(res)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self._observer.stop()
|
self._observer.stop()
|
||||||
self._observer.join()
|
self._observer.join()
|
||||||
|
|
|
@ -5,10 +5,10 @@ import re
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
from threading import RLock
|
from threading import RLock
|
||||||
from typing import List, Optional, Iterable
|
from typing import Optional, Iterable
|
||||||
|
|
||||||
from platypush.backend.file.monitor import (
|
from platypush.plugins.file.monitor import (
|
||||||
FileMonitorBackend,
|
FileMonitorPlugin,
|
||||||
EventHandler,
|
EventHandler,
|
||||||
MonitoredResource,
|
MonitoredResource,
|
||||||
)
|
)
|
||||||
|
@ -152,28 +152,32 @@ class LogEventHandler(EventHandler):
|
||||||
return HttpLogEvent(logfile=file, **info)
|
return HttpLogEvent(logfile=file, **info)
|
||||||
|
|
||||||
|
|
||||||
class LogHttpBackend(FileMonitorBackend):
|
class LogHttpPlugin(FileMonitorPlugin):
|
||||||
"""
|
"""
|
||||||
This backend can be used to monitor one or more HTTP log files (tested on Apache and Nginx) and trigger events
|
This plugin can be used to monitor one or more HTTP log files (tested on
|
||||||
whenever a new log line is added.
|
Apache and Nginx) and trigger events whenever a new log line is added.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class EventHandlerFactory:
|
def __init__(
|
||||||
@staticmethod
|
self, paths: Iterable[str], log_files: Optional[Iterable[str]] = None, **kwargs
|
||||||
def from_resource(resource: str) -> LogEventHandler:
|
):
|
||||||
resource = MonitoredResource(resource)
|
"""
|
||||||
return LogEventHandler.from_resource(resource)
|
:param paths: List of log files to be monitored.
|
||||||
|
"""
|
||||||
|
if log_files:
|
||||||
|
self.logger.warning(
|
||||||
|
'The log_files parameter is deprecated, use paths instead'
|
||||||
|
)
|
||||||
|
|
||||||
def __init__(self, log_files: List[str], **kwargs):
|
paths = {os.path.expanduser(log) for log in {*paths, *(log_files or [])}}
|
||||||
"""
|
directories = {os.path.dirname(log) for log in paths}
|
||||||
:param log_files: List of log files to be monitored.
|
|
||||||
"""
|
|
||||||
self.log_files = {os.path.expanduser(log) for log in log_files}
|
|
||||||
directories = {os.path.dirname(log) for log in self.log_files}
|
|
||||||
super().__init__(paths=directories, **kwargs)
|
super().__init__(paths=directories, **kwargs)
|
||||||
|
|
||||||
# noinspection PyProtectedMember
|
|
||||||
handlers = self._observer._handlers
|
handlers = self._observer._handlers
|
||||||
for hndls in handlers.values():
|
for hndls in handlers.values():
|
||||||
for hndl in hndls:
|
for hndl in hndls:
|
||||||
hndl.monitor_files(self.log_files)
|
hndl.monitor_files(paths)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def event_handler_from_resource(resource: str) -> LogEventHandler:
|
||||||
|
return LogEventHandler.from_resource(MonitoredResource(resource))
|
|
@ -1,6 +1,6 @@
|
||||||
manifest:
|
manifest:
|
||||||
events:
|
events:
|
||||||
platypush.message.event.log.http.HttpLogEvent: when a new log line is created.
|
- platypush.message.event.log.http.HttpLogEvent
|
||||||
install:
|
install:
|
||||||
apk:
|
apk:
|
||||||
- py3-watchdog
|
- py3-watchdog
|
||||||
|
@ -12,5 +12,5 @@ manifest:
|
||||||
- python-watchdog
|
- python-watchdog
|
||||||
pip:
|
pip:
|
||||||
- watchdog
|
- watchdog
|
||||||
package: platypush.backend.log.http
|
package: platypush.plugins.log.http
|
||||||
type: backend
|
type: plugin
|
Loading…
Reference in a new issue