-
Notifications
You must be signed in to change notification settings - Fork 135
/
upconvert.py
executable file
·84 lines (69 loc) · 2.42 KB
/
upconvert.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
#!/usr/bin/env python
""" A universal hardware design file format converter using
Upverter's Open JSON Interchange Format """
# upconvert.py - A universal hardware design file format converter using
# Upverter's Open JSON Interchange Format
# (http://upverter.com/resources/open-json-format/)
#
# Authors:
# Alex Ray [email protected]
# Upverter [email protected]
#
# Usage example:
# ./upconvert.py -i test.upv -o test.json
import os, re, copy, json
import parser.openjson, parser.kicad, parser.eaglexml
import writer.openjson, writer.kicad
from argparse import ArgumentParser
PARSERS = {
'openjson': parser.openjson.JSON,
'kicad': parser.kicad.KiCAD,
'eaglexml': parser.eaglexml.EagleXML,
}
WRITERS = {
'openjson': writer.openjson.JSON,
'kicad': writer.kicad.KiCAD
}
def parse(in_file, in_format='openjson'):
""" Parse the given input file using the in_format """
try:
p = PARSERS[in_format]()
except KeyError:
print "ERROR: Unsupported input type:", in_format
exit(1)
return p.parse(in_file)
def write(dsgn, out_file, out_format='openjson'):
""" Write the converted input file to the out_format """
try:
w = WRITERS[out_format]()
except KeyError:
print "ERROR: Unsupported output type:", out_format
exit(1)
return w.write(dsgn, out_file)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-i", "--input", dest="inputfile",
help="read INPUT file in", metavar="INPUT")
parser.add_argument("-f", "--from", dest="inputtype",
help="read input file as TYPE", metavar="TYPE",
default="openjson")
parser.add_argument("-o", "--output", dest="outputfile",
help="write OUTPUT file out", metavar="OUTPUT")
parser.add_argument("-t", "--to", dest="outputtype",
help="write output file as TYPE", metavar="TYPE",
default="openjson")
args = parser.parse_args()
inputtype = args.inputtype
outputtype = args.outputtype
inputfile = args.inputfile
outputfile = args.outputfile
if None == inputfile:
args.print_help()
exit(1)
# parse and export the data
design = parse(inputfile, inputtype)
if design is not None: # we got a good result
write(design, outputfile, outputtype)
else: # parse returned None -> something went wrong
print "Output cancelled due to previous errors."
exit(1)