Skip to content

Commit

Permalink
[#292] Converted backend.log.http to a runnable plugin.
Browse files Browse the repository at this point in the history
Closes: #292
  • Loading branch information
blacklight committed Dec 4, 2023
1 parent 1843ab2 commit 5823dd0
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 55 deletions.
1 change: 0 additions & 1 deletion docs/source/backends.rst
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions docs/source/platypush/backend/log.http.rst

This file was deleted.

5 changes: 5 additions & 0 deletions docs/source/platypush/plugins/log.http.rst
@@ -0,0 +1,5 @@
``log.http``
============

.. automodule:: platypush.plugins.log.http
:members:
1 change: 1 addition & 0 deletions docs/source/plugins.rst
Expand Up @@ -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
Expand Down
58 changes: 30 additions & 28 deletions platypush/plugins/file/monitor/__init__.py
Expand Up @@ -8,33 +8,6 @@
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.
Expand Down Expand Up @@ -120,16 +93,45 @@ def __init__(

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()
Expand Down
File renamed without changes.
Expand Up @@ -5,10 +5,10 @@
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,
)
Expand Down Expand Up @@ -152,28 +152,32 @@ def _build_event(cls, file: str, line: str) -> Optional[HttpLogEvent]:
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, log_files: List[str], **kwargs):
def __init__(
self, paths: Iterable[str], log_files: Optional[Iterable[str]] = None, **kwargs
):
"""
:param log_files: List of log files to be monitored.
:param paths: 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}
if log_files:
self.logger.warning(
'The log_files parameter is deprecated, use paths instead'
)

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
Expand All @@ -12,5 +12,5 @@ manifest:
- python-watchdog
pip:
- watchdog
package: platypush.backend.log.http
type: backend
package: platypush.plugins.log.http
type: plugin

0 comments on commit 5823dd0

Please sign in to comment.