diff --git a/pyanaconda/payload/__init__.py b/pyanaconda/payload/__init__.py index 35dd81e..481195b 100644 --- a/pyanaconda/payload/__init__.py +++ b/pyanaconda/payload/__init__.py @@ -139,7 +139,47 @@ class DependencyError(PayloadError): class PayloadInstallError(PayloadError): pass -PayloadRequirement = namedtuple('PayloadRequirement', ['id', 'reasons', 'strong']) +PayloadRequirementReason = namedtuple('PayloadRequirementReason', ['reason', 'strong']) + +class PayloadRequirement(object): + """An object to store a payload requirement with info about its reasons. + + For each requirement multiple reasons together with their strength + can be stored in this object using the add_reason method. + A reason should be just a string with description (ie for tracking purposes). + Strength is a boolean flag that can be used to indicate whether missing the + requirement should be considered fatal. Strength of the requirement is + given by strength of all its reasons. + """ + def __init__(self, req_id, reasons=None): + self._id = req_id + self._reasons = reasons or [] + + @property + def id(self): + """Identifier of the requirement (eg a package name)""" + return self._id + + @property + def reasons(self): + """List of reasons for the requirement""" + return [reason for reason, strong in self._reasons] + + @property + def strong(self): + """Strength of the requirement (ie should it be considered fatal?)""" + return any(strong for reason, strong in self._reasons) + + def add_reason(self, reason, strong=False): + """Adds a reason to the requirement with optional strength of the reason""" + self._reasons.append(PayloadRequirementReason(reason, strong)) + + def __str__(self): + return "PayloadRequirement(id=%s, reasons=%s, strong=%s)" % (self.id, self.reasons, self.strong) + + def __repr__(self): + return 'PayloadRequirement(id=%s, reasons=%s)' % (self.id, self._reasons) + class PayloadRequirementsMissingApply(Exception): pass @@ -195,11 +235,9 @@ class PayloadRequirements(object): log.debug("no %s requirement added for %s", req_type.value, reason) reqs = self._reqs[req_type] for r_id in ids: - if r_id in reqs: - reqs[r_id].reasons.append(reason) - reqs[r_id].strong = reqs[r_id].strong or strong - else: - reqs[r_id] = PayloadRequirement(r_id, [reason], strong) + if r_id not in reqs: + reqs[r_id] = PayloadRequirement(r_id) + reqs[r_id].add_reason(reason, strong) self._apply_called_for_all_requirements = False log.debug("added %s requirement '%s' for %s, strong=%s", req_type.value, r_id, reason, strong)