cowbird.monitoring.monitoring

Attributes

AnySettingsContainer

LOGGER

Exceptions

MonitorException

Error indicating that a Monitor cannot be started because of an invalid path or callback.

MonitoringConfigurationException

Exception thrown when the monitoring instance cannot be initialized because of a bad configuration.

Classes

MonitoringStore

Registry for monitoring instances.

HandlerFactory

Create handler instance using handler name.

FSMonitor

Interface being called when something changes on the filesystem.

Monitor

Implementation of the watchdog FileSystemEventHandler class Allows to start/stop directory monitoring and

SingletonMeta

A metaclass that creates a Singleton base class when called.

Monitoring

Class handling file system monitoring and registering listeners.

Functions

get_db(→ mongodb.MongoDatabase)

Obtains the database connection from configured application settings.

get_logger(→ logging.Logger)

Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when

Module Contents

cowbird.monitoring.monitoring.get_db(container: cowbird.typedefs.AnySettingsContainer, reset_connection: bool = False) mongodb.MongoDatabase[source]

Obtains the database connection from configured application settings.

If reset_connection is True, the container must be the application Registry or any container that can retrieve it to accomplish reference reset. Otherwise, any settings container can be provided.

class cowbird.monitoring.monitoring.MonitoringStore(*args: Any, **kwargs: Any)[source]

Bases: StoreInterface, MongodbStore

Registry for monitoring instances.

Uses MongoDB to store what is monitored and by whom.

Init the store used to save monitors.

type = 'monitors'
index_fields = ['callback', 'path']
save_monitor(monitor: cowbird.monitoring.monitor.Monitor) None[source]

Stores Monitor in MongoDB storage.

delete_monitor(monitor: cowbird.monitoring.monitor.Monitor) None[source]

Removes Monitor from MongoDB storage.

list_monitors() List[cowbird.monitoring.monitor.Monitor][source]

Lists all Monitor in MongoDB storage.

clear_services(drop: bool = True) None[source]

Removes all Monitor from MongoDB storage.

class cowbird.monitoring.monitoring.HandlerFactory[source]

Create handler instance using handler name.

create_handler(name: Literal['Catalog']) cowbird.handlers.impl.catalog.Catalog[source]
create_handler(name: Literal['FileSystem']) cowbird.handlers.impl.filesystem.FileSystem
create_handler(name: Literal['Geoserver']) cowbird.handlers.impl.geoserver.Geoserver
create_handler(name: Literal['Magpie']) cowbird.handlers.impl.magpie.Magpie
create_handler(name: Literal['Nginx']) cowbird.handlers.impl.nginx.Nginx
create_handler(name: Literal['Thredds']) cowbird.handlers.impl.thredds.Thredds
create_handler(name: str) cowbird.handlers.handler.Handler | None

Instantiates a new Handler implementation using its name, overwriting an existing instance if required.

get_handler(name: Literal['Catalog']) cowbird.handlers.impl.catalog.Catalog[source]
get_handler(name: Literal['FileSystem']) cowbird.handlers.impl.filesystem.FileSystem
get_handler(name: Literal['Geoserver']) cowbird.handlers.impl.geoserver.Geoserver
get_handler(name: Literal['Magpie']) cowbird.handlers.impl.magpie.Magpie
get_handler(name: Literal['Nginx']) cowbird.handlers.impl.nginx.Nginx
get_handler(name: Literal['Thredds']) cowbird.handlers.impl.thredds.Thredds
get_handler(name: str) cowbird.handlers.handler.Handler | None

Instantiates a Handler implementation using its name if it doesn’t exist or else returns the existing one from cache.

get_active_handlers() List[cowbird.handlers.handler.Handler][source]

Return a sorted list by priority of Handler implementation activated in the config.

class cowbird.monitoring.monitoring.FSMonitor[source]

Bases: abc.ABC

Interface being called when something changes on the filesystem.

static get_instance() FSMonitor | None[source]
Abstractmethod:

Must return an instance of the class implementing FSMonitor.

abstract on_created(path: str) None[source]

Called when a new path is found.

Parameters:

path – Absolute path of a new file/directory

abstract on_deleted(path: str) None[source]

Called when a path is deleted.

Parameters:

path – Absolute path of a new file/directory

abstract on_modified(path: str) None[source]

Called when a path is updated.

Parameters:

path – Absolute path of a new file/directory

class cowbird.monitoring.monitoring.Monitor(path: str, recursive: bool, callback: cowbird.monitoring.fsmonitor.FSMonitor | Type[cowbird.monitoring.fsmonitor.FSMonitor] | str)[source]

Bases: watchdog.events.FileSystemEventHandler

Implementation of the watchdog FileSystemEventHandler class Allows to start/stop directory monitoring and send events to FSMonitor callback.

Initialize the path monitoring and ready to be started.

Parameters:
  • path – Path to monitor

  • recursive – Monitor subdirectory recursively?

  • callback – Events are sent to this FSMonitor. Can be an object, a class type implementing FSMonitor or a string containing module and class name. The class type or string is used to instantiate an object using the class method FSMonitor.get_instance()

