1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#!/usr/bin/python
import os.path, os, shutil, time, string
import sys, getopt
from xml.dom.minidom import *
from xml import xpath
pg_version="1.9"
pg_author="Frederic Aoustin"
pg_contact="fraoustin@yahoo.fr"
pg_name="flat2xml"
# todo
# not fix
# gerer log et trace
def usage():
print """Usage:
%s
%s -h --help : help
%s -v : version
%s -i FlatFile -d XmlDescriptor [-o XmlOut] : convert file in xml
""" % (pg_name, pg_name, pg_name, pg_name)
version()
def version():
print "Version:\n%s version %s by %s(%s)" % (pg_name, pg_version, pg_author, pg_contact)
def saveFile(path, content):
endfile = open(path,'w')
endfile.write(content)
endfile.close()
def readFile(inFile):
f=open(inFile,'r')
contents=f.readlines()
f.close()
return contents
def init(xmldoc):
if "num" in xmldoc.documentElement.attributes.keys():
fix = True
num = xmldoc.documentElement.attributes["num"].value
else:
fix = False
num="-1"
if "name" in xmldoc.documentElement.attributes.keys():
name = xmldoc.documentElement.attributes["name"].value
else:
name="root"
return fix, num, name
def getAttr(node, attr, default="unknown"):
if attr in node.attributes.keys():
return node.attributes[attr].value
return default
def createElt(xmldoc, node, desc, data, ids):
for n in desc.childNodes:
if n.nodeType == Node.ELEMENT_NODE and n.nodeName == "element":
xmlelt = xmldoc.createElement(n.lastChild.data)
mi=getAttr(n,"min","0")
mx=getAttr(n,"max","")
if mi != "" and mx != "":
xmlelt.appendChild(xmldoc.createTextNode(data[int(mi):int(mx)]))
if mi != "" and mx == "":
xmlelt.appendChild(xmldoc.createTextNode(data[int(mi):]))
if mi == "" and mx != "":
xmlelt.appendChild(xmldoc.createTextNode(data[:int(mx)]))
if mi == "" and mx == "":
xmlelt.appendChild(xmldoc.createTextNode(data))
node.appendChild(xmlelt)
createElt(xmldoc, xmlelt, n , data, ids)
if n.nodeType == Node.ELEMENT_NODE and n.nodeName == "row":
xmlelt = xmldoc.createElement(getAttr(n,"name","row"))
node.appendChild(xmlelt)
createElt(xmldoc, xmlelt, n , data, ids)
if n.nodeType == Node.ELEMENT_NODE and n.nodeName == "attr":
if "min" in n.attributes.keys():
mi=getAttr(n,"min","0")
mx=getAttr(n,"max","")
if mi != "" and mx != "":
d=data[int(mi):int(mx)]
if mi != "" and mx == "":
d=data[int(mi):]
if mi == "" and mx != "":
d = data[:int(mx)]
if mi == "" and mx == "":
d = data
node.setAttribute(n.lastChild.data,d)
if "fix" in n.attributes.keys():
node.setAttribute(n.lastChild.data,getAttr(n,"fix","0"))
if "param" in n.attributes.keys():
if getAttr(n,"param","0") == "id":
node.setAttribute(n.lastChild.data,ids)
def convert(flat, xml, out=None):
try:
data = readFile(flat)
xmldoc = parse(xml)
root=xmldoc.documentElement
fix, num, name = init(xmldoc)
xmlout = Document()
xmltag = xmlout.createElement(name)
xmlout.appendChild(xmltag)
for i in data:
i =i[:-1]
if fix:
row = xpath.Evaluate("/root/row[@id='%s']" % i[0:int(num)] , root)
else:
rows = xpath.Evaluate("/root/row" , root)
row=[]
for k in rows:
if getAttr(k,"id","") == i[0:len(getAttr(k,"id",""))]:
row.append(k)
for j in row:
xmlrow = xmlout.createElement(getAttr(j, "name", "row"))
xmltag.appendChild(xmlrow)
if fix:
l = int(num)
else:
l = len(getAttr(j,"id",""))
createElt(xmlout, xmlrow, j , i[l:], i[0:l])
if out == None:
print xmlout.toxml()
else:
saveFile(out, xmlout.toxml())
except Exception, e:
print "Error ", e
if __name__ == "__main__":
#convert(flat = "/home/fraoustin/MyProject/XML/flat_to_xml/flat_to_xml_fix.txt",xml = "/home/fraoustin/MyProject/XML/flat_to_xml/flat_to_xml_fix.xml",out = "/home/fraoustin/MyProject/XML/flat_to_xml/out.xml")
try:
i, d, o = None, None, None
opts, args = getopt.getopt(sys.argv[1:], "hvi:d:o:", ["help"])
if len(opts) == 0:
usage()
sys.exit()
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
if opt in ("-v"):
version()
sys.exit()
if opt in ("-i"):
i = arg
if opt in ("-o"):
o = arg
if opt in ("-d"):
d = arg
if i != None and d != None:
convert(flat=i, xml=d, out=o)
else:
print "Error in parameters"
usage()
except getopt.GetoptError, r:
print "Error", r
usage()
except Exception,e:
print e |
Partager