Skip to content

Environment


Classes that represent the environment of a study.

GitDependency

Bases: Dependency

Environment GitDependency class for substituting a git dependency.

Source code in maestrowf/datastructures/environment/gitdependency.py
class GitDependency(Dependency):
    """Environment GitDependency class for substituting a git dependency."""

    def __init__(self, name, value, path, token='$', **kwargs):
        """
        Initialize the GitDependency class.

        The GitDepedency represents a dependency that is stored in a user
        accessible remote repository that supports the git protocol. Each
        GitDependency acquires itself from its designated remote repository;
        otherwise, this class operates like the Variable class and
        represents substrings that can be present within String data that are
        meant to be replaced. The general format that such items take is
        generally expressed as '<token>(<name>)', and will be replaced
        with the value specified.

        Currently, the GitDependency class only supports three optional
        parameters: branch, hash, and tag. Each operate as their name specifies
        according to how they would be used in git. The class will acquire the
        specific repository in accordance with a specified optional (example:
        if a tag is specfied, the class will clone then checkout the tag). The
        only caveat to the optionals is that only one may be used at a time.

        :params name: String name that refers to a GitDependency instance.
        :params value: The URL (SSH or FTP) to the remote git repository.
        :params path: The local path where the copy of the repository is
            cloned to.
        :params token: String of expected character(s) that appear at the
            beginning of a substring representing the dependency variable.
        :params kwargs: Optional keyword arguments - Only valid optionals are
            "branch", "hash", and "tag".
        """
        # Required base information
        self.name = name
        self.url = value
        # If the path is a valid path that exists, get abspath
        if os.path.exists(path):
            self.path = os.path.abspath(path)
        else:
            # Otherwise, store it as it is.
            self.path = path

        self.token = token

        # Optional information
        self.hash = kwargs.pop("hash", "")
        self.tag = kwargs.pop("tag", "")
        self.branch = kwargs.pop("branch", "")

        self._verification("PathDependency initialized without complete "
                           " settings. Set required [name, value] before "
                           "calling methods.")
        self._is_acquired = False

    def get_var(self):
        """
        Get the variable representation of the dependency's name.

        :returns: String of the Dependencies's name in token form.
        """
        return "{}({})".format(self.token, self.name)

    def substitute(self, data):
        """
        Substitute the dependency's value for its notation.

        :param data: String to substitute dependency into.
        :returns: String with the dependency's name replaced with its value.
        """
        if not self._verify():
            error = "Ensure that all required fields (name, value)," \
                    "are populated and that value is a valid path."
            logger.exception(error)
            raise ValueError(error)

        path = os.path.join(self.path, self.name)
        logger.debug("%s: %s", self.get_var(),
                     data.replace(self.get_var(), path))
        return data.replace(self.get_var(), path)

    def acquire(self, substitutions=None):
        """
        Acquire the dependency specified by the PathDependency.

        The GitDependency will clone the remote repository specified by the
        instance's value to the local repository specified by path. If a commit
        hash is specified, acquire will attempt to rebase to the repository
        version described by the hash. Alternatively, if a tag is specfied
        acquire will attempt to checkout the version labeled by the tag.

        :param substitutions: List of Substitution objects that can be applied.
        """
        if self._is_acquired:
            return

        if not self._verify():
            error = "Ensure that all required fields (name, value, " \
                    "path), are populated and that value is a " \
                    "valid path."
            logger.error(error)
            raise ValueError(error)

        if substitutions:
            for substitution in substitutions:
                self.path = substitution.substitute(self.path)
                self.url = substitution.substitute(self.url)

        path = os.path.join(self.path, self.name)

        # Moved the path existence here because git doesn't actually return a
        # specific enough error code.
        if os.path.exists(path):
            msg = "Destination path '{}' already exists and is not an " \
                  "empty directory.".format(path)
            logger.error(msg)
            raise Exception(msg)

        logger.info("Checking for connectivity to '%s'", self.url)
        p = start_process(["git", "ls-remote", self.url], shell=False)
        p.communicate()
        if p.returncode != 0:
            msg = "Connectivity check failed. Check that you have " \
                "permissions to the specified repository, that the URL is " \
                "correct, and that you have network connectivity. (url = {})" \
                .format(self.url)
            logger.error(msg)
            raise RuntimeError(msg)
        logger.info("Connectivity achieved!")

        logger.info("Cloning '%s' from '%s'...", self.name, self.url)
        clone = start_process(["git", "clone", self.url, path], shell=False)
        clone.communicate()
        if clone.returncode != 0:
            msg = "Failed to acquire GitDependency named '{}'. Check " \
              "that repository URL ({}) and repository local path ({}) " \
              "are valid.".format(self.name, self.url, path)
            logger.error(msg)
            raise Exception(msg)

        if self.hash:
            logger.info("Checking out SHA1 hash '%s'...", self.hash)
            chkout = start_process(["git", "checkout", self.hash],
                                   cwd=path, shell=False)
            retcode = chkout.wait()

            if retcode != 0:
                msg = "Unable to checkout SHA1 hash '{}' for the repository" \
                      " located at {}." \
                      .format(self.hash, self.url)
                logger.error(msg)
                raise ValueError(msg)

        if self.tag:
            logger.info("Checking out git tag '%s'...", self.tag)
            tag = "tags/{}".format(self.tag)
            chkout = start_process(["git", "checkout", tag],
                                   cwd=path, shell=False)

            retcode = chkout.wait()

            if retcode != 0:
                msg = "Unable to checkout tag '{}' for the repository" \
                      " located at {}".format(self.tag, self.url)
                logger.error(msg)
                raise ValueError(msg)

        if self.branch:
            logger.info("Checking out git branch '%s'...", self.branch)
            chkout = start_process(["git", "checkout", self.branch],
                                   cwd=path, shell=False)

            retcode = chkout.wait()

            if retcode != 0:
                msg = "Unable to checkout branch '{}' for the repository" \
                      " located at {}".format(self.tag, self.url)
                logger.error(msg)
                raise ValueError(msg)

        if not os.path.exists(self.path):
            error = "The specified path '{}' does not exist.".format(self.name)
            logger.exception(error)
            raise ValueError(error)

        self._is_acquired = True

    def _verify(self):
        """
        Verify that the necessary Dependency fields are populated.

        :returns: True if Dependency is valid, False otherwise.
        """
        valid_param_pattern = re.compile(r"\w+")
        required = bool(re.search(valid_param_pattern, self.name) and
                        re.search(valid_param_pattern, self.url) and
                        re.search(valid_param_pattern, self.path) and
                        self.token)

        opt_args = set([self.branch, self.hash, self.tag])
        opt_args.discard("")
        if len(opt_args) > 1:
            msg = "A GitDependency cannot specify both a commit hash and " \
                  "release tag. Specify one or the other, but not both."
            logger.error(msg)
            raise ValueError(msg)
        elif self.hash:
            optional = bool(re.search(valid_param_pattern, self.hash))
        elif self.tag:
            optional = bool(re.search(valid_param_pattern, self.tag))
        elif self.branch:
            optional = bool(re.search(valid_param_pattern, self.branch))
        else:
            optional = True

        return required and optional

    def __str__(self):
        """
        Generate the string representation of the object.

        :returns: A string with the token form of the variable.
        """
        return str(self.get_var())