static get_fsmonitor_instance(callback: cowbird.monitoring.fsmonitor.FSMonitor | Type[cowbird.monitoring.fsmonitor.FSMonitor] | str) cowbird.monitoring.fsmonitor.FSMonitor[source]

Return a FSMonitor instance from multiple possible forms including the FSMonitor type, the FSMonitor full qualified class name or a direct instance which is returned as is.

static get_qualified_class_name(monitor: cowbird.monitoring.fsmonitor.FSMonitor) str[source]

Returns the full qualified class name of the FSMonitor object (string of the form module.class_name)

property recursive: bool
property path: str
property callback: str
property callback_instance: cowbird.monitoring.fsmonitor.FSMonitor
property key: MonitorKey

Return a dict that can be used as a unique key to identify this Monitor in a BD.

property is_alive: bool

Returns true if the monitor observer exists and is currently running.

params() MonitorParameters[source]

Return a dict serializing this object from which a new Monitor can be recreated using the init function.

start() None[source]

Start the monitoring so that events can be fired.

stop() None[source]

Stop the monitoring so that events stop to be fired.

on_moved(event: watchdog.events.DirMovedEvent | watchdog.events.FileMovedEvent) None[source]

Called when a file or a directory is moved or renamed.

Parameters:

event – Event representing file/directory movement.

on_created(event: watchdog.events.DirCreatedEvent | watchdog.events.FileCreatedEvent) None[source]

Called when a file or directory is created.

Parameters:

event – Event representing file/directory creation.

on_deleted(event: watchdog.events.DirDeletedEvent | watchdog.events.FileDeletedEvent) None[source]

Called when a file or directory is deleted.

Parameters:

event – Event representing file/directory deletion.

on_modified(event: watchdog.events.DirModifiedEvent | watchdog.events.FileModifiedEvent) None[source]

Called when a file or directory is modified.

Parameters:

event – Event representing file/directory modification.

exception cowbird.monitoring.monitoring.MonitorException[source]

Bases: Exception

Error indicating that a Monitor cannot be started because of an invalid path or callback.

Initialize self. See help(type(self)) for accurate signature.

cowbird.monitoring.monitoring.AnySettingsContainer[source]
class cowbird.monitoring.monitoring.SingletonMeta[source]

Bases: type

A metaclass that creates a Singleton base class when called.

Create a class such that.

class A(object, metaclass=SingletonMeta):
    pass

class B(object, metaclass=SingletonMeta):
    pass

a1 = A()
a2 = A()
b1 = B()
b2 = B()
a1 is a2    # True
b1 is b2    # True
a1 is b1    # False
_instances: Dict[SingletonMetaType, SingletonMetaType]
__call__(*args: Any, **kwargs: Any) SingletonMetaType[source]

Call self as a function.

cowbird.monitoring.monitoring.get_logger(name: str, level: int | None = None, force_stdout: bool = None, message_format: str | None = None, datetime_format: str | None = None) logging.Logger[source]

Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when level is logging.NOTSET.

cowbird.monitoring.monitoring.LOGGER[source]
exception cowbird.monitoring.monitoring.MonitoringConfigurationException[source]

Bases: Exception

Exception thrown when the monitoring instance cannot be initialized because of a bad configuration.

Initialize self. See help(type(self)) for accurate signature.

class cowbird.monitoring.monitoring.Monitoring(config: cowbird.typedefs.AnySettingsContainer = None)[source]

Class handling file system monitoring and registering listeners.

Todo

At some point we will need a consistency function that goes through all monitored folder and make sure that monitoring handlers are up to date.

Initialize the monitoring instance from configured application settings.

Parameters:

config – AnySettingsContainer object from which the db can be retrieved. The default value of None is only there to disable pylint E1120. The singleton instance must be initialized with a proper config and after that the init function should not be hit.

start() None[source]

Load existing monitors and start the monitoring.

register(path: str, recursive: bool, cb_monitor: cowbird.monitoring.fsmonitor.FSMonitor | Type[cowbird.monitoring.fsmonitor.FSMonitor] | str) cowbird.monitoring.monitor.Monitor | None[source]

Register a monitor for a specific path and start it. If a monitor already exists for the specific path/cb_monitor combination it is directly returned. If this monitor was not recursively monitoring its path and the recursive flag is now true, this one take precedence and the monitor is updated accordingly. If the recursive flag was true, and now it is false it has no effect.

Parameters:
  • path – Path to monitor

  • recursive – Monitor subdirectory recursively?

  • cb_monitor – FSMonitor for which an instance is created and events are sent Can be an object, a class type implementing FSMonitor or a string containing module and class name.

Returns:

The monitor registered or already existing for the specific path/cb_monitor combination. Note that the monitor is not created/returned if a MonitorException occurs.

unregister(path: str, cb_monitor: cowbird.monitoring.fsmonitor.FSMonitor | Type[cowbird.monitoring.fsmonitor.FSMonitor] | str) bool[source]

Stop a monitor and unregister it.

Parameters:
  • path – Path used by the monitor

  • cb_monitor – FSMonitor object to remove Can be an object, a class type implementing FSMonitor or a string containing module and class name.

Returns:

True if the monitor is found and successfully stopped, False otherwise

unregister_all() None[source]

Stops and unregisters all monitors.