authentik.blueprints.management.commands.blueprint_shell

Test and debug Blueprints

 1"""Test and debug Blueprints"""
 2
 3import atexit
 4import readline
 5from pathlib import Path
 6from pprint import pformat
 7from sys import exit as sysexit
 8from textwrap import indent
 9
10from django.core.management.base import BaseCommand, no_translations
11from structlog.stdlib import get_logger
12from yaml import load
13
14from authentik.blueprints.v1.common import BlueprintLoader, EntryInvalidError
15from authentik.core.management.commands.shell import get_banner_text
16from authentik.lib.utils.errors import exception_to_string
17
18LOGGER = get_logger()
19
20
21class Command(BaseCommand):
22    """Test and debug Blueprints"""
23
24    lines = []
25
26    def __init__(self, *args, **kwargs) -> None:
27        super().__init__(*args, **kwargs)
28        histfolder = Path("~").expanduser() / Path(".local/share/authentik")
29        histfolder.mkdir(parents=True, exist_ok=True)
30        histfile = histfolder / Path("blueprint_shell_history")
31        readline.parse_and_bind("tab: complete")
32        readline.parse_and_bind("set editing-mode vi")
33
34        try:
35            readline.read_history_file(str(histfile))
36        except FileNotFoundError:
37            pass
38
39        atexit.register(readline.write_history_file, str(histfile))
40
41    @no_translations
42    def handle(self, *args, **options):
43        """Interactively debug blueprint files"""
44        self.stdout.write(get_banner_text("Blueprint shell"))
45        self.stdout.write("Type '.eval' to evaluate previously entered statement(s).")
46
47        def do_eval():
48            yaml_input = "\n".join([line for line in self.lines if line])
49            data = load(yaml_input, BlueprintLoader)
50            self.stdout.write(pformat(data))
51            self.lines = []
52
53        while True:
54            try:
55                line = input("> ")
56                if line == ".eval":
57                    do_eval()
58                else:
59                    self.lines.append(line)
60            except EntryInvalidError as exc:
61                self.stdout.write("Failed to evaluate expression:")
62                self.stdout.write(indent(exception_to_string(exc), prefix="  "))
63            except EOFError:
64                break
65            except KeyboardInterrupt:
66                self.stdout.write()
67                sysexit(0)
68        self.stdout.write()
LOGGER = <BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
class Command(django.core.management.base.BaseCommand):
22class Command(BaseCommand):
23    """Test and debug Blueprints"""
24
25    lines = []
26
27    def __init__(self, *args, **kwargs) -> None:
28        super().__init__(*args, **kwargs)
29        histfolder = Path("~").expanduser() / Path(".local/share/authentik")
30        histfolder.mkdir(parents=True, exist_ok=True)
31        histfile = histfolder / Path("blueprint_shell_history")
32        readline.parse_and_bind("tab: complete")
33        readline.parse_and_bind("set editing-mode vi")
34
35        try:
36            readline.read_history_file(str(histfile))
37        except FileNotFoundError:
38            pass
39
40        atexit.register(readline.write_history_file, str(histfile))
41
42    @no_translations
43    def handle(self, *args, **options):
44        """Interactively debug blueprint files"""
45        self.stdout.write(get_banner_text("Blueprint shell"))
46        self.stdout.write("Type '.eval' to evaluate previously entered statement(s).")
47
48        def do_eval():
49            yaml_input = "\n".join([line for line in self.lines if line])
50            data = load(yaml_input, BlueprintLoader)
51            self.stdout.write(pformat(data))
52            self.lines = []
53
54        while True:
55            try:
56                line = input("> ")
57                if line == ".eval":
58                    do_eval()
59                else:
60                    self.lines.append(line)
61            except EntryInvalidError as exc:
62                self.stdout.write("Failed to evaluate expression:")
63                self.stdout.write(indent(exception_to_string(exc), prefix="  "))
64            except EOFError:
65                break
66            except KeyboardInterrupt:
67                self.stdout.write()
68                sysexit(0)
69        self.stdout.write()

Test and debug Blueprints

Command(*args, **kwargs)
27    def __init__(self, *args, **kwargs) -> None:
28        super().__init__(*args, **kwargs)
29        histfolder = Path("~").expanduser() / Path(".local/share/authentik")
30        histfolder.mkdir(parents=True, exist_ok=True)
31        histfile = histfolder / Path("blueprint_shell_history")
32        readline.parse_and_bind("tab: complete")
33        readline.parse_and_bind("set editing-mode vi")
34
35        try:
36            readline.read_history_file(str(histfile))
37        except FileNotFoundError:
38            pass
39
40        atexit.register(readline.write_history_file, str(histfile))
lines = []
def handle(*args, **kwargs):
106    def wrapper(*args, **kwargs):
107        from django.utils import translation
108
109        saved_locale = translation.get_language()
110        translation.deactivate_all()
111        try:
112            res = handle_func(*args, **kwargs)
113        finally:
114            if saved_locale is not None:
115                translation.activate(saved_locale)
116        return res

The type of the None singleton.