# Copyright 2022-2026 The Ramble Authors
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
import ramble.util.colors as color
[docs]
class PackageManagerRequirement:
"""Class representing a package manager requirement for a modifier"""
print_attr_map = [
("command", "Command"),
("validation_type", "Validation Type"),
("package_manager", "Package Manager"),
("regex", "Regex"),
("when", "When"),
]
def __init__(
self, command: str, validation_type: str, regex: str, package_manager: str, when: list
):
"""Constructor for PackageManagerRequirement object
Args:
command: Package manager command to execute, when evaluating the requirement
validation_type: Type of validation to perform on output of command
regex: Regular expression to use when validation_type is either 'contains_regex'
or 'does_no_contain_regex'
package_manager: Name of the package manager this requirement should apply to
when: List of when conditions this requirement should apply to
"""
self.name = command # In order to print correctly from info, all obj need 'name'
self.command = command
self.validation_type = validation_type
self.regex = regex
self.package_manager = package_manager
self.when = when
def __str__(self):
if not hasattr(self, "_str_indent"):
self._str_indent = 0
return self.as_str(n_indent=self._str_indent)
[docs]
def as_str(self, n_indent: int = 0, verbose: bool = False):
"""String representation of this requirement
Args:
n_indent: Number of spaces to indent string lines with
verbose: Print verbose output
Returns:
(str): Representation of this requirement
"""
indentation = " " * n_indent
if verbose:
out_str = color.title_color(f"{indentation}{self.name}:\n", n_indent)
for attr_name, print_name in self.print_attr_map:
attr_val = getattr(self, attr_name, None)
if attr_val:
out_str += (
f"{indentation} "
f"{color.title_color(print_name, n_indent=n_indent + 4)}: "
f"{str(attr_val)}\n"
)
else:
out_str = f"{indentation}{self.name}"
return out_str