2020-08-08 23:59:13 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
|
|
"""
|
2023-11-15 21:36:12 +01:00
|
|
|
Legacy fixed width record importing functions.
|
2020-08-08 23:59:13 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import builtins
|
2023-11-15 21:36:12 +01:00
|
|
|
|
|
|
|
|
def parse_fwr (string, widths, start=0):
|
|
|
|
|
"""Parse a fixed-width record.
|
|
|
|
|
|
|
|
|
|
string: the string to parse.
|
|
|
|
|
widths: a list of record widths. A negative width denotes a field to be skipped.
|
|
|
|
|
start: optional start index.
|
|
|
|
|
|
|
|
|
|
Returns a list of strings.
|
|
|
|
|
"""
|
|
|
|
|
results = []
|
|
|
|
|
current_index = start
|
|
|
|
|
for width in widths:
|
|
|
|
|
if width > 0:
|
|
|
|
|
results.append(string[current_index : current_index + width])
|
|
|
|
|
current_index += width
|
|
|
|
|
else:
|
|
|
|
|
current_index -= width
|
|
|
|
|
|
|
|
|
|
return results
|
2020-08-08 23:59:13 +02:00
|
|
|
|
|
|
|
|
def int (v):
|
|
|
|
|
return builtins.int(float(v))
|
|
|
|
|
|
|
|
|
|
def parse_line (string, spec):
|
|
|
|
|
"""Parse a line from an SPS file."""
|
|
|
|
|
names = spec["names"]
|
|
|
|
|
widths = spec["widths"]
|
|
|
|
|
normalisers = spec["normalisers"]
|
|
|
|
|
record = [ t[0](t[1]) for t in zip(normalisers, parse_fwr(string, widths)) ]
|
|
|
|
|
return dict(zip(names, record))
|
|
|
|
|
|
|
|
|
|
def from_file(path, spec = None):
|
|
|
|
|
if spec is None:
|
|
|
|
|
spec = {
|
|
|
|
|
"names": [ "line_name", "point_number", "easting", "northing" ],
|
|
|
|
|
"widths": [ -1, 10, 10, -25, 10, 10 ],
|
|
|
|
|
"normalisers": [ int, int, float, float ]
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
normaliser_tokens = [ "int", "float", "str", "bool" ]
|
|
|
|
|
spec["normalisers"] = [ eval(t) for t in spec["types"] if t in normaliser_tokens ]
|
|
|
|
|
|
|
|
|
|
records = []
|
|
|
|
|
with open(path) as fd:
|
|
|
|
|
cnt = 0
|
|
|
|
|
line = fd.readline()
|
|
|
|
|
while line:
|
|
|
|
|
cnt = cnt+1
|
|
|
|
|
|
|
|
|
|
if line == "EOF":
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
record = parse_line(line, spec)
|
|
|
|
|
if record is not None:
|
|
|
|
|
records.append(record)
|
|
|
|
|
|
|
|
|
|
line = fd.readline()
|
|
|
|
|
|
2020-10-04 03:42:19 +02:00
|
|
|
del spec["normalisers"]
|
2020-08-08 23:59:13 +02:00
|
|
|
return records
|