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/http.rst
|
||||
platypush/backend/kafka.rst
|
||||
platypush/backend/log.http.rst
|
||||
platypush/backend/mail.rst
|
||||
platypush/backend/midi.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/light.hue.rst
|
||||
platypush/plugins/linode.rst
|
||||
platypush/plugins/log.http.rst
|
||||
platypush/plugins/logger.rst
|
||||
platypush/plugins/luma.oled.rst
|
||||
platypush/plugins/mail.imap.rst
|
||||
|
|
|
@ -8,33 +8,6 @@ from .entities.handlers import EventHandler
|
|||
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):
|
||||
"""
|
||||
A plugin to monitor changes to files and directories.
|
||||
|
@ -120,16 +93,45 @@ class FileMonitorPlugin(RunnablePlugin):
|
|||
|
||||
super().__init__(**kwargs)
|
||||
self._observer = Observer()
|
||||
self.paths = set()
|
||||
|
||||
for path in paths:
|
||||
handler = event_handler_from_resource(path)
|
||||
handler = self.event_handler_from_resource(path)
|
||||
if not handler:
|
||||
continue
|
||||
|
||||
self.paths.add(handler.resource.path)
|
||||
self._observer.schedule(
|
||||
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):
|
||||
self._observer.stop()
|
||||
self._observer.join()
|
||||
|
|
|
@ -5,10 +5,10 @@ import re
|
|||
from dataclasses import dataclass
|
||||
from logging import getLogger
|
||||
from threading import RLock
|
||||
from typing import List, Optional, Iterable
|
||||
from typing import Optional, Iterable
|
||||
|
||||
from platypush.backend.file.monitor import (
|
||||
FileMonitorBackend,
|
||||
from platypush.plugins.file.monitor import (
|
||||
FileMonitorPlugin,
|
||||
EventHandler,
|
||||
MonitoredResource,
|
||||
)
|
||||
|
@ -152,28 +152,32 @@ class LogEventHandler(EventHandler):
|
|||
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
|
||||
whenever a new log line is added.
|
||||
This plugin can be used to monitor one or more HTTP log files (tested on
|
||||
Apache and Nginx) and trigger events whenever a new log line is added.
|
||||
"""
|
||||
|
||||
class EventHandlerFactory:
|
||||
@staticmethod
|
||||
def from_resource(resource: str) -> LogEventHandler:
|
||||
resource = MonitoredResource(resource)
|
||||
return LogEventHandler.from_resource(resource)
|
||||
def __init__(
|
||||
self, paths: Iterable[str], log_files: Optional[Iterable[str]] = None, **kwargs
|
||||
):
|
||||
"""
|
||||
: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):
|
||||
"""
|
||||
: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}
|
||||
paths = {os.path.expanduser(log) for log in {*paths, *(log_files or [])}}
|
||||
directories = {os.path.dirname(log) for log in paths}
|
||||
super().__init__(paths=directories, **kwargs)
|
||||
|
||||
# noinspection PyProtectedMember
|
||||
handlers = self._observer._handlers
|
||||
for hndls in handlers.values():
|
||||
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:
|
||||
events:
|
||||
platypush.message.event.log.http.HttpLogEvent: when a new log line is created.
|
||||
- platypush.message.event.log.http.HttpLogEvent
|
||||
install:
|
||||
apk:
|
||||
- py3-watchdog
|
||||
|
@ -12,5 +12,5 @@ manifest:
|
|||
- python-watchdog
|
||||
pip:
|
||||
- watchdog
|
||||
package: platypush.backend.log.http
|
||||
type: backend
|
||||
package: platypush.plugins.log.http
|
||||
type: plugin
|
Loading…
Reference in a new issue