X Tutup
Skip to content

Commit 2853dfb

Browse files
author
Troy Melhase
committed
Inching towards release.
1 parent 646fe5f commit 2853dfb

File tree

12 files changed

+195
-56
lines changed

12 files changed

+195
-56
lines changed

bin/j2py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
""" j2py -> Java to Python compiler script.
34
5+
This is all very ordinary. We import the package bits, open and read
6+
a file, translate it, and write it out.
7+
8+
"""
49
import sys
510
from collections import defaultdict
611
from logging import _levelNames as logLevels, exception, warning, info, basicConfig
@@ -17,15 +22,18 @@ version = '0.5'
1722

1823

1924
def isWindows():
25+
""" True if running on Windows. """
2026
return sys.platform.startswith('win')
2127

2228

2329
def badLogLevel(name, value):
30+
""" Raise an error indicating a bad log level. """
2431
msg = 'option %s: invalid loglevel: %r'
2532
raise OptionValueError(msg % (name, value))
2633

2734

2835
def checkLogLevel(option, opt, value):
36+
""" Option type checker (see LocalOption class) to verify a log level. """
2937
try:
3038
lvl = int(value)
3139
except (ValueError, ):
@@ -40,12 +48,14 @@ def checkLogLevel(option, opt, value):
4048

4149

4250
class LocalOption(Option):
51+
""" Supplements the Option class with our log level checker. """
4352
TYPES = Option.TYPES + ('loglevel', )
4453
TYPE_CHECKER = Option.TYPE_CHECKER.copy()
4554
TYPE_CHECKER['loglevel'] = checkLogLevel
4655

4756

48-
def mainProfile(options):
57+
def profileMain(options):
58+
""" Runs our main function with profiling if indicated by options. """
4959
if options.profile:
5060
import cProfile, pstats
5161
prof = cProfile.Profile()
@@ -59,11 +69,13 @@ def mainProfile(options):
5969

6070

6171
def configFromDir(inname, dirname):
72+
""" Returns a file name from the given config directory. """
6273
name = path.join(dirname, path.basename(path.splitext(inname)[0]))
6374
return '%s.py' % path.abspath(name)
6475

6576

6677
def main(options):
78+
""" Compile the indicated java source with the given options. """
6779
timed = defaultdict(time)
6880
timed['overall']
6981

@@ -155,6 +167,7 @@ def main(options):
155167

156168

