def schema(self): ret = { "name": self.name, "type": self.__class__.__name__, "verbose_name": self.verbose_name, "default": self.default, "readonly": self.readonly, "is_key": self.is_key, "datatype": self.datatype.__name__ } gripe.call_if_exists(self, "schema_customizer", None, ret) return ret
def set_instance(self, instance=None): i = self._instance self._instance = instance() if isinstance(instance, (grumble.Model, grumble.Key)) else None if self._instance is not None and (i is None or self._instance != i): self.form.apply(self._instance) gripe.call_if_exists(self, "assign", self._instance) self.instanceAssigned.emit(self._instance.key()) elif instance is None: self.form.clear() self.newInstance.emit() else: self.form.apply_labels(instance)
def to_key(k): ret = None if k is not None: ret = gripe.call_if_exists(k, "key", None) if ret is None: ret = Key(urllib.parse.unquote_plus(str(k))) return ret
def get_values(self, instance): instances = set() if instance is None: instance = gripe.call_if_exists( self.parent(), "init_instance", lambda: self.init_instance() if self.init_instance else self.kind()() if self.kind() else None) assert instance, "No instance created!" for (prop, bridge) in filter(lambda itm: itm[1].is_modified(), self._properties.items()): path = prop.split(".") i = instance for n in path[:-1]: i = getattr(i, n) if n != "^" else i.parent() v = bridge.get_value(i) p = path[-1] if p == "^": i.set_parent(v) else: setattr(i, path[-1], v) instances.add(i) bridge.set_value(i) for s in self.sublayouts: instances |= s.get_values(instance) return instance, instances
def __init__(self, *args): assert args, "Cannot construct void Key" if self.id is not None: return if len(args) == 1: value = args[0] assert value is not None, "Cannot initialize Key from None" if isinstance(value, str): self._assign_id(value) elif isinstance(value, dict): if "id" in value: self.__init__(value["id"]) elif "key" in value: self.__init__(value["key"]) elif "kind" in value and "name" in value and "scope" in value: self.__init__(value["kind"], value.get("scope"), value["name"]) elif "kind" in value and "name" in value: self.__init__(value["kind"], value["name"]) else: assert 0, "Cannot create Key object from dict %s" % value elif gripe.hascallable(value, "key"): k = value.key() self._kind = k.kind() self.id = k.id self.name = k.name self._scope = k._scope else: assert 0, "Cannot initialize Key from %s, type %s" % ( value, type(value)) else: kind = args[0] if not isinstance(kind, str): assert isinstance(kind, str) or gripe.hascallable(kind, "kind"), \ "First argument of Key(kind, name) must be string or datamodel class, not %s" % type(kind) kind = gripe.call_if_exists(kind, "kind", kind) assert kind, "Kind must not be empty" name = args[-1] if not isinstance(name, str): pass assert isinstance(name, str), \ "Last argument of Key(%s, ..., name) must be string, not %s" % (kind, type(name)) assert name, "Key name argument must not be empty" name = urllib.parse.quote_plus(str(args[-1])) if len(args) == 3 and args[1] is not None: p = args[1] assert isinstance(p, str) or gripe.hascallable(p, "key"), \ "Scope must be Key, Model or string" p = to_key(p) else: p = None self._assign(p, kind, name) if not (hasattr(self, "id") and self.id): self.id = base64.urlsafe_b64encode(bytearray(str(self), 'UTF-8'))
def customize(self, widget): regexp = self.config.get("regexp") if regexp: validator = QRegExpValidator(QRegExp(regexp)) if "casesensitive" in self.config: cs = bool(self.config.get("casesensitive")) validator.setCaseSensitivity( Qt.CaseSensitive if cs else Qt.CaseInsensitive) widget.setValidator(validator) maxlength = int(self.config.get("maxlength", 0)) if maxlength > 0: widget.setMaxLength(maxlength) fm = widget.fontMetrics() widget.setMaximumWidth(maxlength * fm.maxWidth() + 11) validator = gripe.call_if_exists(self, "create_validator", None) if validator: widget.setValidator(validator) return widget
def after_store_(self, value): return gripe.call_if_exists(self, "after_store", None, value)
def on_store_(self, value): return gripe.call_if_exists(self, "on_store", None, value)
def initial_value_(self): return gripe.call_if_exists(self, "initial_value", self.default, self.default)
def on_insert_(self, instance): value = self.__get__(instance) if not value and self.default: value = self.__set__(instance, self.default) return gripe.call_if_exists(self, "on_insert", value, instance, value)
def refresh(self, *args): self.model().beginResetModel() self.resetQuery() self.model().flush() self.model().endResetModel() gripe.call_if_exists(self, "on_refresh", None, *args)
def tabChanged(self, ix): gripe.call_if_exists(self.tabs.currentWidget(), "selected", None)