Changeset - 82501640d97b
[Not reviewed]
0 3 3
Lance Edgar (lance) - 22 months ago 2022-12-23 23:28:50
lance@edbob.org
Add initial logic for `rattail cleanup` command

will add a cleaner for beaker sessions shortly; more will come later
i'm sure
6 files changed with 160 insertions and 0 deletions:
0 comments (0 inline, 0 general)
docs/api/index.rst
Show inline comments
 
@@ -24,6 +24,7 @@ attributes and method signatures etc.
 
   rattail/batch/vendorcatalog
 
   rattail/board
 
   rattail/bouncer/index
 
   rattail/cleanup
 
   rattail/clientele
 
   rattail/commands.batch
 
   rattail/config
docs/api/rattail/cleanup.rst
Show inline comments
 
new file 100644
 

	
 
``rattail.cleanup``
 
===================
 

	
 
.. automodule:: rattail.cleanup
 
   :members:
rattail/app.py
Show inline comments
 
@@ -540,6 +540,20 @@ class AppHandler(object):
 

	
 
        return self.bounce_handlers[key]
 

	
 
    def get_cleanup_handler(self, **kwargs):
 
        """
 
        Get the configured "cleanup" handler.
 

	
 
        :returns: The :class:`~rattail.cleanup.CleanupHandler`
 
           instance for the app.
 
        """
 
        if not hasattr(self, 'cleanup_handler'):
 
            spec = self.config.get('rattail.cleanup', 'handler',
 
                                   default='rattail.cleanup:CleanupHandler')
 
            Handler = self.load_object(spec)
 
            self.cleanup_handler = Handler(self.config)
 
        return self.cleanup_handler
 

	
 
    def get_clientele_handler(self, **kwargs):
 
        """
 
        Get the configured "clientele" handler.
rattail/cleanup.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8; -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2022 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
#  Rattail 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.
 
#
 
#  Rattail 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
 
#  Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 
"""
 
Cleanup Handler
 

	
 
See also :doc:`rattail-manual:base/handlers/other/cleanup`.
 
"""
 

	
 
from __future__ import unicode_literals, absolute_import
 

	
 
import logging
 

	
 
from rattail.app import GenericHandler
 
from rattail.util import load_entry_points
 

	
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
class CleanupHandler(GenericHandler):
 
    """
 
    Base class and default implementation for the "cleanup" handler,
 
    responsible for removing old unwanted files etc.
 
    """
 

	
 
    def cleanup_everything(self, session, dry_run=False, progress=None, **kwargs):
 
        """
 
        Invoke cleanup logic for all enabled cleaners.
 
        """
 
        cleaners = self.get_all_cleaners()
 
        for key in sorted(cleaners):
 
            cleaner = cleaners[key]
 
            log.debug("running cleanup for: %s", key)
 
            cleaner.cleanup(session, dry_run=dry_run, progress=progress)
 

	
 
    def get_all_cleaners(self, **kwargs):
 
        """
 
        Return a dictionary containing all registered cleaner objects.
 
        """
 
        cleaners = load_entry_points('rattail.cleaners')
 
        for key in list(cleaners):
 
            cleaner = cleaners[key](self.config)
 
            cleaner.key = key
 
            cleaners[key] = cleaner
 
        return cleaners
 

	
 
    def get_cleaner(self, key):
 
        """
 
        Retrieve a specific cleaner object.
 
        """
 
        cleaners = self.get_all_cleaners()
 
        return cleaners.get(key)
 

	
 

	
 
class Cleaner(object):
 
    """
 
    Base class for cleaners.
 
    """
 

	
 
    def __init__(self, config, **kwargs):
 
        self.config = config
 
        self.app = config.get_app()
 
        self.model = self.app.model
 

	
 
    def cleanup(self, session, dry_run=False, progress=None, **kwargs):
 
        """
 
        Perform actual cleanup steps as needed.
 
        """
rattail/commands/cleanup.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8; -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2022 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
#  Rattail 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.
 
#
 
#  Rattail 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
 
#  Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 
"""
 
Cleanup commands
 
"""
 

	
 
from __future__ import unicode_literals, absolute_import
 

	
 
from rattail.commands import Subcommand, date_argument
 

	
 

	
 
class Cleanup(Subcommand):
 
    """
 
    Cleanup by removing older data etc.
 
    """
 
    name = 'cleanup'
 
    description = __doc__.strip()
 

	
 
    def add_parser_args(self, parser):
 
        parser.add_argument('--dry-run', action='store_true',
 
                            help="Go through motions and log actions but do not "
 
                            "actually commit the results.")
 

	
 
    def run(self, args):
 
        self.cleanup_handler = self.app.get_cleanup_handler()
 
        session = self.make_session()
 
        self.cleanup_handler.cleanup_everything(session,
 
                                                dry_run=args.dry_run,
 
                                                progress=self.progress)
 
        self.finalize_session(session, dry_run=args.dry_run)
setup.py
Show inline comments
 
@@ -208,6 +208,7 @@ setup(
 
            'backup = rattail.commands.backup:Backup',
 
            'bouncer = rattail.commands.core:EmailBouncer',
 
            'checkdb = rattail.commands.core:CheckDatabase',
 
            'cleanup = rattail.commands.cleanup:Cleanup',
 
            'clonedb = rattail.commands.core:CloneDatabase',
 
            'datasync = rattail.commands.datasync:DataSync',
 
            'date-organize = rattail.commands.core:DateOrganize',
0 comments (0 inline, 0 general)