python-schdoc/parse.py

98 lines
3.5 KiB
Python
Raw Normal View History

2020-03-07 08:01:22 +00:00
import argparse, textwrap
import olefile
import re
import json
import copy
2020-03-07 08:01:22 +00:00
def parse(input, format, **kwargs):
fullPath = input
blah = olefile.OleFileIO(fullPath)
stream = blah.openstream('FileHeader')
# split binary stream into lines using a repeated 5 byte signature
pattern = re.compile(b'.{3}\x00\x00\|')
lines = pattern.split(stream.read()[5:-1]) # lopping off first 4 bytes, and last byte, since they don't seem to matter?
schematic = {}
datums = []
# loop through every "line" and parse each into a dictionary
for line in lines:
datum = {}
pairs = line.split(b"|")
for pair in pairs:
data = pair.split(b"=")
datum[data[0].decode()] = data[1].decode()
datums.append(datum)
# separate out the header dictionary from the "records" dictionaries
schematic["header"] = [x for x in datums if 'HEADER' in x.keys()]
schematic["records"] = [x for x in datums if 'RECORD' in x.keys()]
# prep a scratchpad copy of records to build hierarchy from
records_copy = copy.deepcopy(schematic["records"])
schematic["hierarchy"] = []
# loop through all "records" and organize them into owner/children
for i, current in enumerate(records_copy):
current['index'] = i
s = current.get("OWNERINDEX")
if (s is None):
schematic["hierarchy"].append(current)
else:
ownerIndex = int(s)
owner = records_copy[ownerIndex]
if (owner.get("children") == None):
owner["children"] = []
owner["children"].append(current)
2020-03-07 08:01:22 +00:00
if format == 'json-hierarchy':
schematic["records"] = schematic["hierarchy"]
schematic.pop("hierarchy", None)
2020-03-04 08:34:33 +00:00
return schematic
2020-03-07 08:25:38 +00:00
def determine_parts_list(schematic):
print('determine parts list')
2020-03-12 06:22:49 +00:00
for record in schematic["records"]:
if record["RECORD"] is "1":
print("part: " + str(record))
2020-03-07 08:25:38 +00:00
2020-03-07 08:12:06 +00:00
def main(args):
schematic = parse(**vars(args))
if args.output:
2020-03-07 08:25:38 +00:00
if args.format in ('json-flat', 'json-hierarchy'):
json_file = open(args.output, 'w')
json.dump(schematic, json_file, indent=4)
elif args.format == 'parts-list':
print('blah')
determine_parts_list(schematic)
else:
print('no format')
2020-03-07 08:12:06 +00:00
else:
print(schematic)
if __name__ == "__main__":
2020-03-07 08:01:22 +00:00
parser = argparse.ArgumentParser(description='Converts Altium .SchDoc files into json.', formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--input', '-i', dest='input',
help='path/to/altiumschematic.schdoc file to parse')
parser.add_argument('--output', '-o', dest='output',
help='path/to/jsonfile.json file to output json to, otherwise prints to terminal')
2020-03-07 08:12:06 +00:00
parser.add_argument('format', nargs='?', default='json-hierarchy',
2020-03-07 08:01:22 +00:00
choices=['json-flat', 'json-hierarchy', 'parts-list', 'net-list'],
help=textwrap.dedent('''\
json-flat, json-hierarchy: Organize records into owner/child "hierarchy" or leave as a "flat" list
parts-list: A listing of parts and their designators
net-list: A listing of nets between parts pins, referred to by their designators'''))
args = parser.parse_args()
2020-03-07 08:12:06 +00:00
main(args)