forked from dark-nova/ies2csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ies2csv.py
executable file
·317 lines (256 loc) · 8.37 KB
/
ies2csv.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python
import argparse
import os
import struct
from pathlib import Path
parser = argparse.ArgumentParser(
description = 'An .ies file to tsv converter'
)
subparser = parser.add_subparsers(
help = 'subcommand help',
required = True,
dest = 'subcommand'
)
parser_file = subparser.add_parser(
'file',
help = 'file help'
)
parser_file.add_argument(
'--output', '-o',
required = False,
help = 'An optional file to output to; overrides default file name',
type = Path
)
parser_file.add_argument(
'ies_file',
help = 'The .ies file to convert',
type = Path
)
parser_batch = subparser.add_parser(
'batch',
help = 'batch help'
)
parser_batch.add_argument(
'directory',
help = 'The directory with .ies files to batch convert',
type = Path
)
NULL_BYTE = '\x00'
SEPARATOR = '\t'
LINE = '\n'
def convert_bytestring(bstr: bytes):
"""Converts a bytestring to a readable string.
Args:
bstr (bytes): the bytestring to decode
Returns:
str: the appropriate string
"""
return bytes(
[(int(b) ^ 0x1) for b in bstr if int(b) != 0]
).decode(errors='ignore').rstrip(NULL_BYTE)
def get_int_from_bytes(bstr: bytes):
"""Get `int` from `bytes`. Obviously
Uses little endian to convert.
Args:
bstr (bytes): the bytestring chunk to convert
Returns:
int: the number converted
"""
return int.from_bytes(bstr, byteorder = 'little')
def get_col_names(
file: Path, bstr: bytes, ncols: int, offset: int, ncols_int: int
):
"""Gets column names from the bytestring of an `.ies` file.
Args:
file (Path): the file itself
bstr (bytes): the bytestring
ncols (int): number of columns
offset (int): offset to start from the bytestring
ncols_int (int): offset to specific columns
Returns:
dict: with key = index and value = column name
Raises:
Exception: if the `.ies` file is corrupt or invalid
"""
col_names = {}
for _ in range(ncols):
col_name = convert_bytestring(bstr[offset:offset+64])
# `n2` is unnecessary in this port.
# Just add 128; 64 for 64 bytes + 64 for `n2`.
offset += 128
col_type = get_int_from_bytes(bstr[offset:offset+2])
# `dummy` is unnecessary in this port.
# Just add 6; 2 for short + 4 for `dummy`.
offset += 6
col_idx = get_int_from_bytes(bstr[offset:offset+2])
offset += 2
if col_type == 0:
try:
if col_names[col_idx]:
raise Exception(
f'IES file {file} is invalid: '
f'{col_names[col_idx]} is not null'
)
except KeyError:
col_names[col_idx] = col_name
else:
try:
if col_names[col_idx + ncols_int]:
raise Exception(
f'IES file {file} is invalid: '
f'{col_names[col_idx+ncols_int]} is not null'
)
except KeyError:
col_names[col_idx + ncols_int] = col_name
return col_names
def get_rows(
file: Path, bstr: bytes, tsv: list, nrows: int, offset: int,
ncols_int: int, ncols_str: int
):
"""Gets rows from the bytestring of an `.ies` file.
Args:
file (Path): the file itself
bstr (bytes): the bytestring
tsv (list): the tsv in list form
nrows (int): number of rows
offset: offset to specify columns
ncols_int (int): number of numeric columns
ncols_str (int): number of string columns
Returns:
list: `tsv` with rows populated
Raises:
Exception: if the `.ies` file is corrupt or invalid
"""
for _ in range(nrows):
row_id = get_int_from_bytes(bstr[offset:offset+4])
offset += 4
# `row_class` is prepended before each record
# but is not used in the `.tsv` until column 3.
# This has an effect of creating padding before
# each "row".
# `lookupkey` is unnecessary in this port, since
# `row_class` is equivalent to it.
row_class = get_int_from_bytes(bstr[offset:offset+2])
offset += 2 + row_class
objs = {}
row = []
for i in range(ncols_int):
# Equivalent to `br.ReadSingle`, line 103.
col = int(struct.unpack('<f', bstr[offset:offset+4])[0])
if col is None:
raise Exception(
f'IES file {file} is invalid: obj is null'
)
row.append(col)
offset += 4
for i in range(ncols_str):
# Equivalent to `br.ReadUInt16`, line 110.
# `col_len` is the character length of the current column.
col_len = struct.unpack('<H', bstr[offset:offset+2])[0]
offset += 2
col = convert_bytestring(bstr[offset:offset+col_len])
if col is None:
raise Exception(
f'IES file {file} is invalid: obj is null'
)
row.append(col)
offset += col_len
tsv.append(row)
offset += ncols_str
return tsv
def convert_file(file: Path, dest: Path = None):
"""Converts a `file` fully from bytes to string.
Optionally outputs to new file `dest`, if not run in batch mode.
(`dest` is not None.)
Args:
file (Path): the file to convert
dest (Path, optional): the destination output; defaults to None
Returns:
bool: True if successful; False otherwise
Raises:
Exception: if the `.ies` file is corrupt or invalid
"""
bstr = file.read_bytes()
table_name = bstr[0:128].decode().rstrip(NULL_BYTE)
# Equivalent to original `val1`, `offset1`, `offset2`, and `filesize`.
# I interpreted it as `value`, but I am unsure.
# Four value slicing equivalent to `ReadInt32`.
value, offset1, offset2, file_size = [
get_int_from_bytes(bstr[i:i+4])
for i
in (128, 132, 136, 140)
]
if len(bstr) != file_size:
raise Exception(
f'IES file {file} has invalid length specified: {len(bstr)}'
)
if value != 1:
raise Exception(
f'IES file {file} has incorrect value: {value}'
)
# Equivalent to original `rows`, `cols`, `ncols_int`, and `ncols_str`.
# `short1` is unnecessary in this port.
# Two value slicing equivalent to `ReadInt16`.
nrows, ncols, ncols_int, ncols_str = [
get_int_from_bytes(bstr[i:i+2])
for i
in (146, 148, 150, 152)
]
if ncols != ncols_int + ncols_str:
raise Exception(
f'IES file {file} has mismatched cols: '
f'{ncols}!={ncols_int}+{ncols_str}'
)
# Equivalent to `ms.Seek`.`
offset_idx = file_size - offset1 - offset2
col_names = get_col_names(file, bstr, ncols, offset_idx, ncols_int)
tsv = []
row = []
for i in range(ncols):
if col_names[i] is None:
raise Exception(
f'IES file {file} is invalid: '
f'col_names at index {i} is null'
)
row.append(str(col_names[i]))
tsv.append(row)
offset_idx = file_size - offset2 # equivalent to `ms.Seek`, line 89
tsv = get_rows(file, bstr, tsv, nrows, offset_idx, ncols_int, ncols_str)
out = Path(
f'{file.stem}.tsv'
if dest is None
else dest
)
out.write_text(
LINE.join(
[
SEPARATOR.join(line)
for line
in tsv
]
)
)
return True
def batch_convert_dir(directory: Path):
"""Traverses a `directory` with max-depth of 1 to convert all
`.ies` files.
Args:
directory (Path): the directory itself (usually relative)
Returns:
None
"""
for file in directory.glob('*.ies'):
try:
convert_file(file)
except Exception as e:
print(
f"""Exception caught: {e}'
{file} was subsequently skipped."""
)
return
if __name__ == "__main__":
args = parser.parse_args()
if args.subcommand == 'file':
convert_file(args.ies_file, args.output)
else:
batch_convert_dir(args.directory)