__init__(name, value, path, token='$', **kwargs)

Initialize the GitDependency class.

The GitDepedency represents a dependency that is stored in a user accessible remote repository that supports the git protocol. Each GitDependency acquires itself from its designated remote repository; otherwise, this class operates like the Variable class and represents substrings that can be present within String data that are meant to be replaced. The general format that such items take is generally expressed as '()', and will be replaced with the value specified.

Currently, the GitDependency class only supports three optional parameters: branch, hash, and tag. Each operate as their name specifies according to how they would be used in git. The class will acquire the specific repository in accordance with a specified optional (example: if a tag is specfied, the class will clone then checkout the tag). The only caveat to the optionals is that only one may be used at a time.

Parameters:

Name Type Description Default
name

String name that refers to a GitDependency instance.

required
value

The URL (SSH or FTP) to the remote git repository.

required
path

The local path where the copy of the repository is cloned to.

required
token

String of expected character(s) that appear at the beginning of a substring representing the dependency variable.

'$'
kwargs

Optional keyword arguments - Only valid optionals are "branch", "hash", and "tag".

{}
Source code in maestrowf/datastructures/environment/gitdependency.py
def __init__(self, name, value, path, token='$', **kwargs):
    """
    Initialize the GitDependency class.

    The GitDepedency represents a dependency that is stored in a user
    accessible remote repository that supports the git protocol. Each
    GitDependency acquires itself from its designated remote repository;
    otherwise, this class operates like the Variable class and
    represents substrings that can be present within String data that are
    meant to be replaced. The general format that such items take is
    generally expressed as '<token>(<name>)', and will be replaced
    with the value specified.

    Currently, the GitDependency class only supports three optional
    parameters: branch, hash, and tag. Each operate as their name specifies
    according to how they would be used in git. The class will acquire the
    specific repository in accordance with a specified optional (example:
    if a tag is specfied, the class will clone then checkout the tag). The
    only caveat to the optionals is that only one may be used at a time.

    :params name: String name that refers to a GitDependency instance.
    :params value: The URL (SSH or FTP) to the remote git repository.
    :params path: The local path where the copy of the repository is
        cloned to.
    :params token: String of expected character(s) that appear at the
        beginning of a substring representing the dependency variable.
    :params kwargs: Optional keyword arguments - Only valid optionals are
        "branch", "hash", and "tag".
    """
    # Required base information
    self.name = name
    self.url = value
    # If the path is a valid path that exists, get abspath
    if os.path.exists(path):
        self.path = os.path.abspath(path)
    else:
        # Otherwise, store it as it is.
        self.path = path

    self.token = token

    # Optional information
    self.hash = kwargs.pop("hash", "")
    self.tag = kwargs.pop("tag", "")
    self.branch = kwargs.pop("branch", "")

    self._verification("PathDependency initialized without complete "
                       " settings. Set required [name, value] before "
                       "calling methods.")
    self._is_acquired = False

