Reference¶
██████╗ ██╗ █████╗ ███╗ ██╗ ██████╗ ██████╗
██╔══██╗ ██║██╔══██╗████╗ ██║██╔════╝ ██╔═══██╗
██║ ██║ ██║███████║██╔██╗ ██║██║ ███╗██║ ██║
██║ ██║██ ██║██╔══██║██║╚██╗██║██║ ██║██║ ██║
██████╔╝╚█████╔╝██║ ██║██║ ╚████║╚██████╔╝╚██████╔╝
╚═════╝ ╚════╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚═════╝
██████╗ ██████╗ ██╗ ██╗████████╗██╗███╗ ██╗███████╗███████╗
██╔══██╗██╔═══██╗██║ ██║╚══██╔══╝██║████╗ ██║██╔════╝██╔════╝
██████╔╝██║ ██║██║ ██║ ██║ ██║██╔██╗ ██║█████╗ ███████╗
██╔══██╗██║ ██║██║ ██║ ██║ ██║██║╚██╗██║██╔══╝ ╚════██║
██║ ██║╚██████╔╝╚██████╔╝ ██║ ██║██║ ╚████║███████╗███████║
╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝╚══════╝
A simple Django app that allows you to specify batches of commands in your settings
files and then run them in sequence by name using the provided routine
command.
- class django_routines.ManagementCommand¶
Bases:
_RoutineCommandA RoutineCommand that holds a information for invoking a management command.
- "options": Dict[str, Any]¶
Any options to pass to the command via
call_command(). Not valid for SystemCommands
- __init__(command, priority=0, switches=(), pre_hook=None, post_hook=None, result=None, options=<factory>)¶
- Parameters:
priority (int)
pre_hook (str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None)
post_hook (str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None)
result (Any)
- Return type:
None
- classmethod from_dict(obj)¶
Return a RoutineCommand object from a dictionary representing it.
- Parameters:
obj (ManagementCommand | SystemCommand | Dict[str, Any])
- Return type:
- "post_hook": str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None = None¶
A function that will be run after the command has been run. It may make modifications to the command (including its result) or decide to exit the routine early by returning True. See
PostHookIt may also make modifications to the next command that will be run, if any.
May be the callable function or an import string to the callable function.
- "pre_hook": str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None = None¶
A function that will be run before the command is run. It may make modifications to the command or decide to skip the command by returning True. See
PreHookMay be the callable function or an import string to the callable function.
- "priority": int = 0¶
The order of the command in the routine. Priority ties will be run in insertion order.
- "result": Any = None¶
The result of the command run. This will either be the value returned by
call_command()or asubprocess.run()result object if the command was run in a subprocess.
- "switches": List[str] | Tuple[str, ...] = ()¶
If any switches are specified, the command will only run when one of the switches is activated on routine invocation from the command line. For example, if you list a switch named
init, you would have to invoke the routine like so to run this command as part of the routine:django-admin routine <routine_name> --init
- "command": str | Tuple[str, ...]¶
The command and its arguments to run the routine, all strings or coercible to strings that the command will parse correctly.
This should be what you would pass as the
argsofcall_command()orsubprocess.run()
- class django_routines.SystemCommand¶
Bases:
_RoutineCommandA RoutineCommand that represents a system command instead of a management command.
- __init__(command, priority=0, switches=(), pre_hook=None, post_hook=None, result=None)¶
- Parameters:
priority (int)
pre_hook (str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None)
post_hook (str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None)
result (Any)
- Return type:
None
- classmethod from_dict(obj)¶
Return a RoutineCommand object from a dictionary representing it.
- Parameters:
obj (ManagementCommand | SystemCommand | Dict[str, Any])
- Return type:
- "post_hook": str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None = None¶
A function that will be run after the command has been run. It may make modifications to the command (including its result) or decide to exit the routine early by returning True. See
PostHookIt may also make modifications to the next command that will be run, if any.
May be the callable function or an import string to the callable function.
- "pre_hook": str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None = None¶
A function that will be run before the command is run. It may make modifications to the command or decide to skip the command by returning True. See
PreHookMay be the callable function or an import string to the callable function.
- "priority": int = 0¶
The order of the command in the routine. Priority ties will be run in insertion order.
- "result": Any = None¶
The result of the command run. This will either be the value returned by
call_command()or asubprocess.run()result object if the command was run in a subprocess.
- "switches": List[str] | Tuple[str, ...] = ()¶
If any switches are specified, the command will only run when one of the switches is activated on routine invocation from the command line. For example, if you list a switch named
init, you would have to invoke the routine like so to run this command as part of the routine:django-admin routine <routine_name> --init
- "command": str | Tuple[str, ...]¶
The command and its arguments to run the routine, all strings or coercible to strings that the command will parse correctly.
This should be what you would pass as the
argsofcall_command()orsubprocess.run()
- class django_routines.Routine¶
Bases:
objectDataclass to hold the routine information.
- "help_text": str | Promise¶
The help text to display for the routine. May be a string wrapped by
gettext_lazy()
- "commands": List[ManagementCommand | SystemCommand]¶
The commands to run in the routine.
- "switch_helps": Dict[str, str | Promise]¶
Help text for switches. The keys are the switch names, and the values are the help text to display for each switch in the CLI help output.
- __init__(name, help_text, commands=<factory>, switch_helps=<factory>, subprocess=False, atomic=False, continue_on_error=False, initialize=None, finalize=None, pre_hook=None, post_hook=None)¶
- Parameters:
name (str)
help_text (str | Promise)
commands (List[ManagementCommand | SystemCommand])
subprocess (bool)
atomic (bool)
continue_on_error (bool)
initialize (str | Callable[[Routine, List[ManagementCommand | SystemCommand], Set[str], Dict[str, Any]], None] | None)
finalize (str | Callable[[Routine, List[Any]], None] | None)
pre_hook (str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None)
post_hook (str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None)
- Return type:
None
- "initialize": str | Callable[[Routine, List[ManagementCommand | SystemCommand], Set[str], Dict[str, Any]], None] | None = None¶
A function to run before the routine is run. See
InitializeCallbackMay be the callable function or an import string to the callable function.
- "finalize": str | Callable[[Routine, List[Any]], None] | None = None¶
A function to run after the routine is run. See
FinalizeCallbackMay be the callable function or an import string to the callable function.
- "pre_hook": str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None = None¶
This function will be run before each command that lacks its own pre_hook in the routine. See
PreHook. You can determine if this is the starting command of the routine by checking if the previous command is None.May be the callable function or an import string to the callable function.
- "post_hook": str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None = None¶
This function will be run after each command that lacks its own post_hook in the routine. See
PostHook. You can determine if this is the last command of the routine by checking if the next command is None. Post hooks may also be used to exit the routine early by returning True.May be the callable function or an import string to the callable function.
- django_routines.routine(name, help_text='', *commands, subprocess=False, atomic=False, continue_on_error=False, initialize=None, finalize=None, pre_hook=None, post_hook=None, **switch_helps)¶
Register a routine to the t.List of routines in settings to be run.
- Parameters:
name (str) – The name of the routine to register.
help_text (str | Promise) – The help text to display for the routine by the routines command.
commands (ManagementCommand | SystemCommand) – The commands to run in the routine.
subprocess (bool) – If true run each of the commands in a subprocess.
atomic (bool) – Run all commands in the same transaction.
continue_on_error (bool) – Keep going if a command fails.
initialize (str | Callable[[Routine, List[ManagementCommand | SystemCommand], Set[str], Dict[str, Any]], None] | None) – A function to run before the routine is run. See
InitializeCallbackfinalize (str | Callable[[Routine, List[Any]], None] | None) – A function to run after the routine is run. See
FinalizeCallbackpre_hook (str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None) – A function to run before each command in the routine is run if the command does not have its own pre_hook. See
PreHookpost_hook (str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None) – A function to run after each command in the routine is run if the command does not have its own post_hook. See
PostHookswitch_helps – A mapping of switch names to help text for the switches.
- Raises:
ImproperlyConfiguredif theDJANGO_ROUTINESsettings variable is not valid.
- django_routines.command(routine, *command, priority=0, switches=(), pre_hook=None, post_hook=None, **options)¶
Add a command to the named routine in settings to be run.
Note
You must call this function from a settings file.
- Parameters:
routine (str) – The name of the routine the command belongs to.
command (str) – The command and its arguments to run the routine, all strings or coercible to strings that the command will parse correctly.
priority (int) – The order of the command in the routine. Priority ties will be run in insertion order (defaults to zero).
switches (Sequence[str] | None) – The command will run only when one of these switches is active, or for all invocations of the routine if no switches are configured.
options – t.Any options to pass to the command via
call_command().pre_hook (str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None) – A function to run before the command is run. See
PreHookpost_hook (str | Callable[[Routine, ManagementCommand | SystemCommand, ManagementCommand | SystemCommand | None, Dict[str, Any]], bool | None] | None) – A function to run after the command has been run. See
PostHook
- Raises:
ImproperlyConfiguredif theDJANGO_ROUTINESsettings variable is not valid.- Returns:
The new command.
- django_routines.get_routine(name, scope=None)¶
Get the routine by name.
Note
If called before settings have been configured, this function must be called from settings.
- Parameters:
name (str) – The name of the routine to get.
- Returns:
The routine.
- Raises:
KeyErrorif the routine does not exist, or routines have not been configured.- Raises:
ImproperlyConfiguredif theDJANGO_ROUTINESsettings variable is not valid.- Return type:
- django_routines.routines(scope=None)¶
A generator that yields Routine objects from settings.
- Yield:
Routine objects
- Raises:
ImproperlyConfiguredif theDJANGO_ROUTINESsettings variable is not valid.- Return type:
- django_routines.Command¶
Command type, either a ManagementCommand or SystemCommand.
alias of
ManagementCommand|SystemCommand
- django_routines.InitializeCallback¶
A callable or import string to a callable that will be run just before the routine’s first command is run.
Signature:
(routine, plan, switches, options) -> None- Parameters:
routine (Routine) – The routine being run.
plan (List[Union[ManagementCommand, SystemCommand]]) – The command execution order that will be run.
switches (Set[str]) – The set of active switches for this routine run.
alias of
str|Callable[[Routine,List[ManagementCommand|SystemCommand],Set[str],Dict[str,Any]],None]
- django_routines.FinalizeCallback¶
A callable or import string to a callable that will be run just after the routine’s last command is run.
See also
django_typer.management.finalize().Signature:
(routine, results) -> None- Parameters:
routine (
RoutineCommand) – An instance of the running routine command.results (list) – The results of the commands that were run in the routine.
- django_routines.PreHook¶
Function type signature for a pre-hook functions. Pre-hook functions can modify command objects (including their arguments) before they are run.
Signature:
(routine, command, previous, options) -> bool | None- Parameters:
routine (
django_routines.Routine) – The routine being run.command (
django_routines.ManagementCommand|django_routines.SystemCommand) – The command about to be run.previous (
django_routines.ManagementCommand|django_routines.SystemCommand) – The previous command that was run (or None if at the beginning).options (Dict[str, Any]) – A dictionary containing the routine level options.
- Returns:
Return true to skip the command.
- Return type:
alias of
str|Callable[[Routine,Command,Command|None,Dict[str,Any]],bool|None]
- django_routines.PostHook¶
Function type signature for a post-hook functions. Post-hook functions can modify command objects (including their results) after they are run or the next command before it is run. Returning a truthy value will exit the routine early.
Signature:
(routine, command, next, options) -> bool | None- Parameters:
routine (
django_routines.Routine) – The routine being run.command (
django_routines.ManagementCommand|django_routines.SystemCommand) – The command about to be run.next (
django_routines.ManagementCommand|django_routines.SystemCommand) – The next command that will be run (or None if at the end).options (Dict[str, Any]) – A dictionary containing the routine level options.
- Returns:
Return true to exit the routine early.
- Return type:
alias of
str|Callable[[Routine,Command,Command|None,Dict[str,Any]],bool|None]
exceptions¶
routine command¶
- django_routines.management.commands.routine.load_hook(hook)¶
Load a hook function from a string or return the hook as-is.
- class django_routines.management.commands.routine.Command¶
Bases:
TyperCommandA
TyperCommandthat reads theDJANGO_ROUTINESsetting from settings and builds out a set of subcommands for each routine that when invoked will run those routines in order. Each routine also has a subcommand calledlistthat prints which commands will be executed and in what order given the switches the user has selected.Note
If
--verbosityis supplied, the value will be passed via options to any command in the routine that accepts it.- property plan: List[ManagementCommand | SystemCommand]¶
The Commands that make up the execution plan for the currently active routine and switches.