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 deserialize(self, data: dict): self._source = serializer.simple_deserialize(data['source']) self.__origin = FieldValue.create( serializer.simple_deserialize(data['origin'])) self.__matched = FieldValue.create( serializer.simple_deserialize(data['matched'])) self.__suggested = FieldValue.create( serializer.simple_deserialize(data['suggested'])) if 'exclude' in data: self._exclude = FieldValue.create( serializer.simple_deserialize(data['exclude']))
def match(self, form): LOG.info("Match field %s with value %s" % (self._source, self.__origin)) if self._matcher: if not FieldValue.is_empty(self.__origin): matched_value = self._matcher(self.__origin, form) self._validate_match(matched_value) self.__matched = FieldValue.create(matched_value) LOG.info("Matched value for field %s is %s(%s)" % (self._source, self.__matched.__class__.__name__, self.__matched))
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 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 match(self, form): """ The logic is the following if there is matcher on the field and its match field then this field is main if there is not matched fields take first with data and use it as main field if there is not fields with data so then use just first field in the group """ first_with_origin = None for field in self.__fields(): if not FieldValue.is_empty(field.origin): if not first_with_origin: first_with_origin = field field.match(form) if not FieldValue.is_empty(field.matched): self.__selected_field = field return if first_with_origin: self.__selected_field = first_with_origin elif len(self.__fields()) > 0: self.__selected_field = self.__fields()[0]
def suggest(self) -> (str, DialogField): LOG.info("==== Start suggesting form %s ====" % self.__class__.__name__) for field_name, field in self._fields.items(): is_optional_field = isinstance(field, OptionalField) if FieldValue.is_empty(field.origin) and is_optional_field: LOG.info("Skipping empty optional field {}".format(field_name)) # skipping optional field suggest continue suggested_result = field.suggest(self) if FieldValue.is_empty(suggested_result)\ or FieldValue.is_suggested(suggested_result): LOG.info("==== End suggesting form %s ====" % self.__class__.__name__) return field_name, field LOG.info("==== End suggesting form %s ====" % self.__class__.__name__) return None
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 _to_suggest_fieldvalue(self, value): return FieldValue.create_suggested(value)
def _to_suggest_fieldvalue(self, value): if FieldValue.is_empty(self.origin): return super(ListField, self)._to_suggest_fieldvalue(value) return FieldValue.create(value)
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()
def _fill_origin(self, raw_payload): self.__origin = FieldValue.create(raw_payload.get(self._source, None))