Merge pull request #2 from a3ng7n/feature/consolidation

Feature/consolidation
master
Aaron 2020-03-04 02:04:31 -07:00 committed by GitHub
commit f8135b668f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4855 changed files with 116 additions and 75658 deletions

5
.gitignore vendored
View File

@ -45,3 +45,8 @@ $RECYCLE.BIN/
Network Trash Folder
Temporary Items
.apdisk
/venv/
/.ipynb_checkpoints/
/.idea/
*.json
/Altium_Schematic_Parser.egg-info/

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,2 +1,11 @@
# Altium-Schematic-Parser
Converts Altium .SchDoc files into json
## Prerequisites
* python 3
* olefile
## Install
git clone git@github.com:a3ng7n/Altium-Schematic-Parser.git
cd Altium-Schematic-Parser
pip install -e .
## Usage
python parse.py -i "path/to/altiumschematic.schdoc" -o "path/to/jsonfile.json"

79
parse.py Normal file
View File

@ -0,0 +1,79 @@
import argparse
import olefile
import re
import json
import copy
def parse(input, json_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)
if json_format == 'hierarchy':
schematic["records"] = schematic["hierarchy"]
schematic.pop("hierarchy", None)
return schematic
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Converts Altium .SchDoc files into json.')
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')
parser.add_argument('json_format', default='hierarchy', nargs='?', choices=['flat', 'hierarchy'],
help='Organize records into owner/child "hierarchy" or leave as a "flat" list.')
args = parser.parse_args()
schematic = parse(**vars(args))
if args.output:
json_file = open(args.output, 'w')
json.dump(schematic, json_file)
else:
print(schematic)

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
olefile>=0.46

22
setup.py Normal file
View File

@ -0,0 +1,22 @@
import os
from setuptools import setup, find_packages
with open("README.md", "r") as fh:
long_description = fh.read()
def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()
setup(
name='Altium-Schematic-Parser',
version='0.0.0',
packages=find_packages(exclude=['tests']),
url='https://github.com/a3ng7n/Altium-Schematic-Parser',
license='see LICENSE',
author='Aaron Goldstein',
author_email='aaronmgoldstein@gmail.com',
description='Converts Altium .SchDoc files into json',
long_description=long_description,
long_description_content_type="text/markdown",
install_requires=read('requirements.txt').splitlines()
)

Some files were not shown because too many files have changed in this diff Show More