-
Notifications
You must be signed in to change notification settings - Fork 1
/
idl2python
executable file
·164 lines (131 loc) · 4.64 KB
/
idl2python
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
#
# Copyright (C) 2005 Christopher J. Stawarz <chris@pseudogreen.org>
#
# This file is part of i2py.
#
# i2py is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# i2py is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with i2py; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import sys
import os.path
from optparse import OptionParser
import i2py
################################################################################
#
# Process command-line options and arguments
#
################################################################################
# Create the OptionParser
oparser = OptionParser(usage=('%prog [-d] [-s] [-r RCFILE] [-o OUTFILE] ' +
'INFILE ...'),
version=('%%prog %s' % i2py.__version__))
oparser.add_option('-d', '--dump', action='store_true',
help='dump parse tree to stdout as IDL code')
oparser.add_option('-o', '--outfile', help='write all output to OUTFILE')
oparser.add_option('-r', '--rcfile',
help='get configuration from RCFILE instead of i2pyrc')
oparser.add_option('-s', '--stdout', action='store_true',
help='write output to stdout')
# Parse the command line
opts, args = oparser.parse_args()
# Load the configuration file
i2py.load_rcfile(opts.rcfile)
# If no arguments or the single argument '-' were given, the input comes from
# stdin
if (len(args) == 0) or ((len(args) == 1) and (args[0] == '-')):
args = [sys.stdin]
################################################################################
#
# Define some work functions
#
################################################################################
#
# Parses the contents of open file object infile and generates the output code.
# If no errors occur, returns the output string. Otherwise, prints the errors
# to stderr (with infile.name prepended to each message) and returns None.
#
def process_input(infile):
output = i2py.parse(infile.read())
if output:
if opts.dump:
output = str(output)
else:
output = output.pycode()
if (not output) or i2py.error_occurred():
for err in i2py.get_error_list():
sys.stderr.write('%s:%s\n' % (infile.name, err))
return None
return output
#
# Returns infilename with the extension changed to '.py'. Assumes infilename
# has a non-empty basename (i.e. it's a file name, not a directory name).
#
def make_outfile_name(infilename):
dirname, basename = os.path.split(infilename)
dot_index = basename.rfind('.')
if dot_index > 0:
basename = basename[0:dot_index]
return os.path.join(dirname, basename + '.py')
################################################################################
#
# Do the actual work
#
################################################################################
exit_stat = 0 # Exit status
outfile = None # Output file object
try:
for infilename in args:
#
# Process the input file
#
if infilename is sys.stdin:
# Read from stdin
output = process_input(sys.stdin)
else:
# Read from a file
infile = file(infilename, 'U') # Open in universal newline mode
try:
output = process_input(infile)
finally:
infile.close()
#
# Write the output file
#
# If an error occurred, don't write any output
if not output:
exit_stat = 1
continue
if opts.outfile:
# --outfile was given, so all output goes to the specified file
if not outfile:
outfile = file(opts.outfile, 'w')
outfile.write(output)
elif opts.dump or opts.stdout or (infilename is sys.stdin):
# --dump or --stdout was given or the input came from stdin, so
# the output goes to stdout
sys.stdout.write(output)
else:
# Output goes to a file
outfile = file(make_outfile_name(infilename), 'w')
try:
outfile.write(output)
finally:
outfile.close()
finally:
# If --outfile was given, close the output file
if opts.outfile and outfile:
outfile.close()
# Done!
sys.exit(exit_stat)