-
Notifications
You must be signed in to change notification settings - Fork 1
/
idl2python.py
executable file
·108 lines (87 loc) · 3.04 KB
/
idl2python.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
#
# Copyright (C) 2009 Scott D. Peckham <[email protected]>
#
# This file is part of i2py. This is a simplified version of the
# idl2python shell script that is used on Unix systems, written
# for use on a PC running windows.
#
# 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
import i2py
################################################################################
#
# 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:
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
#
################################################################################
def convert(*args):
exit_stat = 0 # Exit status
outfile = None # Output file object
for infilename in args:
#
# Process the input file
#
## print 'infilename =', infilename ########
# 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
# Output goes to a file
outfile = file(make_outfile_name(infilename), 'w')
## print 'outfilename =', outfile.name ########
try:
outfile.write(output)
finally:
outfile.close()