__str__()

Generate the string representation of the object.

Returns:

Type Description

A string with the token form of the variable.

Source code in maestrowf/datastructures/environment/gitdependency.py
def __str__(self):
    """
    Generate the string representation of the object.

    :returns: A string with the token form of the variable.
    """
    return str(self.get_var())

acquire(substitutions=None)

Acquire the dependency specified by the PathDependency.

The GitDependency will clone the remote repository specified by the instance's value to the local repository specified by path. If a commit hash is specified, acquire will attempt to rebase to the repository version described by the hash. Alternatively, if a tag is specfied acquire will attempt to checkout the version labeled by the tag.

Parameters:

Name Type Description Default
substitutions

List of Substitution objects that can be applied.

None
Source code in maestrowf/datastructures/environment/gitdependency.py
def acquire(self, substitutions=None):
    """
    Acquire the dependency specified by the PathDependency.

    The GitDependency will clone the remote repository specified by the
    instance's value to the local repository specified by path. If a commit
    hash is specified, acquire will attempt to rebase to the repository
    version described by the hash. Alternatively, if a tag is specfied
    acquire will attempt to checkout the version labeled by the tag.

    :param substitutions: List of Substitution objects that can be applied.
    """
    if self._is_acquired:
        return

    if not self._verify():
        error = "Ensure that all required fields (name, value, " \
                "path), are populated and that value is a " \
                "valid path."
        logger.error(error)
        raise ValueError(error)

    if substitutions:
        for substitution in substitutions:
            self.path = substitution.substitute(self.path)
            self.url = substitution.substitute(self.url)

    path = os.path.join(self.path, self.name)

    # Moved the path existence here because git doesn't actually return a
    # specific enough error code.
    if os.path.exists(path):
        msg = "Destination path '{}' already exists and is not an " \
              "empty directory.".format(path)
        logger.error(msg)
        raise Exception(msg)

    logger.info("Checking for connectivity to '%s'", self.url)
    p = start_process(["git", "ls-remote", self.url], shell=False)
    p.communicate()
    if p.returncode != 0:
        msg = "Connectivity check failed. Check that you have " \
            "permissions to the specified repository, that the URL is " \
            "correct, and that you have network connectivity. (url = {})" \
            .format(self.url)
        logger.error(msg)
        raise RuntimeError(msg)
    logger.info("Connectivity achieved!")

    logger.info("Cloning '%s' from '%s'...", self.name, self.url)
    clone = start_process(["git", "clone", self.url, path], shell=False)
    clone.communicate()
    if clone.returncode != 0:
        msg = "Failed to acquire GitDependency named '{}'. Check " \
          "that repository URL ({}) and repository local path ({}) " \
          "are valid.".format(self.name, self.url, path)
        logger.error(msg)
        raise Exception(msg)

    if self.hash:
        logger.info("Checking out SHA1 hash '%s'...", self.hash)
        chkout = start_process(["git", "checkout", self.hash],
                               cwd=path, shell=False)
        retcode = chkout.wait()

        if retcode != 0:
            msg = "Unable to checkout SHA1 hash '{}' for the repository" \
                  " located at {}." \
                  .format(self.hash, self.url)
            logger.error(msg)
            raise ValueError(msg)

    if self.tag:
        logger.info("Checking out git tag '%s'...", self.tag)
        tag = "tags/{}".format(self.tag)
        chkout = start_process(["git", "checkout", tag],
                               cwd=path, shell=False)

        retcode = chkout.wait()

        if retcode != 0:
            msg = "Unable to checkout tag '{}' for the repository" \
                  " located at {}".format(self.tag, self.url)
            logger.error(msg)
            raise ValueError(msg)

    if self.branch:
        logger.info("Checking out git branch '%s'...", self.branch)
        chkout = start_process(["git", "checkout", self.branch],
                               cwd=path, shell=False)

        retcode = chkout.wait()

        if retcode != 0:
            msg = "Unable to checkout branch '{}' for the repository" \
                  " located at {}".format(self.tag, self.url)
            logger.error(msg)
            raise ValueError(msg)

    if not os.path.exists(self.path):
        error = "The specified path '{}' does not exist.".format(self.name)
        logger.exception(error)
        raise ValueError(error)

    self._is_acquired = True

