-
Notifications
You must be signed in to change notification settings - Fork 1
/
makeDB.py
executable file
·70 lines (48 loc) · 1.74 KB
/
makeDB.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
#!/usr/bin/env python
from optparse import OptionParser
import os
import sys
def check_inputs(options, args):
if (not len(args) == 1):
parser.print_usage()
return False
if (not options.output):
directory = os.path.dirname(args[0])
basename = os.path.basename(args[0])
basename = basename.rsplit(".", 1)[0] + ".db"
options.output = os.path.join(directory, basename)
return True
def parse(input_file, output_file, options):
for line in input_file:
line = line.strip()
if (not line or line[0] == "#"):
continue
(first_half, second_half) = line.split("->")
name = first_half.split("[")[0].strip()
type = second_half.split("/")[0].strip()
to_write = """\
record(%s, %s)
{
field(DTYP, "asynInt32")
field(SCAN, "I/O Intr")
field(INP, "@asyn($(PORT), 0, 0)%s")
}
"""
if type == "Bool":
output_file.write(to_write % ("bi", options.prefix + name, name))
else:
output_file.write(to_write % ("ai", options.prefix + name, name))
if __name__ == "__main__":
usage = "Usage: %prog specification_file [options]"
parser = OptionParser(usage=usage)
parser.add_option("-p", "--prefix", metavar="PREFIX",
action="store", type="string", dest="prefix", default="$(P)$(R)",
help="Record name prefix [default: %default]")
parser.add_option("-o", "--output", metavar="FILE",
action="store", type="string", dest="output", default=None,
help="Output database filename. By default, uses the input file to determine output name")
(options, args) = parser.parse_args()
if check_inputs(options, args):
with open(args[0], "r") as input_file:
with open(options.output, "w") as output_file:
parse(input_file, output_file, options)