mirror of
https://github.com/grp/Wii.py.git
synced 2025-06-18 14:55:35 -04:00
parsing and writing complete for csv
This commit is contained in:
parent
675bf6f947
commit
8fa3891f00
@ -4,41 +4,108 @@ import sys, re, struct
|
|||||||
|
|
||||||
from Struct import Struct
|
from Struct import Struct
|
||||||
|
|
||||||
|
hardstring = '\xfe\xff\x00\x22\x63\xa5\x7d\x9a\x30\x59\x30\x8b\x98\x06\x75\x6a'
|
||||||
|
hardstring += '\x30\x6b\x00\x0d\x00\x0a\x24\x60\x30\xdc\x30\xbf\x30\xf3\x30\x68'
|
||||||
|
|
||||||
def nullterm(str_plus):
|
def nullterm(str_plus):
|
||||||
z = str_plus.find('\x00\x22')
|
z = str_plus.find('\x00\x09')
|
||||||
|
if z != -1:
|
||||||
|
return str_plus[:z]
|
||||||
|
else:
|
||||||
|
return str_plus
|
||||||
|
|
||||||
|
def denullterm(str_plus):
|
||||||
|
z = str_plus.find('\x0a\x0a')
|
||||||
if z != -1:
|
if z != -1:
|
||||||
return str_plus[:z]
|
return str_plus[:z]
|
||||||
else:
|
else:
|
||||||
return str_plus
|
return str_plus
|
||||||
|
|
||||||
class CSV(object):
|
class CSV(object):
|
||||||
def __init__(self, data):
|
def __init__(self, data=None, outfile=None, debug=False):
|
||||||
self.data = []
|
self.data = []
|
||||||
if data != None:
|
if data != None:
|
||||||
self.Unpack(data)
|
self.Unpack(data, outfile, debug)
|
||||||
|
|
||||||
def Unpack(self, data):
|
def Unpack(self, data, outfile=None, debug=False):
|
||||||
pos = 0x4a
|
if outfile != None:
|
||||||
|
file = open(outfile, 'wb')
|
||||||
|
|
||||||
|
pos = 0
|
||||||
|
|
||||||
|
version = Struct.uint16(data[pos:pos+2], endian='>')
|
||||||
|
pos += 2
|
||||||
|
if debug == True:
|
||||||
|
print "Version: %04x" % version
|
||||||
|
print
|
||||||
|
|
||||||
|
self.string_list = []
|
||||||
while pos < len(data):
|
while pos < len(data):
|
||||||
string = nullterm(data[pos:])
|
string = nullterm(data[pos:])
|
||||||
string = unicode(string, 'utf_16_be')
|
string = unicode(string, 'utf_16_be')
|
||||||
print "String: %s" % string
|
if debug == True:
|
||||||
pos += len(string) * 2 + 6
|
print "String: %s" % string
|
||||||
|
pos += len(string) * 2 + 2
|
||||||
|
self.string_list.append(string)
|
||||||
|
file.write(string.encode('utf-8'))
|
||||||
|
file.write('\n')
|
||||||
|
if pos < len(data):
|
||||||
|
file.write('\n')
|
||||||
|
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
def write(self, data, output_file):
|
||||||
|
file = open(output_file, 'wb')
|
||||||
|
if file:
|
||||||
|
file.write('\xfe\xff')
|
||||||
|
|
||||||
|
pos = 0
|
||||||
|
while pos < len(data):
|
||||||
|
temp = denullterm(data[pos:])
|
||||||
|
pos += len(temp) + 2
|
||||||
|
string = unicode(temp, 'utf-8')
|
||||||
|
file.write(string.encode('utf_16_be'))
|
||||||
|
|
||||||
|
if pos < len(data):
|
||||||
|
file.write('\x00\x09')
|
||||||
|
else:
|
||||||
|
file.write('\x00\x0a')
|
||||||
|
|
||||||
|
file.close()
|
||||||
|
else:
|
||||||
|
print "Could not open file for writing"
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) == 1:
|
if len(sys.argv) == 1:
|
||||||
print 'Usage: python csv.py <filename>'
|
print 'Usage: python -r csv.py <filename.csv> <filename.hog>'
|
||||||
|
print ' == OR == '
|
||||||
|
print 'Usage: python -w csv.py <filename.hog> <filename.csv>'
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
f = open(sys.argv[1], 'rb')
|
if sys.argv[1] == "-r":
|
||||||
if f:
|
f = open(sys.argv[2], 'rb')
|
||||||
csv_buffer = f.read()
|
if f:
|
||||||
f.close()
|
csv_buffer = f.read()
|
||||||
|
f.close()
|
||||||
|
else:
|
||||||
|
print 'Could not open file for reading'
|
||||||
|
sys.exit(1)
|
||||||
|
csv = CSV(csv_buffer, outfile=sys.argv[3], debug=False)
|
||||||
|
elif sys.argv[1] == "-w":
|
||||||
|
f = open(sys.argv[2], 'rb')
|
||||||
|
if f:
|
||||||
|
input_file = f.read()
|
||||||
|
f.close()
|
||||||
|
else:
|
||||||
|
print 'Could not open file for reading'
|
||||||
|
sys.exit(1)
|
||||||
|
csv = CSV()
|
||||||
|
csv.write(input_file, sys.argv[3])
|
||||||
else:
|
else:
|
||||||
print 'Could not open file for reading'
|
print 'Usage: python -r csv.py <filename.csv> <filename.hog>'
|
||||||
|
print ' == OR == '
|
||||||
|
print 'Usage: python -w csv.py <filename.hog> <filename.csv>'
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
csv = CSV(csv_buffer)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user