示例#1
0
文件: autoxml.py 项目: Tayyib/uludag
    def gen_list_tag(cls, tag, spec):
        """generate a list datatype. stores comps in tag/comp_tag"""
        name, tag_type, req, path = cls.parse_spec(tag, spec)

        pathcomps = path.split('/')
        comp_tag = pathcomps.pop()
        list_tagpath = util.makepath(pathcomps, sep='/', relative=True)

        if len(tag_type) != 1:
            raise Error(_('List type must contain only one element'))

        x = cls.gen_tag(comp_tag, [tag_type[0], mandatory])
        (init_item, decode_item, encode_item, errors_item, format_item) = x

        def init():
            return []

        def decode(node, errs, where):
            l = []
            nodes = xmlext.getAllNodes(node, path)
            #print node, tag + '/' + comp_tag, nodes
            if len(nodes)==0 and req==mandatory:
                errs.append(where + ': ' + _('Mandatory list "%s" under "%s" node is empty.') % (path, node.name()))
            ix = 1
            for node in nodes:
                dummy = xmlext.newNode(node, "Dummy")
                xmlext.addNode(dummy, '', node)
                l.append(decode_item(dummy, errs, where + unicode("[%s]" % ix)))
                #l.append(decode_item(node, errs, where + unicode("[%s]" % ix)))
                ix += 1
            return l

        def encode(node, l, errs):
            if l and len(l) > 0:
                for item in l:
                    if list_tagpath:
                        listnode = xmlext.addNode(node, list_tagpath, branch = False)
                    else:
                        listnode = node
                    encode_item(listnode, item, errs)
                    #encode_item(node, item, errs)
            else:
                if req is mandatory:
                    errs.append(_('Mandatory list "%s" under "%s" node is empty.') % (path, node.name()))

        def errors(l, where):
            errs = []
            ix = 1
            for node in l:
                errs.extend(errors_item(node, where + '[%s]' % ix))
                ix += 1
            return errs

        def format(l, f, errs):
            l.sort()
            for node in l:
                format_item(node, f, errs)
                f.add_literal_data(' ')

        return (init, decode, encode, errors, format)
示例#2
0
    def gen_list_tag(cls, tag, spec):
        """generate a list datatype. stores comps in tag/comp_tag"""
        name, tag_type, req, path = cls.parse_spec(tag, spec)

        pathcomps = path.split("/")
        comp_tag = pathcomps.pop()
        list_tagpath = util.makepath(pathcomps, sep="/", relative=True)

        if len(tag_type) != 1:
            raise Error(_("List type must contain only one element"))

        x = cls.gen_tag(comp_tag, [tag_type[0], mandatory])
        (init_item, decode_item, encode_item, errors_item, format_item) = x

        def init():
            return []

        def decode(node, errs, where):
            l = []
            nodes = getAllNodes(node, path)
            # print node, tag + '/' + comp_tag, nodes
            if len(nodes) == 0 and req == mandatory:
                errs.append(where + ": " + _("Mandatory list empty"))
            ix = 1
            for node in nodes:
                dummy = newNode(node, "Dummy")
                dummy.appendChild(node)
                l.append(decode_item(dummy, errs, where + unicode("[%s]" % ix)))
                # l.append(decode_item(node, errs, where + unicode("[%s]" % ix)))
                ix += 1
            return l

        def encode(node, l, errs):
            dom = node.ownerDocument
            if l and len(l) > 0:
                for item in l:
                    if list_tagpath:
                        listnode = addNode(node, list_tagpath, branch=False)
                    else:
                        listnode = node
                    encode_item(listnode, item, errs)
                    # encode_item(node, item, errs)
            else:
                if req is mandatory:
                    errs.append(_("Mandatory list empty"))

        def errors(l, where):
            errs = []
            ix = 1
            for node in l:
                errs.extend(errors_item(node, where + "[%s]" % ix))
                ix += 1
            return errs

        def format(l, f, errs):
            # TODO: indent here
            ix = 1
            length = len(l)
            for node in l:
                f.add_flowing_data(str(ix) + ": ")
                format_item(node, f, errs)
                if ix != length:
                    f.add_flowing_data(", ")
                ix += 1

        return (init, decode, encode, errors, format)
示例#3
0
    def gen_list_tag(cls, tag, spec):
        """generate a list datatype. stores comps in tag/comp_tag"""
        name, tag_type, req, path = cls.parse_spec(tag, spec)

        pathcomps = path.split("/")
        comp_tag = pathcomps.pop()
        list_tagpath = util.makepath(pathcomps, sep="/", relative=True)

        if len(tag_type) != 1:
            raise Error(_("List type must contain only one element"))

        x = cls.gen_tag(comp_tag, [tag_type[0], mandatory])
        (init_item, decode_item, encode_item, errors_item, format_item) = x

        def init():
            return []

        def decode(node, errs, where, use_ondemand):

            if use_ondemand:
                l = ondemand.OnDemandList()
            else:
                l = []

            nodes = xmlext.getAllNodes(node, path)
            # print node, tag + '/' + comp_tag, nodes
            if len(nodes) == 0 and req == mandatory:
                errs.append(where + ": " + _('Mandatory list "%s" under "%s" node is empty.') % (path, node.name()))
            ix = 1
            for node in nodes:
                dummy = xmlext.newNode(node, "Dummy")
                xmlext.addNode(dummy, "", node)
                if use_ondemand:
                    l.append(ondemand.OnDemandNode(decode_item, dummy, where + unicode("[%s]" % ix)))
                else:
                    l.append(decode_item(dummy, errs, where + unicode("[%s]" % ix), use_ondemand))
                # l.append(decode_item(node, errs, where + unicode("[%s]" % ix)))
                ix += 1
            return l

        def encode(node, l, errs):
            if l:
                for item in l:
                    if list_tagpath:
                        listnode = xmlext.addNode(node, list_tagpath, branch=False)
                    else:
                        listnode = node
                    encode_item(listnode, item, errs)
                    # encode_item(node, item, errs)
            else:
                if req is mandatory:
                    errs.append(_('Mandatory list "%s" under "%s" node is empty.') % (path, node.name()))

        def errors(l, where):
            errs = []
            ix = 1
            if isinstance(l, ondemand.OnDemandList):
                # print "pass error check %s[%s]" % (where, ix) # debuginfo
                l.errors_item_function = errors_item
            else:
                for node in l:
                    errs.extend(errors_item(node, where + "[%s]" % ix))
                    ix += 1

            return errs

        def format(l, f, errs):
            l.sort()
            for node in l:
                format_item(node, f, errs)
                f.add_literal_data(" ")

        return (init, decode, encode, errors, format)