get_var()

Get the variable representation of the dependency's name.

Returns:

Type Description

String of the Dependencies's name in token form.

Source code in maestrowf/datastructures/environment/gitdependency.py
def get_var(self):
    """
    Get the variable representation of the dependency's name.

    :returns: String of the Dependencies's name in token form.
    """
    return "{}({})".format(self.token, self.name)

substitute(data)

Substitute the dependency's value for its notation.

Parameters:

Name Type Description Default
data

String to substitute dependency into.

required

Returns:

Type Description

String with the dependency's name replaced with its value.

Source code in maestrowf/datastructures/environment/gitdependency.py
def substitute(self, data):
    """
    Substitute the dependency's value for its notation.

    :param data: String to substitute dependency into.
    :returns: String with the dependency's name replaced with its value.
    """
    if not self._verify():
        error = "Ensure that all required fields (name, value)," \
                "are populated and that value is a valid path."
        logger.exception(error)
        raise ValueError(error)

    path = os.path.join(self.path, self.name)
    logger.debug("%s: %s", self.get_var(),
                 data.replace(self.get_var(), path))
    return data.replace(self.get_var(), path)

PathDependency

Bases: Dependency

Environment PathDependency class for substituting a path dependency.

Source code in maestrowf/datastructures/environment/pathdependency.py
class PathDependency(Dependency):
    """Environment PathDependency class for substituting a path dependency."""

    def __init__(self, name, value, token='$'):
        """
        Initialize the PathDependency class.

        The PathDependency represents a dependency that is stored in the local
        file system. These dependencies can be things like shared group folders
        or local directories in user space which contain items the study needs
        to run. Otherwise, this class operates like the Variable class and
        represents substrings that can be present within String data that are
        meant to be replaced. The general format that such items take is
        generally expressed as '<token>(<name>)', and will be replaced
        with the value specified.

        :params name: String name that refers to a PathDependency instance.
        :params value: The value to substitute for the PathDependency instance.
        :params token: String of expected character(s) that appear at the
            beginning of a substring representing the dependency variable.
        """
        self.name = name
        self.value = os.path.abspath(value)
        self.token = token

        self._verification("PathDependency initialized without complete"
                           " settings. Set required [name, value] before "
                           "calling methods.")
        self._is_acquired = False

    def get_var(self):
        """
        Get the variable representation of the dependency's name.

        :returns: String of the Dependencies's name in token form.
        """
        return "{}({})".format(self.token, self.name)

    def substitute(self, data):
        """
        Substitute the dependency's value for its notation.

        :param data: String to substitute dependency into.
        :returns: String with the dependency's name replaced with its value.
        """
        if not self._verify():
            error = "Ensure that all required fields (name, value)," \
                    "are populated and that value is a valid path."
            logger.exception(error)
            raise ValueError(error)

        logger.debug("%s: %s", self.get_var(),
                     data.replace(self.get_var(), self.value))
        return data.replace(self.get_var(), self.value)

    def acquire(self, substitutions=None):
        """
        Acquire the dependency specified by the PathDependency.

        The PathDependency is simply a path that already exists, so the method
        doesn't actually acquire anything, but it does verify that the path
        exists.

        :param substitutions: List of Substitution objects that can be applied.
        """
        if self._is_acquired:
            return

        if not self._verify():
            error = "Ensure that all required fields (name, " \
                    "value), are populated and that value is a " \
                    "valid path."
            logger.exception(error)
            raise ValueError(error)

        if not os.path.exists(self.value):
            error = "The specified path '{}' does not exist.".format(self.name)
            logger.exception(error)
            raise ValueError(error)

        self._is_acquired = True

    def _verify(self):
        """
        Verify that the necessary Dependency fields are populated.

        :returns: True if Dependency is valid, False otherwise.
        """
        valid_param_pattern = re.compile(r"\w+")
        return bool(re.search(valid_param_pattern, self.name) and
                    re.search(valid_param_pattern, self.value) and
                    self.token)

    def __str__(self):
        """
        Generate the string representation of the object.

        :returns: A string with the token form of the variable.
        """
        return str(self.get_var())

