Skip to content
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
25 changes: 12 additions & 13 deletions bin/j2py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def logLevel(value):
msg = 'invalid loglevel: %r'
try:
lvl = int(value)
except (ValueError, ):
except ValueError:
name = value.upper()
if name not in logLevels:
raise ArgumentTypeError(msg % value)
Expand All @@ -40,21 +40,20 @@ def logLevel(value):
def configFromDir(inname, dirname):
""" Returns a file name from the given config directory. """
name = path.join(dirname, path.basename(path.splitext(inname)[0]))
return '%s.py' % path.abspath(name)
return f'{path.abspath(name)}.py'


def runMain(options):
""" Runs our main function with profiling if indicated by options. """
if options.profile:
import cProfile, pstats
prof = cProfile.Profile()
prof.runcall(runOneOrMany, options)
stats = pstats.Stats(prof, stream=sys.stderr)
stats.strip_dirs().sort_stats('cumulative')
stats.print_stats().print_callers()
return 0
else:
if not options.profile:
return runOneOrMany(options)
import cProfile, pstats
prof = cProfile.Profile()
prof.runcall(runOneOrMany, options)
stats = pstats.Stats(prof, stream=sys.stderr)
stats.strip_dirs().sort_stats('cumulative')
stats.print_stats().print_callers()
return 0

def runOneOrMany(options):
""" Runs our main transformer with each of the input files. """
Expand All @@ -72,7 +71,7 @@ def runOneOrMany(options):
if outfile and outfile != '-' and not isinstance(outfile, file):
full = path.abspath(path.join(outfile, fullname))
head, tail = path.split(full)
tail = path.splitext(tail)[0] + '.py'
tail = f'{path.splitext(tail)[0]}.py'
if not path.exists(head):
makedirs(head)
options.outputfile = path.join(head, tail)
Expand Down Expand Up @@ -242,7 +241,7 @@ def configScript(argv):
add('-t', '--lexer-tokens', dest='lexertokens',
help='Print lexer tokens to stderr.',
default=False, action='store_true')
add('-v', '--version', action='version', version='%(prog)s ' + version)
add('-v', '--version', action='version', version=f'%(prog)s {version}')

ns = parser.parse_args(argv)
if ns.inputfile == '-':
Expand Down
30 changes: 17 additions & 13 deletions java2python/compiler/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, config):
def __getattr__(self, name):
try:
return partial(self.types[name], self.config)
except (KeyError, ):
except KeyError:
raise AttributeError('Factory missing "{0}" type.'.format(name))


Expand All @@ -67,10 +67,10 @@ class FactoryTypeDetector(type):
type names.

"""
def __init__(cls, name, bases, namespace):
def __init__(self, name, bases, namespace):
try:
Factory.types[cls.factoryName] = cls
except (AttributeError, ):
Factory.types[self.factoryName] = self
except AttributeError:
pass


Expand Down Expand Up @@ -156,7 +156,7 @@ def altIdent(self, name):
if name in klass.variables:
try:
method = self.parents(lambda v:v.isMethod).next()
except (StopIteration, ):
except StopIteration:
return name
if name in [p['name'] for p in method.parameters]:
return name
Expand Down Expand Up @@ -221,7 +221,7 @@ def isStatic(self):
@property
def isVoid(self):
""" True if this item is void. """
return 'void' == self.type
return self.type == 'void'

def iterPrologue(self):
""" Yields the items in the prologue of this template. """
Expand Down Expand Up @@ -260,8 +260,7 @@ def find(self, pred=lambda v:True):
if pred(child):
yield child
if hasattr(child, 'find'):
for value in child.find(pred):
yield value
yield from child.find(pred)

@property
def className(self):
Expand All @@ -276,8 +275,8 @@ def typeName(self):
def toExpr(self, value):
""" Returns an expression for the given value if it is a string. """
try:
return self.factory.expr(left=value+'')
except (TypeError, ):
return self.factory.expr(left=f'{value}')
except TypeError:
return value

def toIter(self, value):
Expand Down Expand Up @@ -341,7 +340,7 @@ def isComment(self):
""" True if this expression is a comment. """
try:
return self.left.strip().startswith('#')
except (AttributeError, ):
except AttributeError:
return False


Expand All @@ -352,8 +351,13 @@ class Comment(Expression):

def __repr__(self):
""" Returns the debug string representation of this comment. """
parts = [colors.white(self.typeName+':'),
colors.black(self.left) + colors.black(self.right) + colors.black(self.tail), ]
parts = [
colors.white(f'{self.typeName}:'),
colors.black(self.left)
+ colors.black(self.right)
+ colors.black(self.tail),
]

return ' '.join(parts)


Expand Down
Loading