def __init__(self, source: Any = None, matcher=None, suggesters: List[Suggester] = None, choosers: List[Chooser] = None, exclude: DialogFieldValue = None): self._source = source self._matcher = matcher self._suggesters = suggesters if suggesters else [] self._choosers = choosers if choosers else [] self.__origin = FieldValue.empty() self.__matched = FieldValue.empty() self.__suggested = FieldValue.empty() self._exclude = exclude # exclude is specially may be None
def value(self): if not FieldValue.is_empty(self.__suggested): return self.__suggested elif not FieldValue.is_empty(self.__matched): return self.__matched else: return FieldValue.empty()
def get_exclude(self, field_name) -> FieldValue: """ For internal needs exclude could be None, but for external needs everything should be FieldValue """ exclude = self._fields[field_name].exclude if not exclude: return FieldValue.empty() return exclude
def suggest(self, form) -> DialogFieldValue: LOG.info("Suggest field %s" % self._source) for suggester in self._suggesters: suggester_name = suggester.__class__.__name__ suggester_result = suggester(self, form) LOG.info("%s -> Suggested value for field %s is %s" % (suggester_name, self._source, suggester_result)) if suggester_result: suggester_result_fieldvalue = self._to_suggest_fieldvalue( suggester_result) if FieldValue.is_suggested(suggester_result_fieldvalue): choosers_result = self._choose(suggester_result) result = suggester_result if not choosers_result else choosers_result suggester_result_fieldvalue = self._to_suggest_fieldvalue( result) self.__suggested = suggester_result_fieldvalue LOG.info("Suggested FieldValue for field %s is %s" % (self._source, self.__suggested.__class__.__name__)) return self.__suggested LOG.info("Suggested value for field %s is empty" % self._source) return FieldValue.empty()
def suggested(self): if self.__selected_field: return self.__selected_field.suggested return FieldValue.empty()
def matched(self): if self.__selected_field: return self.__selected_field.matched return FieldValue.empty()
def origin(self): if self.__selected_field: return self.__selected_field.origin return FieldValue.empty()
def value(self): if self.__selected_field: return self.__selected_field.value return FieldValue.empty()
def suggest(self, form) -> DialogFieldValue: if self.__selected_field: return self.__selected_field.suggest(form) return FieldValue.empty()