Skip to content

Commit

Permalink
Explicit sandbox config section names, close #165.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Oct 3, 2023
1 parent dca607b commit 52fe3f9
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 23 deletions.
18 changes: 9 additions & 9 deletions law.cfg.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
; information. The law home directory defaults to "$LAW_HOME" or "$HOME/.law".

; --- Configuration file resolution order ----------------------------------------------------------
; Law configuration files (like the one you are just reading) are looked up in a certain order:
; $LAW_CONFIG_FILE, ./law.cfg, "config" in the law home directory, or "etc/law/config". The config
; parser only reads the first existing file. Please also note the optional synchronization with
; environment variables described below.
; Law configuration files are looked up in a certain order: "$LAW_CONFIG_FILE", "./law.cfg",
; "config" in the law home directory, or "etc/law/config". The config parser only reads the first
; existing file. Please also note the optional synchronization with environment variables described
; below.

; --- Inheritance and extensions -------------------------------------------------------------------
; The law configuration can inherit from or can be extended by additional configuration files. See
Expand Down Expand Up @@ -1043,7 +1043,7 @@
; Note:
; Here you can define environment variables via key-value pairs that are accessible in a bash
; sandbox. When an option has no value, i.e., when only a key is given, the value of the variable of
; the current environment is used.
; the host environment is used.
; The environment variables defined in this section are applied to all bash sandboxes. To configure
; variables per bash initialization file, create a section "bash_sandbox_env_<init_file>" with the
; desired values.
Expand Down Expand Up @@ -1087,7 +1087,7 @@
; Note:
; Here you can define environment variables via key-value pairs that are accessible in a venv
; sandbox. When an option has no value, i.e., when only a key is given, the value of the variable of
; the current environment is used.
; the host environment is used.
; The environment variables defined in this section are applied to all venv sandboxes. To configure
; variables per venv directory, create a section "venv_sandbox_env_<venv_dir>" with the desired
; values.
Expand Down Expand Up @@ -1160,7 +1160,7 @@
; Note:
; Here you can define environment variables via key-value pairs that are accessible in a docker
; sandbox. When an option has no value, i.e., when only a key is given, the value of the variable of
; the current environment is used.
; the host environment is used.
; The environment variables defined in this section are applied to all docker sandboxes. To
; configure variables per image, create a section "docker_sandbox_env_<image_name>" with the desired
; values.
Expand Down Expand Up @@ -1255,7 +1255,7 @@
; Note:
; Here you can define environment variables via key-value pairs that are accessible in a singularity
; sandbox. When an option has no value, i.e., when only a key is given, the value of the variable of
; the current environment is used.
; the host environment is used.
; The environment variables defined in this section are applied to all singularity sandboxes. To
; configure variables per image, create a section "singularity_sandbox_env_<image_name>" with the
; desired values.
Expand Down Expand Up @@ -1316,7 +1316,7 @@
; Note:
; Here you can define environment variables via key-value pairs that are accessible in a cmssw
; sandbox. When an option has no value, i.e., when only a key is given, the value of the variable of
; the current environment is used.
; the host environment is used.
; The environment variables defined in this section are applied to all cmssw sandboxes. To
; configure variables per cmssw version, create a section "cmssw_sandbox_env_<cmssw_version>" with
; the desired values.
25 changes: 13 additions & 12 deletions law/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,16 @@ def include(self, filename, *args, **kwargs):
skip_luigi_sync=True)
self.update(p._sections, *args, **kwargs)

