A tool to create, manage, and destroy data science development containers.
Uses the parser from dsco.options
to read the command line
options (args). Creates a conf dictionary to load project
information (based on current path).
Runs the function found by the cmd --> run_cmd
mapping in
dispatcher, passing both args and conf. i.e.
run_cmd(args, conf)
.
dsco.application.
main
()¶dsco.helpers.
Machine
(name, url)¶Bases: tuple
name
¶Alias for field number 0
url
¶Alias for field number 1
dsco.helpers.
add_timer
(func_name)¶Decorator to add timing to run commands
dsco.helpers.
docker_ps
(project='', service='', dsco=False, local=True, remote=False)¶dsco.helpers.
docker_ps_cmd
(project='', service='', dsco=False)¶dsco.helpers.
docker_ps_output_cleanup
(ps_output, machine)¶dsco.helpers.
find_proj_root
(path)¶dsco.helpers.
get_container
(proj_name, service)¶dsco.helpers.
get_docker_compose_conf
(path)¶dsco.helpers.
get_port
(conf, service)¶dsco.helpers.
get_pyproject
(path)¶dsco.helpers.
list_docker_machines
()¶dsco.helpers.
parse_machine
(out)¶dsco.helpers.
update_port
(lines, old_port, new_port)¶Add commands to the argument parser
The argument parser is what translates the text entered on the command
line to specific actions. For example, if we enter dsco init
we
want our program to create a new project folder populated from our
project template.
The form this takes is dsco [subparser ...]
. Where subparser
consists of a command plus options. For instance, dsco up --dev
consists of the command up
and the option --dev
. This
would be processed by the subparser up
and would launch the
command required to bring up a dev container.
This module adds the subparsers in the commands
directory to our
argument parser. The name of the commands
directory is specified
as cmd_dir
.
Each module in cmd_dir is expected to define the following:
- cmd_name (str)
The name of the sub-command
- add_subparser (subparsers)
This should take the subparsers object created in this file and add a subparser to it.
subparsers
is mutable so it does not need to return anything.
- Args
- subparsers (argparse._SubParsersAction)
The subparser object to which all sub-commands will be assigned.
- Returns
None
- add_dispatch(dispatcher)
The dispatcher dictionary maps the command given at the command line to a function to be executed. For instance when we run
dsco up --dev
, the dispatcher would contain a mapping:up –> function that launches dev container via docker-compose
More specifically, add_dispatch takes the dispatcher dict created in this file and adds an entry corresponding to the command name. The entry should be a function that takes the
args
andconf
objects created in application.py and executes the desired action.
- Args
- dispatcher (dict)
Dictionary containing the mappings
cmd --> run_cmd
. The dictionary is mutated to add a new mapping. For consistency, you should name the functionrun_cmd
, but this is not required.We will define the requirements on the mapping next:
- cmd (string)
This is the name of the subparser
- run_cmd(args, conf)
Function executing desired action.
- Args
- args (argParse.Namespace)
This contains the command line arguments as parsed by the appropriate subparser.
- conf (dict)
This dictionary contains project specific settings such as the project root directory, settings from the pyproject.toml file (if it exists) etc.
- Returns
None
Example: See
dsco.commands
.
With this structure in place, this file first creates the objects:
- parser (argparse.ArgumentParser)
Interpret command line options
- subparsers (argparse._SubParsersAction)
Contains definitions of sub commands.
- dispatcher (dict)
dictionary used to run command based on command line inputs.
Then for each module found in cmd_dir
(files of the form
*.py
, excluding files that begin with "_"
) we add all
the subparsers and dispatchers with:
module.add_subparser(subparsers)
module.add_dispatch(dispatcher)
At run time, the application imports parser to interpret the command line options and dispatcher to run the appropriate command.