def get_qrs_per_role_and_models(self, db, as_dict=True): """ :param as_dict: Boolean. If False, the result is returned as a list or rule keys """ qrs = dict() role_registry = RoleRegistry() role_qrs = role_registry.get_role_quality_rules(role_registry.get_active_role()) if role_qrs == ALL_QUALITY_RULES: role_qrs = self.__quality_rules if role_qrs: db_models = db.get_models() model_registry = LADMColModelRegistry() for qr in role_qrs: # First check if the role QR is registered if qr in self.__quality_rules: # Then check if the models required by the QR are in the DB req_models = self.__quality_rules[qr].models() num_models = len(req_models) all_models_found = True if num_models: # We don't check models if a QR has no required models (e.g., iliValidator) for req_model in req_models: model = model_registry.model(req_model) model_key = model.full_name() if model_key and model_key not in db_models: all_models_found = False self.logger.debug(__name__, "Model '{}' not found in the DB. QR '{}' cannot be listed.".format( model_key, qr )) break if all_models_found: qrs[qr] = self.__quality_rules[qr] return qrs if as_dict else list(qrs.keys())
class QualityRuleManager(QObject, metaclass=SingletonQObject): def __init__(self): self.logger = Logger() self.__quality_rules_data = QualityRuleConfig.get_quality_rules_config( ) self.__translated_strings = TranslatableConfigStrings( ).get_translatable_config_strings() self._quality_rule_groups = dict() self.__quality_rules = dict() self.role_registry = RoleRegistry() self._initialize_quality_rule_manager() def _initialize_quality_rule_manager(self): for group_k, group_v in self.__quality_rules_data.items(): self._quality_rule_groups[group_k] = group_v[QUALITY_GROUP_NAME] for rule_k, rule_v in group_v[QUALITY_RULES].items(): self.__quality_rules[rule_k] = QualityRule(rule_v) self.logger.info( __name__, "{} quality rules registered!".format(len(self.__quality_rules))) def get_quality_rule(self, rule_key): """ Returns the QualityRule object corresponding to a rule code. :param rule_key: rule key :return: QualityRule """ return self.__quality_rules.get(rule_key) def get_quality_rule_group_name(self, group_key): """ Returns a quality rule group name. :param group_key: Group key :return: Group name if the group key is found. Otherwise, None. """ return self._quality_rule_groups.get(group_key) def get_quality_rules_by_group(self, enum_group=None): """ Returns all rules in a given group. If no enum_group is given, it returns the whole set of rules classified by group. :param enum_group: EnumQualityRule.Point|Line|Polygon|Logic :return: dict of rules """ quality_rules_group = dict() role_key = self.role_registry.get_active_role() role_quality_rules = self.role_registry.get_role_quality_rules( role_key) if enum_group: quality_rules_group = { k_rule: v_rule for k_rule, v_rule in self.__quality_rules.items() if k_rule in enum_group and k_rule in role_quality_rules } else: quality_rules_group[EnumQualityRule.Point] = dict() quality_rules_group[EnumQualityRule.Line] = dict() quality_rules_group[EnumQualityRule.Polygon] = dict() quality_rules_group[EnumQualityRule.Logic] = dict() for k_quality_rule, v_quality_rule in self.__quality_rules.items(): if k_quality_rule in role_quality_rules: if k_quality_rule in EnumQualityRule.Point: quality_rules_group[EnumQualityRule.Point][ k_quality_rule] = v_quality_rule elif k_quality_rule in EnumQualityRule.Line: quality_rules_group[EnumQualityRule.Line][ k_quality_rule] = v_quality_rule elif k_quality_rule in EnumQualityRule.Polygon: quality_rules_group[EnumQualityRule.Polygon][ k_quality_rule] = v_quality_rule elif k_quality_rule in EnumQualityRule.Logic: quality_rules_group[EnumQualityRule.Logic][ k_quality_rule] = v_quality_rule self.logger.debug( __name__, "Quality Rules for role '{}': {}".format( role_key, ", ".join([str(rqr.value) for rqr in role_quality_rules]))) return quality_rules_group def get_error_message(self, error_code): return self.__translated_strings.get(error_code)