__init__(name, value, token='$')

Initialize the PathDependency class.

The PathDependency represents a dependency that is stored in the local file system. These dependencies can be things like shared group folders or local directories in user space which contain items the study needs to run. Otherwise, this class operates like the Variable class and represents substrings that can be present within String data that are meant to be replaced. The general format that such items take is generally expressed as '()', and will be replaced with the value specified.

Parameters:

Name Type Description Default
name

String name that refers to a PathDependency instance.

required
value

The value to substitute for the PathDependency instance.

required
token

String of expected character(s) that appear at the beginning of a substring representing the dependency variable.

'$'
Source code in maestrowf/datastructures/environment/pathdependency.py
def __init__(self, name, value, token='$'):
    """
    Initialize the PathDependency class.

    The PathDependency represents a dependency that is stored in the local
    file system. These dependencies can be things like shared group folders
    or local directories in user space which contain items the study needs
    to run. Otherwise, this class operates like the Variable class and
    represents substrings that can be present within String data that are
    meant to be replaced. The general format that such items take is
    generally expressed as '<token>(<name>)', and will be replaced
    with the value specified.

    :params name: String name that refers to a PathDependency instance.
    :params value: The value to substitute for the PathDependency instance.
    :params token: String of expected character(s) that appear at the
        beginning of a substring representing the dependency variable.
    """
    self.name = name
    self.value = os.path.abspath(value)
    self.token = token

    self._verification("PathDependency initialized without complete"
                       " settings. Set required [name, value] before "
                       "calling methods.")
    self._is_acquired = False

__str__()

Generate the string representation of the object.

Returns:

Type Description

A string with the token form of the variable.

Source code in maestrowf/datastructures/environment/pathdependency.py
def __str__(self):
    """
    Generate the string representation of the object.

    :returns: A string with the token form of the variable.
    """
    return str(self.get_var())

acquire(substitutions=None)

Acquire the dependency specified by the PathDependency.

