Exemplo n.º 1
0
    def _set_flat(self, pairs, sep):
        del self[:]
        prune = self.prune_empty
        child_name = self.member_schema.name

        # TODO: some complexity snuck in below with the thought of supporting
        # arrays of containers.  they're *not* working yet.
        assert not issubclass(
            self.member_schema, Container
        ), "Flattened Arrays are only supported for scalar child types."

        if not self.name:
            child_prefix = child_name or u""
            for key, value in pairs:
                if prune and value == u"" and key == child_prefix:
                    continue
                if key == u"":
                    key = None
                if child_name and key != child_name:
                    continue
                member = self.member_schema.from_flat([(key, value)])
                self.append(member)
        else:
            regex = re.compile(ur"^(%s(?:%s|$))" % (re_uescape(self.name), re_uescape(sep)), re.UNICODE)
            for key, value in pairs:
                m = regex.match(key)
                if not m:
                    continue
                remainder = key[m.end() :] or None
                if child_name and not remainder:
                    continue
                elif prune and value == u"" and remainder == child_name:
                    continue
                member = self.member_schema.from_flat([(remainder, value)])
                self.append(member)
Exemplo n.º 2
0
    def _set_flat(self, pairs, sep):
        del self[:]
        prune = self.prune_empty
        child_name = self.member_schema.name

        # TODO: some complexity snuck in below with the thought of supporting
        # arrays of containers.  they're *not* working yet.
        assert not issubclass(self.member_schema, Container), \
               "Flattened Arrays are only supported for scalar child types."

        if not self.name:
            child_prefix = child_name or u''
            for key, value in pairs:
                if prune and value == u'' and key == child_prefix:
                    continue
                if key == u'':
                    key = None
                if child_name and key != child_name:
                    continue
                member = self.member_schema.from_flat([(key, value)])
                self.append(member)
        else:
            regex = re.compile(ur'^(%s(?:%s|$))' % (
                re_uescape(self.name), re_uescape(sep)), re.UNICODE)
            for key, value in pairs:
                m = regex.match(key)
                if not m:
                    continue
                remainder = key[m.end():] or None
                if child_name and not remainder:
                    continue
                elif prune and value == u'' and remainder == child_name:
                    continue
                member = self.member_schema.from_flat([(remainder, value)])
                self.append(member)
Exemplo n.º 3
0
    def _set_flat(self, pairs, sep):
        del self[:]

        if not pairs:
            return

        if self.name:
            regex = re.compile(
                u'^%s(\\d+)(?:%s|$)' %
                (re_uescape(self.name + sep), re_uescape(sep)), re.UNICODE)
        else:
            regex = re.compile(u'^(\\d+)(?:%s|$)' % (re_uescape(sep)),
                               re.UNICODE)

        indexes = defaultdict(list)
        prune = self.prune_empty

        for key, value in pairs:
            if value == u'' and prune:
                continue
            m = regex.match(key)
            if not m:
                continue
            try:
                index = int(m.group(1))
            except TypeError:
                # Ignore keys with outrageously large indexes- they
                # aren't valid data for us.
                pass
            else:
                child_key = key[len(m.group(0)):] or None
                indexes[index].append((child_key, value))

        if not indexes:
            return

        # lossy: missing (or empty-valued) indexes are omitted.
        #        the python indexes may not match the flat indexes
        if prune:
            for offset, index in enumerate(sorted(indexes)):
                if offset == self.maximum_set_flat_members:
                    break
                slot = self._new_slot()
                list.append(self, slot)
                slot.element.set_flat(indexes[index], sep)
        # lossless: elements are built up to the highest seen index or a
        #           schema-configured maximum. flat + python indexes match.
        else:
            max_index = min(max(indexes) + 1, self.maximum_set_flat_members)
            for index in xrange(0, max_index):
                slot = self._new_slot()
                list.append(self, slot)
                flat = indexes.get(index, None)
                if flat:
                    slot.element.set_flat(flat, sep)
Exemplo n.º 4
0
    def _set_flat(self, pairs, sep):
        del self[:]

        if not pairs:
            return

        if self.name:
            regex = re.compile(u'^%s(\\d+)(?:%s|$)' % (
                re_uescape(self.name + sep), re_uescape(sep)), re.UNICODE)
        else:
            regex = re.compile(u'^(\\d+)(?:%s|$)' % (
                re_uescape(sep)), re.UNICODE)

        indexes = defaultdict(list)
        prune = self.prune_empty

        for key, value in pairs:
            if value == u'' and prune:
                continue
            m = regex.match(key)
            if not m:
                continue
            try:
                index = int(m.group(1))
            except TypeError:
                # Ignore keys with outrageously large indexes- they
                # aren't valid data for us.
                pass
            else:
                child_key = key[len(m.group(0)):] or None
                indexes[index].append((child_key, value))

        if not indexes:
            return

        # lossy: missing (or empty-valued) indexes are omitted.
        #        the python indexes may not match the flat indexes
        if prune:
            for offset, index in enumerate(sorted(indexes)):
                if offset == self.maximum_set_flat_members:
                    break
                slot = self._new_slot()
                list.append(self, slot)
                slot.element.set_flat(indexes[index], sep)
        # lossless: elements are built up to the highest seen index or a
        #           schema-configured maximum. flat + python indexes match.
        else:
            max_index = min(max(indexes) + 1, self.maximum_set_flat_members)
            for index in xrange(0, max_index):
                slot = self._new_slot()
                list.append(self, slot)
                flat = indexes.get(index, None)
                if flat:
                    slot.element.set_flat(flat, sep)