Пример #1
0
    def sub(self, key, path=None, dct=None):
        if dct == None: dct = {} 

        if isinstance(key, dict):
            for k,v in key.items():
                subpath = k
                if path != None: subpath = path + "." + k

                self.sub(v, subpath, dct)
        else:
            PathHelper.setValue(dct, path, PathHelper.getValue(self._data, path))

        return dct
 def merge(self, path, value):
     return PathHelper.setValue(self._data, path, value, merge=True)
 def set(self, path, value):
     return PathHelper.setValue(self._data, path, value)
    def setData(self, index, value, role):
        if role == QtCore.Qt.EditRole:
            col = index.column()
            row = index.row()

            if self.rowCount(self._parent) <= row: return False

            key = self.getKey(row)

            oldVal = None 

            if self._columns[col] == '*':
                if (self._isList and key < len(self._data)) or key in self._data:
                    oldVal = self._data[key]
                else:
                    val = u""
            elif self._columns[col] == '__key__':
                oldVal = key
            else:
                if self._isList:
                    if key < len(self._data): oldVal = PathHelper.getValue(self._data[key], self._columns[col])
                else:
                    if key in self._data: oldVal = PathHelper.getValue(self._data[key], self._columns[col])

            vtype = self._types[col]

            if vtype not in ("autoedit", "dictedit", "listedit") and self._columns[col] == "*" and isinstance(oldVal, (dict, list, tuple)):
                return False

            if vtype == "autoedit":
                if isinstance(oldVal, (float, int)):
                    vtype = "doubleedit"
                elif isinstance(oldVal, bool):
                    vtype = "booledit"
                else:
                    if isinstance(value, (str, unicode)):
                        import re
                        if value == "true" or value == "false":
                            vtype = "booledit"
                        elif re.match(r"^\d+\.?\d*$", value):
                            vtype = "doubleedit"
                        elif re.match(r"^\[", value):
                            vtype = "stringedit"
                            if oldVal is None or not isinstance(oldVal, list): oldVal = []
                        elif re.match(r"^\{", value):
                            vtype = "stringedit"
                            if oldVal is None or not isinstance(oldVal, dict): oldVal = []
                        else:
                            vtype = "stringedit"
                    else:
                        vtype = "stringedit"
            elif vtype == "dictedit":
                vtype = "stringedit"
                if oldVal is None or not isinstance(oldVal, dict): oldVal = {}
            elif vtype == "listedit":
                vtype = "stringedit"
                if oldVal is None or not isinstance(oldVal, list): oldVal = []

            if vtype == "stringedit":
                val = unicode(value)

                if isinstance(oldVal, (list, dict)):
                    import json
                    try:
                        value = json.loads(u'{"value":' + val + u'}')
                        val = value['value']

                    except Exception as e:
                        if isinstance(oldVal, list): val = []
                        elif isinstance(oldVal, dict): val = {}

                if self._columns[col] == '*':
                    self._data[key] = val
                elif not self._isList and self._columns[col] == '__key__':
                    if val != key:
                        if key in self._data:
                            self._data[val] = self._data[key]
                            del self._data[key]

                        del self._rows[self._keys[row]]
                        self._keys[row] = val
                        self._rows[val] = row
                        self._peer.select(val)
                else:
                    if self._data[key] == None or not isinstance(self._data[key], dict): 
                        self._data[key] = {}

                    PathHelper.setValue(self._data[key], self._columns[col], val)

            elif vtype in ("doubleedit", "numberedit", "currencyedit"):
                val = 0
                if isinstance(value, (unicode, str)):
                    try:
                        if re.match(r"^\d+$", value):
                            val = int(value)
                        else:
                            val = float(value)
                    except: pass
                elif isinstance(value, (float, int, bool)):
                    val = value

                if self._columns[col] == '*':
                    self._data[key] = val
                elif not self._isList and self._columns[col] == '__key__':
                    self._data[val] = self._data[key]
                    del self._data[key]
                    del self._rows[self._keys[row]]
                    self._keys[row] = val
                    self._rows[val] = row
                    self._peer.select(val)
                else:
                    PathHelper.setValue(self._data[key], self._columns[col], val)
            elif vtype in ("booledit"):
                val = False
                if isinstance(value, (unicode, str)):
                    try:
                        if value == "true": val = True
                        elif value == "false": val = False
                        else: val = bool(value)
                    except: pass
                elif isinstance(value, (bool)):
                    val = value

                if self._columns[col] == '*':
                    self._data[key] = val
                elif not self._isList and self._columns[col] == '__key__':
                    self._data[val] = self._data[key]
                    del self._data[key]
                    del self._rows[self._keys[row]]
                    self._keys[row] = val
                    self._rows[val] = row
                    self._peer.select(val)
                else:
                    PathHelper.setValue(self._data[key], self._columns[col], val)
 
            if vtype in ("stringedit", "doubleedit", "numberedit", "currencyedit", "booledit"):
                self.dataChanged.emit(index, index)
                self._peer.fieldChanged(key, self._columns[col])
                return True

        return False
Пример #5
0
 def select(self, path, index):
     PathHelper.setValue(self.selection, path + "._selection", index)
Пример #6
0
 def overwrite(self, path, value):
     return PathHelper.setValue(self._data, path, value, self.selection, recurse=True)