示例#4
0
    def gen_list_tag(cls, tag, spec):
        """generate a list datatype. stores comps in tag/comp_tag"""
        name, tag_type, req, path = cls.parse_spec(tag, spec)

        pathcomps = path.split('/')
        comp_tag = pathcomps.pop()
        list_tagpath = util.makepath(pathcomps, sep='/', relative=True)

        if len(tag_type) != 1:
            raise Error(_('List type must contain only one element'))

        x = cls.gen_tag(comp_tag, [tag_type[0], mandatory])
        (init_item, decode_item, encode_item, errors_item, format_item) = x

        def init():
            return []

        def decode(node, errs, where):
            l = []
            nodes = getAllNodes(node, path)
            #print node, tag + '/' + comp_tag, nodes
            if len(nodes) == 0 and req == mandatory:
                errs.append(where + ': ' + _('Mandatory list empty'))
            ix = 1
            for node in nodes:
                dummy = newNode(node, "Dummy")
                addNode(dummy, '', node)
                l.append(
                    decode_item(dummy, errs,
                                where + unicode("[%s]" % ix, 'utf8')))
                #l.append(decode_item(node, errs, where + unicode("[%s]" % ix)))
                ix += 1
            return l

        def encode(node, l, errs):
            if l and len(l) > 0:
                for item in l:
                    if list_tagpath:
                        listnode = addNode(node, list_tagpath, branch=False)
                    else:
                        listnode = node
                    encode_item(listnode, item, errs)
                    #encode_item(node, item, errs)
            else:
                if req is mandatory:
                    errs.append(_('Mandatory list empty'))

        def errors(l, where):
            errs = []
            ix = 1
            for node in l:
                errs.extend(errors_item(node, where + '[%s]' % ix))
                ix += 1
            return errs

        def format(l, f, errs):
            # TODO: indent here
            ix = 1
            length = len(l)
            for node in l:
                f.add_flowing_data(str(ix) + ': ')
                format_item(node, f, errs)
                if ix != length:
                    f.add_flowing_data(', ')
                ix += 1

        return (init, decode, encode, errors, format)
示例#5
0
    def gen_list_tag(cls, tag, spec):
        """generate a list datatype. stores comps in tag/comp_tag"""
        name, tag_type, req, path = cls.parse_spec(tag, spec)

        pathcomps = path.split('/')
        comp_tag = pathcomps.pop()
        list_tagpath = util.makepath(pathcomps, sep='/', relative=True)

        if len(tag_type) != 1:
            raise Error(_('List type must contain only one element'))

        x = cls.gen_tag(comp_tag, [tag_type[0], mandatory])
        (init_item, decode_item, encode_item, errors_item, format_item) = x

        def init():
            return []

        def decode(node, errs, where, use_ondemand):

            if use_ondemand:
                l = ondemand.OnDemandList()
            else:
                l = []

            nodes = xmlext.getAllNodes(node, path)
            #print node, tag + '/' + comp_tag, nodes
            if len(nodes) == 0 and req == mandatory:
                errs.append(
                    where + ': ' +
                    _('Mandatory list "%s" under "%s" node is empty.') %
                    (path, node.name()))
            ix = 1
            for node in nodes:
                dummy = xmlext.newNode(node, "Dummy")
                xmlext.addNode(dummy, '', node)
                if use_ondemand:
                    l.append(
                        ondemand.OnDemandNode(decode_item, dummy,
                                              where + unicode("[%s]" % ix)))
                else:
                    l.append(
                        decode_item(dummy, errs, where + unicode("[%s]" % ix),
                                    use_ondemand))
                #l.append(decode_item(node, errs, where + unicode("[%s]" % ix)))
                ix += 1
            return l

        def encode(node, l, errs):
            if l:
                for item in l:
                    if list_tagpath:
                        listnode = xmlext.addNode(node,
                                                  list_tagpath,
                                                  branch=False)
                    else:
                        listnode = node
                    encode_item(listnode, item, errs)
                    #encode_item(node, item, errs)
            else:
                if req is mandatory:
                    errs.append(
                        _('Mandatory list "%s" under "%s" node is empty.') %
                        (path, node.name()))

        def errors(l, where):
            errs = []
            ix = 1
            if isinstance(l, ondemand.OnDemandList):
                # print "pass error check %s[%s]" % (where, ix) # debuginfo
                l.errors_item_function = errors_item
            else:
                for node in l:
                    errs.extend(errors_item(node, where + '[%s]' % ix))
                    ix += 1

            return errs

        def format(l, f, errs):
            l.sort()
            for node in l:
                format_item(node, f, errs)
                f.add_literal_data(' ')

        return (init, decode, encode, errors, format)