def get_default(self, section, option, default=None, type=None, expand_vars=False,
def get_default(self, section, option, default=no_value, type=None, expand_vars=False,
expand_user=False, split_csv=False, dereference=True, default_when_none=True,
_skip_refs=None):
""" get_default(section, option, default=None, type=None, expand_vars=False, expand_user=False, split_csv=False, dereference=True, default_when_none=True)
""" get_default(section, option, default=no_value, type=None, expand_vars=False, expand_user=False, split_csv=False, dereference=True, default_when_none=True)
Returns the config value defined by *section* and *option*. When either the section or the
option does not exist, the *default* value is returned instead. When *type* is set, it must
be either `"str"`, `"int"`, `"float"`, or `"boolean"`. When *expand_vars* is *True*,
environment variables are expanded. When *expand_user* is *True*, user variables are
expanded as well. Sequences of values can be identified, split by comma and returned as a
list when *split_csv* is *True*, which will also trigger brace expansion.
option do not exist and a *default* value is provided, this value returned instead. When
*type* is set, it must be either `"str"`, `"int"`, `"float"`, or `"boolean"`. When
*expand_vars* is *True*, environment variables are expanded. When *expand_user* is *True*,
user variables are expanded as well. Sequences of values can be identified, split by comma
and returned as a list when *split_csv* is *True*, which will also trigger brace expansion.
Also, options retrieved by this method are allowed to refer to values of other options
within the config, even to those in other sections. The syntax for config references is
Expand All @@ -382,11 +382,12 @@ def get_default(self, section, option, default=None, type=None, expand_vars=Fals
This behavior is the default and, if desired, can be disabled by setting *dereference* to
*False*. When the reference is not resolvable, the default value is returned.
When *default_when_none* is *True* and the option was found but its value is *None* or
``"None"`` (case-insensitive), the *default* is returned.
When *default_when_none* is *True*, a *default* value is provided, and the option was found
but its value is *None* or ``"None"`` (case-insensitive), the *default* is returned.
""" # noqa
# return the default when either the section or the option does not exist
if not self.has_section(section) or not self.has_option(section, option):
default_set = default != no_value
if (not self.has_section(section) or not self.has_option(section, option)) and default_set:
return default

# get the value
Expand Down Expand Up @@ -418,10 +419,10 @@ def get_default(self, section, option, default=None, type=None, expand_vars=Fals
default_when_none=default_when_none, _skip_refs=_skip_refs)

# interpret None and "None" as missing?
if default_when_none:
if default_when_none and default_set:
if value is None:
return default
elif isinstance(value, six.string_types) and value.lower() == "none":
if isinstance(value, six.string_types) and value.lower() == "none":
return default

# helper for optional type conversion
Expand Down
2 changes: 2 additions & 0 deletions law/contrib/docker/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class DockerSandbox(Sandbox):

sandbox_type = "docker"

config_section_prefix = sandbox_type

@property
def image(self):
return self.name
Expand Down
2 changes: 2 additions & 0 deletions law/contrib/singularity/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class SingularitySandbox(Sandbox):

sandbox_type = "singularity"

config_section_prefix = sandbox_type

@property
def image(self):
return self.name
Expand Down
2 changes: 1 addition & 1 deletion law/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def setup_logger(logger, level=None, add_console_handler=None, clear=False, forc
# sanitize the level
if level is None:
from law.config import Config
level = Config.instance().get_expanded("logging", name)
level = Config.instance().get_expanded("logging", name, None)
if isinstance(level, six.string_types):
level = getattr(logging, level.upper(), None)
if level is None:
Expand Down
17 changes: 16 additions & 1 deletion law/sandbox/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ def __repr__(self):


class Sandbox(six.with_metaclass(ABCMeta, object)):
"""
Sandbox definition.
The config section used by instances if this or inheriting classes constructed using
:py:attr:`config_section_prefix` followed by ``"_sandbox"`` and optional postifxes. The minimal
set of options in the main section are:
- ``"stagein_dir_name"`` (usually ``"stagein"``)
- ``"stageout_dir_name"`` (usually ``"stageout"``)
- ``"law_executable"`` (usually ``"law"``)
"""

delimiter = "::"

Expand Down Expand Up @@ -178,6 +189,10 @@ def scheduler_on_host(self):
def force_local_scheduler(self):
return False

@abstractproperty
def config_section_prefix(self):
return

@abstractproperty
def env_cache_key(self):
return
Expand Down Expand Up @@ -218,7 +233,7 @@ def get_custom_config_section_postfix(self):
return self.name

def get_config_section(self, postfix=None):
section = self.sandbox_type + "_sandbox"
section = self.config_section_prefix + "_sandbox"
if postfix:
section += "_" + postfix

Expand Down
2 changes: 2 additions & 0 deletions law/sandbox/bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class BashSandbox(Sandbox):

sandbox_type = "bash"

config_section_prefix = sandbox_type

@property
def script(self):
return os.path.expandvars(os.path.expanduser(self.name))
Expand Down
2 changes: 2 additions & 0 deletions law/sandbox/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class VenvSandbox(Sandbox):

sandbox_type = "venv"

config_section_prefix = sandbox_type

@property
def venv_dir(self):
return os.path.expandvars(os.path.expanduser(self.name))
Expand Down

0 comments on commit 52fe3f9

Please sign in to comment.