Skip to content

Working in Python3 #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 29 additions & 39 deletions bin/j2py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ a file, translate it, and write it out.
import sys
from argparse import ArgumentParser, ArgumentTypeError
from collections import defaultdict
from logging import _levelNames as logLevels, exception, warning, info, basicConfig
from os import path, makedirs
from os import path, makedirs, walk
import logging
from logging import basicConfig
from time import time
from io import IOBase

from java2python.compiler import Module, buildAST, transformAST
from java2python.config import Config
Expand All @@ -22,19 +24,7 @@ version = '0.5.1'


def logLevel(value):
""" Returns a valid logging level or raises and exception. """
msg = 'invalid loglevel: %r'
try:
lvl = int(value)
except (ValueError, ):
name = value.upper()
if name not in logLevels:
raise ArgumentTypeError(msg % value)
lvl = logLevels[name]
else:
if lvl not in logLevels:
raise ArgumentTypeError(msg % value)
return lvl
return logging.getLevelName(value.upper())


def configFromDir(inname, dirname):
Expand All @@ -60,24 +50,24 @@ def runOneOrMany(options):
""" Runs our main transformer with each of the input files. """
infile, outfile = options.inputfile, options.outputfile

if infile and not isinstance(infile, file) and path.isdir(infile):
if outfile and not isinstance(outfile, file) and not path.isdir(outfile):
if infile and not isinstance(infile, IOBase) and path.isdir(infile):
if outfile and not isinstance(outfile, IOBase) and not path.isdir(outfile):
warning('Must specify output directory or stdout when using input directory.')
return 2
def walker(arg, dirname, files):
for name in [name for name in files if name.endswith('.java')]:
fullname = path.join(dirname, name)
options.inputfile = fullname
info('opening %s', fullname)
if outfile and outfile != '-' and not isinstance(outfile, file):
logging.info('opening %s', fullname)
if outfile and outfile != '-' and not isinstance(outfile, IOBase):
full = path.abspath(path.join(outfile, fullname))
head, tail = path.split(full)
tail = path.splitext(tail)[0] + '.py'
if not path.exists(head):
makedirs(head)
options.outputfile = path.join(head, tail)
runTransform(options)
path.walk(infile, walker, None)
walk(infile, walker, None)
return 0
else:
return runTransform(options)
Expand All @@ -89,15 +79,15 @@ def runTransform(options):
timed['overall']

filein = fileout = filedefault = '-'
if options.inputfile and not isinstance(options.inputfile, file):
if options.inputfile and not isinstance(options.inputfile, IOBase):
filein = options.inputfile
if options.outputfile and not isinstance(options.outputfile, file):
if options.outputfile and not isinstance(options.outputfile, IOBase):
fileout = options.outputfile
elif fileout != filedefault:
fileout = '%s.py' % (path.splitext(filein)[0])

configs = options.configs
if options.configdirs and not isinstance(filein, file):
if options.configdirs and not isinstance(filein, IOBase):
for configdir in options.configdirs:
dirname = configFromDir(filein, configdir)
if path.exists(dirname):
Expand All @@ -110,15 +100,15 @@ def runTransform(options):
source = open(filein).read()
else:
source = sys.stdin.read()
except (IOError, ), exc:
except (IOError, ) as exc:
code, msg = exc.args[0:2]
print 'IOError: %s.' % (msg, )
print('IOError: %s.' % (msg, ))
return code

timed['comp']
try:
tree = buildAST(source)
except (Exception, ), exc:
except (Exception, ) as exc:
exception('exception while parsing')
return 1
timed['comp_finish']
Expand All @@ -136,44 +126,44 @@ def runTransform(options):
timed['visit_finish']

timed['encode']
source = unicode(module)
source = str(module)
timed['encode_finish']
timed['overall_finish']

if options.lexertokens:
for idx, tok in enumerate(tree.parser.input.tokens):
print >> sys.stderr, '{0} {1}'.format(idx, tok)
print >> sys.stderr
print('{0} {1}'.format(idx, tok), file=sys.stderr)
print(file=sys.stderr)

if options.javaast:
tree.dump(sys.stderr)
print >> sys.stderr
print(file=sys.stderr)

if options.pytree:
module.dumpRepr(sys.stderr)
print >> sys.stderr
print(file=sys.stderr)

if not options.skipsource:
if fileout == filedefault:
output = sys.stdout
else:
output = open(fileout, 'w')
module.name = path.splitext(filein)[0] if filein != '-' else '<stdin>'
print >> output, source
print(source, file=output)

if not options.skipcompile:
try:
compile(source, '<string>', 'exec')
except (SyntaxError, ), ex:
except (SyntaxError, ) as ex:
warning('Generated source has invalid syntax. %s', ex)
else:
info('Generated source has valid syntax.')
logging.info('Generated source has valid syntax.')

