示例#1
0
def writeHierarchicalXml(struct, options):
    if not struct.root.attributes:
        options.skip_root = True
    with contextlib.closing(xml2csv.getOutStream(options.output)) as outputf:
        if options.source.isdigit():
            inputf = xml2csv.getSocketStream(int(options.source))
        else:
            inputf = open(options.source)
        lastRow = OrderedDict()
        tagStack = [struct.root.name]
        if options.skip_root:
            if (PY3):
                outputf.write(str.encode('<%s' % struct.root.name))
            else:
                outputf.write('<%s' % struct.root.name)
        fields = None
        enums = {}
        first = True
        for raw in csv.reader(inputf, delimiter=options.delimiter):
            if not fields:
                fields = raw
                for f in fields:
                    if not '_' in f:
                        continue
                    enum = struct.getEnumerationByAttr(*f.split('_', 1))
                    if enum:
                        enums[f] = enum
            else:
                row = OrderedDict()
                for field, entry in zip(fields, raw):
                    if field in enums and entry.isdigit():
                        entry = enums[field][int(entry)]
                    row[field] = entry
                if first and not options.skip_root:
                    checkAttributes(outputf, lastRow, row, struct.root,
                                    tagStack, 0)
                    first = False
                checkChanges(outputf, lastRow, row, struct.root, tagStack, 1)
                lastRow = row
        outputf.write(str.encode("/>\n"))
        for idx in range(len(tagStack) - 2, -1, -1):
            if (PY3):
                outputf.write(
                    str.encode("%s</%s>\n" % (idx * '    ', tagStack[idx])))
            else:
                outputf.write("%s</%s>\n" % (idx * '    ', tagStack[idx]))
示例#2
0
文件: csv2xml.py 项目: behrisch/sumo
def writeHierarchicalXml(struct, options):
    if not struct.root.attributes:
        options.skip_root = True
    with contextlib.closing(xml2csv.getOutStream(options.output)) as outputf:
        if options.source.isdigit():
            inputf = xml2csv.getSocketStream(int(options.source))
        else:
            inputf = open(options.source)
        lastRow = OrderedDict()
        tagStack = [struct.root.name]
        if options.skip_root:
            if(PY3):
                outputf.write(str.encode('<%s' % struct.root.name))
            else:
                outputf.write('<%s' % struct.root.name)
        fields = None
        enums = {}
        first = True
        for raw in csv.reader(inputf, delimiter=options.delimiter):
            if not fields:
                fields = raw
                for f in fields:
                    if '_' not in f:
                        continue
                    enum = struct.getEnumerationByAttr(*f.split('_', 1))
                    if enum:
                        enums[f] = enum
            else:
                row = OrderedDict()
                for field, entry in zip(fields, raw):
                    if field in enums and entry.isdigit():
                        entry = enums[field][int(entry)]
                    row[field] = entry
                if first and not options.skip_root:
                    checkAttributes(
                        outputf, lastRow, row, struct.root, tagStack, 0)
                    first = False
                checkChanges(outputf, lastRow, row, struct.root, tagStack, 1)
                lastRow = row
        outputf.write(str.encode("/>\n"))
        for idx in range(len(tagStack) - 2, -1, -1):
            if(PY3):
                outputf.write(
                    str.encode("%s</%s>\n" % (idx * '    ', tagStack[idx])))
            else:
                outputf.write("%s</%s>\n" % (idx * '    ', tagStack[idx]))
示例#3
0
def writeXml(root, module, options):
    with contextlib.closing(xml2csv.getOutStream(options.output)) as outputf:
        outputf.write('<?xml version="1.0" encoding="UTF-8"?>\n\n<%s' % root)
        if options.source.isdigit():
            inp = xml2csv.getSocketStream(int(options.source))
        else:
            inp = open(options.source, 'rb')
        with contextlib.closing(inp) as inputf:
            first = True
            while True:
                length = struct.unpack('>L', read_n(inputf, 4))[0]
                if length == 0:
                    break
                obj = vars(module)[root.capitalize()]()
                obj.ParseFromString(read_n(inputf, length))
                for attr, value in obj.ListFields():
                    if attr.type == google.protobuf.descriptor.FieldDescriptor.TYPE_MESSAGE:
                        if attr.label == google.protobuf.descriptor.FieldDescriptor.LABEL_REPEATED:
                            for item in value:
                                msg2xml(attr, item, outputf)
                    elif first:
                        outputf.write(' %s="%s"' % (attr.name, value))
                first = False
        outputf.write(">\n</%s>\n" % root)
示例#4
0
def writeXml(root, module, options):
    with contextlib.closing(xml2csv.getOutStream(options.output)) as outputf:
        outputf.write('<?xml version="1.0" encoding="UTF-8"?>\n\n<%s' % root)
        if options.source.isdigit():
            inp = xml2csv.getSocketStream(int(options.source))
        else:
            inp = open(options.source, 'rb')
        with contextlib.closing(inp) as inputf:
            first = True
            while True:
                length = struct.unpack('>L', read_n(inputf, 4))[0]
                if length == 0:
                    break
                obj = vars(module)[root.capitalize()]()
                obj.ParseFromString(read_n(inputf, length))
                for attr, value in obj.ListFields():
                    if attr.type == google.protobuf.descriptor.FieldDescriptor.TYPE_MESSAGE:
                        if attr.label == google.protobuf.descriptor.FieldDescriptor.LABEL_REPEATED:
                            for item in value:
                                msg2xml(attr, item, outputf)
                    elif first:
                        outputf.write(' %s="%s"' % (attr.name, value))
                first = False
        outputf.write(">\n</%s>\n" % root)
示例#5
0
 def __init__(self, module, attrFinder, output):
     self.module = module
     self.attrFinder = attrFinder
     self.out = xml2csv.getOutStream(output)
     self.msgStack = []
     self.emptyRootMsg = None
 def __init__(self, module, attrFinder, output):
     self.module = module
     self.attrFinder = attrFinder
     self.out = xml2csv.getOutStream(output)
     self.msgStack = []
     self.emptyRootMsg = None
示例#7
0
 def __init__(self, module, tagAttrs, output):
     self.module = module
     self.tagAttrs = tagAttrs
     self.out = xml2csv.getOutStream(output)
     self.msgStack = []
     self.emptyRootMsg = None