The PathDependency is simply a path that already exists, so the method doesn't actually acquire anything, but it does verify that the path exists.

Parameters:

Name Type Description Default
substitutions

List of Substitution objects that can be applied.

None
Source code in maestrowf/datastructures/environment/pathdependency.py
def acquire(self, substitutions=None):
    """
    Acquire the dependency specified by the PathDependency.

    The PathDependency is simply a path that already exists, so the method
    doesn't actually acquire anything, but it does verify that the path
    exists.

    :param substitutions: List of Substitution objects that can be applied.
    """
    if self._is_acquired:
        return

    if not self._verify():
        error = "Ensure that all required fields (name, " \
                "value), are populated and that value is a " \
                "valid path."
        logger.exception(error)
        raise ValueError(error)

    if not os.path.exists(self.value):
        error = "The specified path '{}' does not exist.".format(self.name)
        logger.exception(error)
        raise ValueError(error)

    self._is_acquired = True

get_var()

Get the variable representation of the dependency's name.

Returns:

Type Description

String of the Dependencies's name in token form.

Source code in maestrowf/datastructures/environment/pathdependency.py
def get_var(self):
    """
    Get the variable representation of the dependency's name.

    :returns: String of the Dependencies's name in token form.
    """
    return "{}({})".format(self.token, self.name)

substitute(data)

Substitute the dependency's value for its notation.

Parameters:

Name Type Description Default
data

String to substitute dependency into.

required

Returns:

Type Description

String with the dependency's name replaced with its value.

Source code in maestrowf/datastructures/environment/pathdependency.py
def substitute(self, data):
    """
    Substitute the dependency's value for its notation.

    :param data: String to substitute dependency into.
    :returns: String with the dependency's name replaced with its value.
    """
    if not self._verify():
        error = "Ensure that all required fields (name, value)," \
                "are populated and that value is a valid path."
        logger.exception(error)
        raise ValueError(error)

    logger.debug("%s: %s", self.get_var(),
                 data.replace(self.get_var(), self.value))
    return data.replace(self.get_var(), self.value)

Script

Bases: Source

Script class for applying changes to the execution environment.

Source code in maestrowf/datastructures/environment/script.py
class Script(Source):
    """Script class for applying changes to the execution environment."""

    # TODO: Sourcing is an issue. We need to figure out a way to handle this.
    def __init__(self, source):
        """
        Initialize the Script class.

        :params source: The command for changing the execution environment.
        """
        self.source = source
        self._verification("Script initialized without complete settings. Set"
                           " source before calling methods.")

    def apply(self, cmds):
        """
        Apply the Script source to the specified list of commands.

        :param cmds: List of commands to add source to.
        :returns: List of commands with the source prepended.
        """
        return [self.source] + list(cmds)

    def _verify(self):
        """
        Verify the Script object's contents.

        :returns: True if the Script object is valid, False otherwise.
        """
        valid_param_pattern = re.compile(r"\w+")
        return bool(re.search(valid_param_pattern, self.source))

__init__(source)

Initialize the Script class.

Parameters:

Name Type Description Default
source

The command for changing the execution environment.

required
Source code in maestrowf/datastructures/environment/script.py
def __init__(self, source):
    """
    Initialize the Script class.

    :params source: The command for changing the execution environment.
    """
    self.source = source
    self._verification("Script initialized without complete settings. Set"
                       " source before calling methods.")

apply(cmds)

Apply the Script source to the specified list of commands.

Parameters:

Name Type Description Default
cmds

List of commands to add source to.

required

Returns:

Type Description

List of commands with the source prepended.

Source code in maestrowf/datastructures/environment/script.py
def apply(self, cmds):
    """
    Apply the Script source to the specified list of commands.

    :param cmds: List of commands to add source to.
    :returns: List of commands with the source prepended.
    """
    return [self.source] + list(cmds)

Variable

Bases: Substitution

Environment Variable class capable of substituting itself into strings.

Derived from the Substitution EnvObject class which requires that a substitution be able to inject itself into data.

