Coverage for .tox/coverage/lib/python3.13/site-packages/wuttjamaican/cleanup.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-10-07 21:12 -0500

1# -*- coding: utf-8; -*- 

2################################################################################ 

3# 

4# WuttJamaican -- Base package for Wutta Framework 

5# Copyright © 2023-2026 Lance Edgar 

6# 

7# This file is part of Wutta Framework. 

8# 

9# Wutta Framework is free software: you can redistribute it and/or modify it 

10# under the terms of the GNU General Public License as published by the Free 

11# Software Foundation, either version 3 of the License, or (at your option) any 

12# later version. 

13# 

14# Wutta Framework is distributed in the hope that it will be useful, but 

15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 

17# more details. 

18# 

19# You should have received a copy of the GNU General Public License along with 

20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>. 

21# 

22################################################################################ 

23""" 

24Cleanup Handler 

25""" 

26 

27import logging 

28 

29from wuttjamaican.app import GenericHandler 

30from wuttjamaican.util import load_entry_points 

31 

32log = logging.getLogger(__name__) 

33 

34 

35class CleanupHandler(GenericHandler): 

36 """ 

37 Base class and default implementation for the :term:`cleanup 

38 handler`. 

39 """ 

40 

41 def cleanup_everything(self, dry_run=False): 

42 """ 

43 Invoke cleanup logic for all registered :term:`cleaners 

44 <cleaner>`. 

45 

46 This calls :meth:`get_all_cleaners()` and then calls 

47 :meth:`~Cleaner.cleanup()` on each cleaner object. 

48 """ 

49 cleaners = self.get_all_cleaners() 

50 if not cleaners: 

51 log.debug("no registered cleaners found") 

52 return 

53 

54 for key in sorted(cleaners): 

55 cleaner = cleaners[key] 

56 log.debug("running cleanup for '%s' using %s", key, cleaner.get_spec()) 

57 cleaner.cleanup(dry_run=dry_run) 

58 

59 def get_all_cleaners(self): 

60 """ 

61 Retrieve a dictionary containing all registered 

62 :term:`cleaner` objects. 

63 

64 Keys will be the entry point names; values will be 

65 :class:`Cleaner` instances. 

66 

67 :rtype: dict 

68 """ 

69 cleaners = load_entry_points("wutta.cleaners") 

70 for key in list(cleaners): 

71 cleaner = cleaners[key](self.config) 

72 cleaner.key = key 

73 cleaners[key] = cleaner 

74 return cleaners 

75 

76 def get_cleaner(self, key): 

77 """ 

78 Retrieve a specific :term:`cleaner` object. 

79 

80 :param key: Unique key (entry point name) for the cleaner. 

81 

82 :rtype: :class:`Cleaner` instance 

83 """ 

84 cleaners = self.get_all_cleaners() 

85 return cleaners.get(key) 

86 

87 

88class Cleaner: 

89 """ 

90 Base class for :term:`cleaners <cleaner>`. 

91 

92 Each cleaner should typically perform cleanup for "one portion" of 

93 the overall system. The :term:`cleanup handler` typically 

94 orchestrates the overall cleanup. 

95 """ 

96 

97 def __init__(self, config): 

98 self.config = config 

99 self.app = config.get_app() 

100 

101 @classmethod 

102 def get_spec(cls): 

103 """ 

104 :returns: The class :term:`spec` string for the cleaner. 

105 """ 

106 return f"{cls.__module__}:{cls.__name__}" 

107 

108 def cleanup(self, dry_run=False): 

109 """ 

110 Perform actual cleanup steps as needed. 

111 

112 :param dry_run: If true, the cleaner should not *actually* 

113 delete anything, but still log what actions it would have 

114 taken if not for dry run. Default is false in which case 

115 actions really will be taken. 

116 """