Source code for wuttjamaican.cleanup

# -*- coding: utf-8; -*-
################################################################################
#
#  WuttJamaican -- Base package for Wutta Framework
#  Copyright © 2023-2026 Lance Edgar
#
#  This file is part of Wutta Framework.
#
#  Wutta Framework is free software: you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option) any
#  later version.
#
#  Wutta Framework is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#  more details.
#
#  You should have received a copy of the GNU General Public License along with
#  Wutta Framework.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Cleanup Handler
"""

import logging

from wuttjamaican.app import GenericHandler
from wuttjamaican.util import load_entry_points

log = logging.getLogger(__name__)


[docs] class CleanupHandler(GenericHandler): """ Base class and default implementation for the :term:`cleanup handler`. """
[docs] def cleanup_everything(self, dry_run=False): """ Invoke cleanup logic for all registered :term:`cleaners <cleaner>`. This calls :meth:`get_all_cleaners()` and then calls :meth:`~Cleaner.cleanup()` on each cleaner object. """ cleaners = self.get_all_cleaners() if not cleaners: log.debug("no registered cleaners found") return for key in sorted(cleaners): cleaner = cleaners[key] log.debug("running cleanup for '%s' using %s", key, cleaner.get_spec()) cleaner.cleanup(dry_run=dry_run)
[docs] def get_all_cleaners(self): """ Retrieve a dictionary containing all registered :term:`cleaner` objects. Keys will be the entry point names; values will be :class:`Cleaner` instances. :rtype: dict """ cleaners = load_entry_points("wutta.cleaners") for key in list(cleaners): cleaner = cleaners[key](self.config) cleaner.key = key cleaners[key] = cleaner return cleaners
[docs] def get_cleaner(self, key): """ Retrieve a specific :term:`cleaner` object. :param key: Unique key (entry point name) for the cleaner. :rtype: :class:`Cleaner` instance """ cleaners = self.get_all_cleaners() return cleaners.get(key)
[docs] class Cleaner: """ Base class for :term:`cleaners <cleaner>`. Each cleaner should typically perform cleanup for "one portion" of the overall system. The :term:`cleanup handler` typically orchestrates the overall cleanup. """ def __init__(self, config): self.config = config self.app = config.get_app()
[docs] @classmethod def get_spec(cls): """ :returns: The class :term:`spec` string for the cleaner. """ return f"{cls.__module__}:{cls.__name__}"
[docs] def cleanup(self, dry_run=False): """ Perform actual cleanup steps as needed. :param dry_run: If true, the cleaner should not *actually* delete anything, but still log what actions it would have taken if not for dry run. Default is false in which case actions really will be taken. """