157169
def config(argv):
170+
""" Return an options object from the given argument sequence. """
158171
parser = OptionParser(option_class=LocalOption, version='%prog '+version)
159172
addopt = parser.add_option
160173
addopt('-i', '--input', dest='inputfile',
@@ -215,4 +228,4 @@ def config(argv):
215228

216229

217230
if __name__ == '__main__':
218-
sys.exit(mainProfile(config(sys.argv)))
231+
sys.exit(profileMain(config(sys.argv)))

doc/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# If extensions (or modules to document with autodoc) are in another directory,
1717
# add these directories to sys.path here. If the directory is relative to the
1818
# documentation root, use os.path.abspath to make it absolute, like shown here.
19-
#sys.path.insert(0, os.path.abspath('.'))
19+
sys.path.insert(0, os.path.abspath('../'))
2020

2121
# -- General configuration -----------------------------------------------------
2222

@@ -25,7 +25,7 @@
2525

2626
# Add any Sphinx extension module names here, as strings. They can be extensions
2727
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
28-
extensions = ['sphinx.ext.todo', 'sphinx.ext.viewcode']
28+
extensions = ['sphinx.ext.todo', 'sphinx.ext.viewcode', 'sphinx.ext.autodoc', ]
2929

3030
# Add any paths that contain templates here, relative to this directory.
3131
templates_path = ['_templates']
@@ -41,7 +41,7 @@
4141

4242
# General information about the project.
4343
project = u'java2python'
44-
copyright = u'2010, Troy Melhase'
44+
copyright = u'2011, Troy Melhase'
4545

4646
# The version info for the project you're documenting, acts as replacement for
4747
# |version| and |release|, also used in various other places throughout the

doc/glossary.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.. _glossary:
2+
3+
********
4+
Glossary
5+
********
6+
7+
8+
.. glossary::
9+
10+
ANTLR
11+
`ANother Tool for Language Recognition`_, the language tool used by
12+
this package to parse Java source code.
13+
14+
AST
15+
Abstract Syntax Tree
16+
17+
Block
18+
A class that combines an AST visitor and template generator.
19+
20+
Template
21+
A class that can serialize a block to Python source.
22+
23+
Visitor
24+
A class that accepts AST nodes and generates code via templating.
25+
26+
.. _ANother Tool for Language Recognition: http://www.antlr.org/

doc/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Contents:
2525
usage.rst
2626
features.rst
2727
customization.rst
28+
glossary.rst
2829

2930

3031
Indices and tables
@@ -33,4 +34,5 @@ Indices and tables
3334
* :ref:`genindex`
3435
* :ref:`modindex`
3536
* :ref:`search`
37+
* :ref:`glossary`
3638

doc/intro.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ What it Does
1010
|j2py| reads the Java source files you give it and produces
1111
somewhat-roughly-equivalent Python source code. It tries to make the
1212
same decisions you would if you were porting the code manually. It
13-
can perform the translation faster and more accuratly than you could,
13+
can perform the translation faster and more accurately than you could,
1414
because it's a dumb machine that does what its told and you're a smart
1515
person with lots of books you haven't read and a love of chocolate so
1616
sometimes you're easily distracted and make mistakes. Like me and
@@ -41,8 +41,8 @@ How it Works
4141

4242
|j2py| first converts the source code you give it into an abstract
4343
syntax tree. (That's a lie, really. |j2py| doesn't do this step,
44-
Antlr does this step, and Antlr is a whole lot bigger and cooler than
45-
|j2py| could ever be. Obviously, really smart people worked on Antlr
44+
ANTLR does this step, and ANTLR is a whole lot bigger and cooler than
45+
|j2py| could ever be. Obviously, really smart people worked on ANTLR
4646
and only one fairly dim one worked on |j2py|).
4747

4848
After the syntax tree is constructed, it's walked and its nodes are
@@ -63,16 +63,16 @@ of Java source code that you can't make into nice and neat and obvious
6363
Python equivalents.
6464

6565
To get around these trouble spots, |j2py| takes one of two approaches
66-
(and sometimes both if she's feeling especially fiesty or if you
66+
(and sometimes both if she's feeling especially feisty or if you
6767
haven't paid her much attention lately). The first approach is to try
6868
and make the problem go away. For example, in Java the `if` statement
69-
can contain an assigment expression::
69+
can contain an assignment expression::
7070

7171
if (++x == 0) { ... }
7272

73-
There isn't a single statement equivalent in Python because assigments
73+
There isn't a single statement equivalent in Python because assignments
7474
are statements there, not expressions. So |j2py| does what it can,
75-
presuably what you would do::
75+
presumably what you would do::
7676

7777
x += 1
7878
if x == 0:
@@ -82,7 +82,7 @@ Careful readers will have spotted just how close we came to driving
8282
over a cliff with that `++x` expression. If the increment had been
8383
done on the other side of the variable, the meaning of the statement
8484
would have changed and the Python code would have been wrong.
85-
Fortuatly, I've driven by lots of cliffs and have been scared by all
85+
Fortunately, I've driven by lots of cliffs and have been scared by all
8686
of them so I thought of this ahead of time and decided to do something
8787
about it::
8888

@@ -96,7 +96,7 @@ will get translated to::
9696
...
9797

9898
See what |j2py| did there? It tried to do what you would do. For
99-
further explaination and enumeration see the :ref:`features` chapter.
99+
further explanation and enumeration see the :ref:`features` chapter.
100100

101101

102102
Why Bother?

java2python/compiler/template.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ def sprinkleBlanks(body):
426426
yield blank
427427
yield item
428428
prev = item
429+
for handler in self.configHandlers('PostWalk'):
430+
handler(self)
429431
head = any(self.iterHead())
430432
body = list(super(Class, self).iterBody())
431433
tail = () if (body or head) else [self.factory.expr(left='pass')]

java2python/config/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
""" java2python.config -> subpackage for run-time configuration. """
4+
35
from functools import reduce
46
from imp import load_source
57
from os import path
68

79

810
class Config(object):
9-
""" Config -> wraps multiple configuration modules
11+
""" Config -> wraps multiple configuration modules """
1012

11-
"""
1213
def __init__(self, names):
1314
self.configs = [self.load(name) for name in names]
1415

1516
def every(self, key, default=None):
17+
""" Returns the value at the given key from each config module. """
1618
return [getattr(config, key, default) for config in self.configs]
1719

1820
def last(self, key, default=None):
21+
""" Returns the value at the given key from the last config module to define it. """
1922
for config in reversed(self.configs):
2023
if hasattr(config, key):
2124
return getattr(config, key)
2225
return default
2326

2427
@staticmethod
2528
def load(name):
26-
""" import and return a module from dotted form or filename. """
29+
""" Imports and returns a module from dotted form or filename. """
2730
if path.exists(name):
2831
mod = load_source(str(hash(name)), name)
2932
else:

java2python/config/default.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
# These generators are called after a class has been completely
5353
# generated; this specific one sorts method bodies by name.
5454
# NB: the code generator doesn't actually use this.
55-
classPostWalkMutators = [
55+
classPostWalkHandlers = [
5656
basic.classContentSort,
5757
]
5858

0 commit comments

Comments
 (0)
X Tutup