info('Parse: %.4f seconds', timed['comp_finish'] - timed['comp'])
info('Visit: %.4f seconds', timed['visit_finish'] - timed['visit'])
info('Transform: %.4f seconds', timed['xform_finish'] - timed['xform'])
info('Encode: %.4f seconds', timed['encode_finish'] - timed['encode'])
info('Total: %.4f seconds', timed['overall_finish'] - timed['overall'])
logging.info('Parse: %.4f seconds', timed['comp_finish'] - timed['comp'])
logging.info('Visit: %.4f seconds', timed['visit_finish'] - timed['visit'])
logging.info('Transform: %.4f seconds', timed['xform_finish'] - timed['xform'])
logging.info('Encode: %.4f seconds', timed['encode_finish'] - timed['encode'])
logging.info('Total: %.4f seconds', timed['overall_finish'] - timed['overall'])
return 0


Expand Down
7 changes: 4 additions & 3 deletions java2python/compiler/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
from java2python.compiler import template, visitor


def addTypeToModule((className, factoryName)):
def addTypeToModule(xxx_todo_changeme):
""" Constructs and adds a new type to this module. """
(className, factoryName) = xxx_todo_changeme
bases = (getattr(template, className), getattr(visitor, className))
newType = type(className, bases, dict(factoryName=factoryName))
setattr(modules[__name__], className, newType)


map(addTypeToModule, (
list(map(addTypeToModule, (
('Annotation', 'at'),
('Class', 'klass'),
('Comment', 'comment'),
Expand All @@ -35,4 +36,4 @@ def addTypeToModule((className, factoryName)):
('Module', 'module'),
('Statement', 'statement'),
)
)
))
26 changes: 13 additions & 13 deletions java2python/compiler/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
# are usually a sign of a bad design and/or language limitations, and
# this case is no exception.

from cStringIO import StringIO
from io import StringIO
from functools import partial
from itertools import chain, ifilter, imap
from itertools import chain

from java2python.lang import tokens
from java2python.lib import FS, colors
from functools import reduce


class Factory(object):
Expand Down Expand Up @@ -74,7 +75,7 @@ def __init__(cls, name, bases, namespace):
pass


class Base(object):
class Base(object, metaclass=FactoryTypeDetector):
""" Base -> base class for formatting Python output.

This class defines a large set of attributes and methods for the
Expand Down Expand Up @@ -110,7 +111,6 @@ class Base(object):
a the template as tree for debugging.

"""
__metaclass__ = FactoryTypeDetector
isAnnotation = isClass = isComment = isEnum = isExpression = \
isInterface = isMethod = isModule = isStatement = False

Expand Down Expand Up @@ -155,7 +155,7 @@ def altIdent(self, name):
for klass in self.parents(lambda v:v.isClass):
if name in klass.variables:
try:
method = self.parents(lambda v:v.isMethod).next()
method = next(self.parents(lambda v:v.isMethod))
except (StopIteration, ):
return name
if name in [p['name'] for p in method.parameters]:
Expand All @@ -173,20 +173,20 @@ def configHandler(self, part, suffix='Handler', default=None):
def configHandlers(self, part, suffix='Handlers'):
""" Returns config handlers for this type of template """
name = '{0}{1}{2}'.format(self.typeName, part, suffix)
return imap(self.toIter, chain(*self.config.every(name, [])))
return map(self.toIter, chain(*self.config.every(name, [])))

def dump(self, fd, level=0):
""" Writes the Python source code for this template to the given file. """
indent, isNotNone = level * self.indent, lambda x:x is not None
lineFormat = '{0}{1}\n'.format
for line in ifilter(isNotNone, self.iterPrologue()):
for line in filter(isNotNone, self.iterPrologue()):
line = lineFormat(indent, line)
fd.write(line if line.strip() else '\n')
for item in ifilter(isNotNone, self.iterHead()):
for item in filter(isNotNone, self.iterHead()):
item.dump(fd, level+1)
for item in self.iterBody():
item.dump(fd, level+1)
for line in ifilter(isNotNone, self.iterEpilogue()):
for line in filter(isNotNone, self.iterEpilogue()):
line = lineFormat(indent, line)
fd.write(line if line.strip() else '\n')

Expand All @@ -200,7 +200,7 @@ def dumpRepr(self, fd, level=0):
""" Writes a debug string for this template to the given file. """
indent, default = self.indent, lambda x, y:None
fd.write('{0}{1!r}\n'.format(indent*level, self))
for child in ifilter(None, self.children):
for child in filter(None, self.children):
getattr(child, 'dumpRepr', default)(fd, level+1)

@property
Expand Down Expand Up @@ -230,7 +230,7 @@ def iterPrologue(self):
def iterHead(self):
""" Yields the items in the head of this template. """
items = chain(*(h(self) for h in self.configHandlers('Head')))
return imap(self.toExpr, items)
return map(self.toExpr, items)

def iterBody(self):
""" Yields the items in the body of this template. """
Expand Down Expand Up @@ -304,10 +304,10 @@ def __init__(self, config, left='', right='', fs=FS.lr, parent=None, tail=''):
def __repr__(self):
""" Returns the debug string representation of this template. """
parts, parent, showfs = [colors.blue(self.typeName)], self.parent, True
if isinstance(self.left, (basestring, )) and self.left:
if isinstance(self.left, str) and self.left:
parts.append(colors.white('left:') + colors.yellow(self.left))
showfs = False
if isinstance(self.right, (basestring, )) and self.right:
if isinstance(self.right, str) and self.right:
parts.append(colors.white('right:') + colors.yellow(self.right))
showfs = False
if self.modifiers:
Expand Down
Loading