genvid.toolbox.BaseTool¶
-
class
genvid.toolbox.
BaseTool
(logname=None, command_option_name='command', **kwargs)¶ Bases:
object
Base class for all tools.
This class sets up and provides basic functionalities to all other tools. In particular, it handles the
main()
andget_parser()
methods. Seeadd_commands()
,add_command()
andrun_command()
for more details.Please, note that most attributes of this class aren’t initialized properly before instantiation.
-
BINDIR
= None¶ Environment variable containing the folder where the binaries of the project are stored.
-
COMMAND_OPTION_NAME
= None¶ The name used for the command arguments on the command line.
See
add_command()
for more details.
-
CURDIR
= None¶ Environment variable containing the current script directory path if set correctly, or the current working directory by default.
Warning: This variable is not very reliable. Try to avoid using it.
-
ENVIRONMENT_VARIABLES
= set()¶ The set of environment variables used by all tools.
The list is updated each time
setdefault()
is called. It is used byenv()
and func:print_env
-
GENVID_TOOLBOX_LOGLEVEL
= None¶ Environment variable of the root logger in toolbox.
-
GENVID_TOOLBOX_VERSION
= None¶ Environment variable of the currently installed toolbox version.
This variable cannot be overridden.
-
HOME
= None¶ Environment variable containing the user home directory name.
Taken from environment variable or
os.path.expanduser()
-
MODE_0600
= 384¶ User RW file permissions
-
MODE_0644
= 420¶ User RW, Group and Other Read file permissions
-
MODE_0700
= 448¶ User RWX file permissions
-
MODE_0755
= 493¶ User RWX and Group and Other RX file permissions
-
PLATFORM
= None¶ Name of the current platform.
Could be either linux or windows for now.
-
PYTHON_EXECUTABLE
= None¶ Environment variable containing the current python executable by default.
Taken from the environment or
sys.executable
if not set.
-
ROOTDIR
= None¶ Environment variable representing the rootdir of the project.
-
add_base_commands
()¶
-
add_command
(name, description, **kwargs)¶ Add a new command to the tool.
The command will be added to the list of
commands
.- Args:
name: The name of the command.
description: A help text about the command.
kwargs: Other keyword arguments to pass to the parser for this tool:attr:subparsers.
- Returns:
- A command parser.
- Raises:
- AssertionError if name already exists as a command.
-
add_commands
()¶ Add commands for the tool.
This method must be overridden. It allows the child class to add new commands using
add_commands()
. The list of all existing commands and their parser are available fromcommands
-
commands
= None¶ A dictionary of the parser for each command.
See
add_command()
for more details.
-
static
delenv
(name: str)¶
-
static
env
()¶ Return the list of used environment variables and their values.
The list of variables to return is taken from
ENVIRONMENT_VARIABLES
.
-
get_parser
(subparsers=None, name=None, description=None, **kwargs)¶ Returns the parser for the tool.
- Args:
subparsers: Custom subparsers to use.
name: The name of the tool (default to
NAME
)description: The description of the tool (default to
DESCRIPTION
)kwargs: Any other arguments to pass to the
argparse.ArgumentParser
factory.- Returns:
- The parser for this tool.
-
static
install_file
(src: str, dst: str, mode=420, uid=-1, gid=-1)¶ Copy a file src to dst, and change its permissions.
If no uid are specified, the default uid of the process is used.
If no gid are specified, the uid is used.
-
static
install_json
(dest: str, config)¶ Dump config as a JSON file and copy it to destination.
-
static
install_tmp_file
(dest: str, data, mode=420)¶ Write data to a temporary file and copy it to dst.
The installation is done using
install_file()
.
-
logger
= None¶ The logger for the tool.
-
main
(options=None)¶ Run the tool.
If options is None, it will obtained them from the command line arguments. Elsewhere, it must be either a namespace object or a dictionary.
-
parser
= None¶ The current parser for this tool.
See
add_commands()
for more details.
-
static
print_env
()¶ Print the list of used environment variables and their values.
The list of variables to return is taken from
ENVIRONMENT_VARIABLES
.
-
pyrun
(*args, **kwargs)¶ Run a python script using the current python version.
This is roughly equivalent to:
run("python", *args, **kwargs)
-
static
run
(*args, check=True, **kwargs)¶ Wrapper around
subprocess.run()
that pass all arguments in args and set the default value of check to True.
-
run_base_command
(command, options)¶
-
run_command
(command, options)¶ Run command with those options.
You must override this method.
- Args:
command: The name of the command to run.
options: A
argparse.Namespace
object with the options pass in arguments.
-
run_daemon
(*args, **kwargs)¶ Run the process in background.
- Args:
args: The command to run.
stdout: The output file for stdout.
stderr: The output file for stderr.
cwd: The current working directory.
env: The environment to used.
pidfile: The pidfile to save the PID of the process.
-
run_elevated
(*args, **kwargs)¶ Run the command using elevated privileges on Windows.
-
run_elevated_win32
(*args, **kwargs)¶ Run the command using elevated privileges on Windows.
-
run_powershell
(*cmds, **kwargs)¶ Run cmds as a powershell script.
- Args:
- cmds: A list of powershell commands to execute.
-
static
safe_unlink
(filename: str)¶ Unlink a file only if it exists
-
static
setbasescript
(path: str)¶ Set
CURDIR
to path, and setROOTDIR
to its parent, if not already set.If you want to set the ROOTDIR to a different path, you must call setrootdir first.
-
static
setcurdir
(path: str)¶ Set the CURDIR environment variable if not already set.
-
static
setdefault
(name: str, value: str)¶ Set an environment variable named name to value if not already set and returns its value.
-
static
setrootdir
(path: str)¶ Set the ROOTDIR environment variable if not already set.
-
static
shrun
(cmdline: str, shell=True, check=True, **kwargs)¶ Wrapper around
subprocess.run()
that pass the cmdline directly, set the default value of check and shell to True.
-
subparsers
= None¶ The subparsers object from argparse module used for all commands.
See
add_commands()
for more details.
-
which
(exe, path=None)¶ Return the full path of the executable that would be run with exe.
If path is include, it will be used for check. Elsewhere, The
BINDIR
is checked first.Raises:
ExecutableNotInPath
if the file doesn’t exist.Note:
This function behaves differently on Windows. Please check theshutil.which()
function for more information.
-