Source code in maestrowf/datastructures/environment/variable.py
class Variable(Substitution):
    """
    Environment Variable class capable of substituting itself into strings.

    Derived from the Substitution EnvObject class which requires that a
    substitution be able to inject itself into data.
    """

    def __init__(self, name, value, token='$'):
        """
        Initialize the Variable class.

        The Variable represents substrings that can be present within String
        data that are meant to be replaced. The general format that such items
        take is generally expressed as '<token>(<name>)', and will be replaced
        with the value specified.

        :params name: String name that refers to a Variable instance.
        :params value: The value to substitute for the Variable instance.
        :params token: String of expected character(s) that appear at the
            beginning of a substring representing the variable.
        """
        self.name = name
        self.value = value
        self.token = token

        if not self._verify():
            msg = "Variable initialized without complete settings. Set " \
                           "required [name, value] before calling methods."
            logger.exception(msg)
            raise ValueError(msg)

    def get_var(self):
        """
        Get the variable representation of the variable's name.

        :returns: String of the Variable's name in token form.
        """
        return "{}({})".format(self.token, self.name)

    def substitute(self, data):
        """
        Substitute the variable's value for its notation.

        :param data: String to substitute variable into.
        :returns: String with the variable's name replaced with its value.
        """
        self._verification("Attempting to substitute a variable that is not"
                           " complete.")
        logger.debug("%s: %s", self.get_var(),
                     data.replace(self.get_var(), str(self.value)))
        return data.replace(self.get_var(), str(self.value))

    def _verify(self):
        """
        Verify that the necessary Variable fields are populated.

        :returns: True if Variable is valid, False otherwise.
        """
        _valid = bool(self.name) and self.value is not None
        return _valid

    def __str__(self):
        """
        Generate the string representation of the objects.

        :returns: A string with the token form of the variable.
        """
        return self.get_var()

__init__(name, value, token='$')

Initialize the Variable class.

The Variable represents substrings that can be present within String data that are meant to be replaced. The general format that such items take is generally expressed as '()', and will be replaced with the value specified.

Parameters:

Name Type Description Default
name

String name that refers to a Variable instance.

required
value

The value to substitute for the Variable instance.

required
token

String of expected character(s) that appear at the beginning of a substring representing the variable.

'$'
Source code in maestrowf/datastructures/environment/variable.py
def __init__(self, name, value, token='$'):
    """
    Initialize the Variable class.

    The Variable represents substrings that can be present within String
    data that are meant to be replaced. The general format that such items
    take is generally expressed as '<token>(<name>)', and will be replaced
    with the value specified.

    :params name: String name that refers to a Variable instance.
    :params value: The value to substitute for the Variable instance.
    :params token: String of expected character(s) that appear at the
        beginning of a substring representing the variable.
    """
    self.name = name
    self.value = value
    self.token = token

    if not self._verify():
        msg = "Variable initialized without complete settings. Set " \
                       "required [name, value] before calling methods."
        logger.exception(msg)
        raise ValueError(msg)

__str__()

Generate the string representation of the objects.

Returns:

Type Description

A string with the token form of the variable.

Source code in maestrowf/datastructures/environment/variable.py
def __str__(self):
    """
    Generate the string representation of the objects.

    :returns: A string with the token form of the variable.
    """
    return self.get_var()

get_var()

Get the variable representation of the variable's name.

Returns:

Type Description

String of the Variable's name in token form.

Source code in maestrowf/datastructures/environment/variable.py
def get_var(self):
    """
    Get the variable representation of the variable's name.

    :returns: String of the Variable's name in token form.
    """
    return "{}({})".format(self.token, self.name)

substitute(data)

Substitute the variable's value for its notation.

Parameters:

Name Type Description Default
data

String to substitute variable into.

required

Returns:

Type Description

String with the variable's name replaced with its value.

Source code in maestrowf/datastructures/environment/variable.py
def substitute(self, data):
    """
    Substitute the variable's value for its notation.

    :param data: String to substitute variable into.
    :returns: String with the variable's name replaced with its value.
    """
    self._verification("Attempting to substitute a variable that is not"
                       " complete.")
    logger.debug("%s: %s", self.get_var(),
                 data.replace(self.get_var(), str(self.value)))
    return data.replace(self.get_var(), str(self.value))