def __init__(self, template, parameters: ObservableList, values: ObservableDict, empty=None) -> None: self._value_property = Property(None) self._template = template self._values = values self._empty = empty self._parameters = parameters pattern = re.compile('\${([^}]+)\}') search_start = 0 script_template = '' required_parameters = set() if template: while search_start < len(template): match = pattern.search(template, search_start) if not match: script_template += template[search_start:] break param_start = match.start() if param_start > search_start: script_template += template[search_start:param_start] param_name = match.group(1) required_parameters.add(param_name) search_start = match.end() + 1 self.required_parameters = tuple(required_parameters) self._reload() if self.required_parameters: values.subscribe(self._value_changed) parameters.subscribe(self)
class _TemplateProperty: def __init__(self, template, parameters: ObservableList, values: ObservableDict, empty=None) -> None: self._value_property = Property(None) self._template = template self._values = values self._empty = empty self._parameters = parameters pattern = re.compile('\${([^}]+)\}') search_start = 0 script_template = '' required_parameters = set() if template: while search_start < len(template): match = pattern.search(template, search_start) if not match: script_template += template[search_start:] break param_start = match.start() if param_start > search_start: script_template += template[search_start:param_start] param_name = match.group(1) required_parameters.add(param_name) search_start = match.end() + 1 self.required_parameters = tuple(required_parameters) self._reload() if self.required_parameters: values.subscribe(self._value_changed) parameters.subscribe(self) def _value_changed(self, parameter, old, new): if parameter in self.required_parameters: self._reload() def on_add(self, parameter, index): if parameter.name in self.required_parameters: self._reload() def on_remove(self, parameter): if parameter.name in self.required_parameters: self._reload() def _reload(self): values_filled = True for param_name in self.required_parameters: value = self._values.get(param_name) if is_empty(value): values_filled = False break if self._template is None: self.value = None elif values_filled: self.value = fill_parameter_values(self._parameters, self._template, self._values) else: self.value = self._empty self._value_property.set(self.value) def subscribe(self, observer): self._value_property.subscribe(observer) def unsubscribe(self, observer): self._value_property.unsubscribe(observer) def get(self): return self._value_property.get()