@@ -9,9 +9,11 @@ a file, translate it, and write it out.
99import sys
1010from argparse import ArgumentParser , ArgumentTypeError
1111from collections import defaultdict
12- from logging import _levelNames as logLevels , exception , warning , info , basicConfig
13- from os import path , makedirs
12+ from os import path , makedirs , walk
13+ import logging
14+ from logging import basicConfig
1415from time import time
16+ from io import IOBase
1517
1618from java2python .compiler import Module , buildAST , transformAST
1719from java2python .config import Config
@@ -22,19 +24,7 @@ version = '0.5.1'
2224
2325
2426def logLevel (value ):
25- """ Returns a valid logging level or raises and exception. """
26- msg = 'invalid loglevel: %r'
27- try :
28- lvl = int (value )
29- except (ValueError , ):
30- name = value .upper ()
31- if name not in logLevels :
32- raise ArgumentTypeError (msg % value )
33- lvl = logLevels [name ]
34- else :
35- if lvl not in logLevels :
36- raise ArgumentTypeError (msg % value )
37- return lvl
27+ return logging .getLevelName (value .upper ())
3828
3929
4030def configFromDir (inname , dirname ):
@@ -60,24 +50,24 @@ def runOneOrMany(options):
6050 """ Runs our main transformer with each of the input files. """
6151 infile , outfile = options .inputfile , options .outputfile
6252
63- if infile and not isinstance (infile , file ) and path .isdir (infile ):
64- if outfile and not isinstance (outfile , file ) and not path .isdir (outfile ):
53+ if infile and not isinstance (infile , IOBase ) and path .isdir (infile ):
54+ if outfile and not isinstance (outfile , IOBase ) and not path .isdir (outfile ):
6555 warning ('Must specify output directory or stdout when using input directory.' )
6656 return 2
6757 def walker (arg , dirname , files ):
6858 for name in [name for name in files if name .endswith ('.java' )]:
6959 fullname = path .join (dirname , name )
7060 options .inputfile = fullname
7161 info ('opening %s' , fullname )
72- if outfile and outfile != '-' and not isinstance (outfile , file ):
62+ if outfile and outfile != '-' and not isinstance (outfile , IOBase ):
7363 full = path .abspath (path .join (outfile , fullname ))
7464 head , tail = path .split (full )
7565 tail = path .splitext (tail )[0 ] + '.py'
7666 if not path .exists (head ):
7767 makedirs (head )
7868 options .outputfile = path .join (head , tail )
7969 runTransform (options )
80- path . walk (infile , walker , None )
70+ walk (infile , walker , None )
8171 return 0
8272 else :
8373 return runTransform (options )
@@ -89,15 +79,15 @@ def runTransform(options):
8979 timed ['overall' ]
9080
9181 filein = fileout = filedefault = '-'
92- if options .inputfile and not isinstance (options .inputfile , file ):
82+ if options .inputfile and not isinstance (options .inputfile , IOBase ):
9383 filein = options .inputfile
94- if options .outputfile and not isinstance (options .outputfile , file ):
84+ if options .outputfile and not isinstance (options .outputfile , IOBase ):
9585 fileout = options .outputfile
9686 elif fileout != filedefault :
9787 fileout = '%s.py' % (path .splitext (filein )[0 ])
9888
9989 configs = options .configs
100- if options .configdirs and not isinstance (filein , file ):
90+ if options .configdirs and not isinstance (filein , IOBase ):
10191 for configdir in options .configdirs :
10292 dirname = configFromDir (filein , configdir )
10393 if path .exists (dirname ):
@@ -110,15 +100,15 @@ def runTransform(options):
110100 source = open (filein ).read ()
111101 else :
112102 source = sys .stdin .read ()
113- except (IOError , ), exc :
103+ except (IOError , ) as exc :
114104 code , msg = exc .args [0 :2 ]
115- print 'IOError: %s.' % (msg , )
105+ print ( 'IOError: %s.' % (msg , ) )
116106 return code
117107
118108 timed ['comp' ]
119109 try :
120110 tree = buildAST (source )
121- except (Exception , ), exc :
111+ except (Exception , ) as exc :
122112 exception ('exception while parsing' )
123113 return 1
124114 timed ['comp_finish' ]
@@ -136,35 +126,35 @@ def runTransform(options):
136126 timed ['visit_finish' ]
137127
138128 timed ['encode' ]
139- source = unicode (module )
129+ source = str (module )
140130 timed ['encode_finish' ]
141131 timed ['overall_finish' ]
142132
143133 if options .lexertokens :
144134 for idx , tok in enumerate (tree .parser .input .tokens ):
145- print >> sys . stderr , '{0} {1}' .format (idx , tok )
146- print >> sys .stderr
135+ print ( '{0} {1}' .format (idx , tok ), file = sys . stderr )
136+ print ( file = sys .stderr )
147137
148138 if options .javaast :
149139 tree .dump (sys .stderr )
150- print >> sys .stderr
140+ print ( file = sys .stderr )
151141
152142 if options .pytree :
153143 module .dumpRepr (sys .stderr )
154- print >> sys .stderr
144+ print ( file = sys .stderr )
155145
156146 if not options .skipsource :
157147 if fileout == filedefault :
158148 output = sys .stdout
159149 else :
160150 output = open (fileout , 'w' )
161151 module .name = path .splitext (filein )[0 ] if filein != '-' else '<stdin>'
162- print >> output , source
152+ print ( source , file = output )
163153
164154 if not options .skipcompile :
165155 try :
166156 compile (source , '<string>' , 'exec' )
167- except (SyntaxError , ), ex :
157+ except (SyntaxError , ) as ex :
168158 warning ('Generated source has invalid syntax. %s' , ex )
169159 else :
170160 info ('Generated source has valid syntax.' )
0 commit comments