|
| 1 | +from abc import ABC |
| 2 | +from typing import Callable, Union |
| 3 | + |
| 4 | +from django.core.management.base import BaseCommand as DjangoCommand |
| 5 | +from django.core.management.base import CommandParser |
| 6 | + |
| 7 | + |
| 8 | +class BaseCommand(DjangoCommand, ABC): |
| 9 | + """BaseCommand that can be inherited and customized.""" |
| 10 | + |
| 11 | + quiet: bool = False |
| 12 | + """If True, the print statements are quiet.""" |
| 13 | + |
| 14 | + def __init__(self, stdout=None, stderr=None, no_color=False, force_color=False, quiet=False): |
| 15 | + self.quiet = quiet |
| 16 | + super().__init__(stdout=stdout, stderr=stderr, no_color=no_color, force_color=force_color) |
| 17 | + |
| 18 | + def add_arguments(self, parser: CommandParser) -> None: |
| 19 | + parser.add_argument( |
| 20 | + "--quiet", |
| 21 | + action="store_true", |
| 22 | + default=False, |
| 23 | + help="Print no information.", |
| 24 | + ) |
| 25 | + |
| 26 | + def _print_s( |
| 27 | + self, |
| 28 | + txt, |
| 29 | + ending=None, |
| 30 | + ): |
| 31 | + """ |
| 32 | + Print given `txt` in SUCCESS style to `self.stdout` (generally green). |
| 33 | + """ |
| 34 | + self._print(txt=txt, style=self.style.SUCCESS, ending=ending) |
| 35 | + |
| 36 | + def _print_w( |
| 37 | + self, |
| 38 | + txt, |
| 39 | + ending=None, |
| 40 | + ): |
| 41 | + """ |
| 42 | + Print given `txt` in WARNING style to `self.stdout` (generally yellow). |
| 43 | + """ |
| 44 | + self._print(txt=txt, style=self.style.WARNING, ending=ending) |
| 45 | + |
| 46 | + def _print_e( |
| 47 | + self, |
| 48 | + txt, |
| 49 | + ending=None, |
| 50 | + ): |
| 51 | + """ |
| 52 | + Print given `txt` in ERROR style to `self.stdout` (generally red). |
| 53 | + """ |
| 54 | + self._print(txt=txt, style=self.style.ERROR, ending=ending) |
| 55 | + |
| 56 | + def _print_n( |
| 57 | + self, |
| 58 | + txt, |
| 59 | + ending=None, |
| 60 | + ): |
| 61 | + """ |
| 62 | + Print given `txt` in NOTICE style to `self.stdout` (generally red, but lighter). |
| 63 | + """ |
| 64 | + self._print(txt=txt, style=self.style.NOTICE, ending=ending) |
| 65 | + |
| 66 | + def _print( |
| 67 | + self, |
| 68 | + txt, |
| 69 | + style: Union[None, Callable] = None, |
| 70 | + ending=None, |
| 71 | + ): |
| 72 | + if self.quiet: |
| 73 | + return |
| 74 | + if style is not None: |
| 75 | + self.stdout.write(style(txt), ending=ending) |
| 76 | + else: |
| 77 | + self.stdout.write(txt, ending=ending) |
| 78 | + |
| 79 | + def confirm_action(self, message: str) -> bool: |
| 80 | + response = input(f"{message} (y/n): ").strip().lower() |
| 81 | + return response == "y" |
| 82 | + |
| 83 | + def handle(self, *args, **options): |
| 84 | + """ |
| 85 | + Override this function to implement the command. Call |
| 86 | + `super().handle(*args, **options)` first in order to add parsing for |
| 87 | + `BaseCommand` arguments. |
| 88 | + """ |
| 89 | + |
| 90 | + if options["quiet"]: |
| 91 | + self.quiet = True |
0 commit comments