Skip to content

Pathdependency

Class representing a file system path dependency.

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)