示例#1
0
    def __fromiter__(self, items):
        extra = False
        if not isinstance(items, dict):
            items = OrderedDict(items)

        # Iterate over the template and construct the object from
        # the template description
        for (name, type, default, minmax, flags) in self.__template__:
            # If this node is an "xs:any" node, note it for later and skip
            if flags & ANY:
                extra = True
                continue
            # I don't remember why I'm doing this
            if name in self:
                continue
            # Get the value out of the items dictionary, or use the
            # default if it doesn't exist
            value = items.pop(name, default)

            # If there is not value, but this is a choice element, skip
            if value is None and (flags & CHOICE):
                continue

            if minmax == (0, 1):
                # Optional element
                # If we have a value, create the type
                if value is not None:
                    value = self._make_type(value, type)
            elif minmax == (1, 1):
                # Manditory element
                value = self._make_type(value, type)
            else:
                # List of elements
                if value is None:
                    value = []
                elif isinstance(value, (list, tuple)):
                    # A real list
                    value = [self._make_type(v, type) for v in value]
                elif isinstance(value, dict) and getattr(
                        value, 'listlike', False):
                    # A dict with integer keys (we hope)
                    value = [self._make_type(v, type) for v in value]
                else:
                    if self.__relax__:
                        value = []
                    else:
                        raise TypeError('Field must be a list', name)
            self[name] = value

        # If this type has an xs:any node, set all the remaining items
        # using the base class.
        if extra:
            DynamicObject.__fromiter__(self, items)
示例#2
0
文件: schema.py 项目: cfrantz/bubbles
    def __fromiter__(self, items):
        extra = False
        if not isinstance(items, dict):
            items = OrderedDict(items)

        # Iterate over the template and construct the object from
        # the template description
        for (name, type, default, minmax, flags) in self.__template__:
            # If this node is an "xs:any" node, note it for later and skip
            if flags & ANY:
                extra = True
                continue
            # I don't remember why I'm doing this
            if name in self:
                continue
            # Get the value out of the items dictionary, or use the
            # default if it doesn't exist
            value = items.pop(name, default)

            # If there is not value, but this is a choice element, skip
            if value is None and (flags & CHOICE):
                continue

            if minmax == (0,1):
                # Optional element
                # If we have a value, create the type
                if value is not None:
                    value = self._make_type(value, type)
            elif minmax == (1,1):
                # Manditory element
                value = self._make_type(value, type)
            else:
                # List of elements
                if value is None:
                    value = []
                elif isinstance(value, (list, tuple)):
                    # A real list
                    value = [self._make_type(v, type) for v in value]
                elif isinstance(value, dict) and getattr(value, 'listlike', False):
                    # A dict with integer keys (we hope)
                    value = [self._make_type(v, type) for v in value]
                else:
                    if self.__relax__:
                        value = []
                    else:
                        raise TypeError('Field must be a list', name)
            self[name] = value

        # If this type has an xs:any node, set all the remaining items
        # using the base class.
        if extra:
            DynamicObject.__fromiter__(self, items)