Skip to content

Commit e527355

Browse files
author
Troy Melhase
committed
Comment and docstring cleanup.
1 parent 4527b9f commit e527355

File tree

15 files changed

+138
-164
lines changed

15 files changed

+138
-164
lines changed

java2python/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
""" java2python -> a package to compile java source code to python. """
4-
##
5-
# Top-level package marker for java2python.
6-
#
3+
# java2python -> top-level package marker.
4+
5+

java2python/compiler/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
""" java2python.compiler package marker. """
4-
##
3+
# java2python.compiler package marker.
4+
#
55
# This module provides a simpler facade over the rest of the compiler
66
# subpackage. Client code should use the values in this module
77
# instead of using directly referencing items within the subpackage.

java2python/compiler/block.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
""" java2python.compiler.block -> Visitors combined with templates. """
4-
##
3+
# java2python.compiler.block -> Visitors combined with templates.
4+
#
55
# This module defines classes which combine AST walking with source
66
# generation. We've put these two behaviors into separate modules,
77
# java2python.compiler.template for creating source code, and
@@ -10,32 +10,29 @@
1010
# Each of the classes depends on the behavior of its counterpart.
1111
# This means they're very tightly coupled and that the classes are not
1212
# very reusable. The module split does allow for grouping of related
13-
# methods and does hide some of the cluttered code.
13+
# methods and does hide the cluttered code.
1414

1515
from sys import modules
1616
from java2python.compiler import template, visitor
1717

1818

19-
def newType(className, factoryTypeName):
20-
""" Creates a class derived from template.className and visitor.className """
19+
def addTypeToModule((className, factoryName)):
20+
""" Constructs and adds a new type to this module. """
2121
bases = (getattr(template, className), getattr(visitor, className))
22-
return type(className, bases, dict(factoryTypeName=factoryTypeName))
23-
24-
25-
def addTypeToModule((cn, ftn)):
26-
""" Adds a new type to this module. """
27-
setattr(modules[__name__], cn, newType(cn, ftn))
22+
newType = type(className, bases, dict(factoryName=factoryName))
23+
setattr(modules[__name__], className, newType)
2824

2925

3026
map(addTypeToModule, (
31-
('Annotation', 'at'),
32-
('Class', 'klass'),
33-
('Comment', 'comment'),
34-
('Enum', 'enum'),
35-
('Expression', 'expr'),
36-
('Interface', 'interface'),
37-
('Method', 'method'),
38-
('MethodContent', 'methodContent'),
39-
('Module', 'module'),
40-
('Statement', 'statement'),
41-
))
27+
('Annotation', 'at'),
28+
('Class', 'klass'),
29+
('Comment', 'comment'),
30+
('Enum', 'enum'),
31+
('Expression', 'expr'),
32+
('Interface', 'interface'),
33+
('Method', 'method'),
34+
('MethodContent', 'methodContent'),
35+
('Module', 'module'),
36+
('Statement', 'statement'),
37+
)
38+
)

java2python/compiler/template.py

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
""" java2python.compiler.template -> Base classes for writing Python source. """
4-
##
5-
# This module defines templates -- blocks of Python source code --
6-
# that can be easily manipulated and written. Each base provides
3+
# java2python.compiler.template -> Base classes for writing Python source.
4+
#
5+
# This module defines templates -- chunks of Python source code --
6+
# that can be easily manipulated and written. Each class provides
77
# string methods (__str__, dump, dumps) for serializing instances as a
8-
# source code string. The base types also provide many utility
9-
# methods.
8+
# source code string.
109
#
1110
# The Factory class is used to to provide runtime lookup of concrete
1211
# classes; this was necessary to accommodate splitting the behavior of
@@ -25,17 +24,17 @@
2524
class Factory(object):
2625
""" Factory -> creates pre-configured callables for new block instances.
2726
28-
The templates use an instance of this class as a quick and simple
29-
interface to create new templates like this:
27+
Both templates and visitors use an instance of this class as a simple
28+
interface to create new blocks like this:
3029
3130
stat = self.factory.statement()
3231
3332
The `__getattr__` method does the work of looking up and returning
34-
the appropriate template class. The lookup depends on the types
33+
the appropriate block class. The lookup depends on the types
3534
registry, which is populated by the FactoryTypeDetector metaclass
3635
below.
3736
38-
The important thing to realize regarding this factory is this:
37+
One important thing to realize regarding this factory is this:
3938
when an attribute is requested (`self.factory.expr` for example),
4039
the factory locates the type and returns a constructor for it with
4140
the config object pre-applied.
@@ -57,19 +56,20 @@ class FactoryTypeDetector(type):
5756
""" FactoryTypeDetector -> detects factory-creatable types as they are defined.
5857
5958
As subclasses are created they are checked for an attribute called
60-
`factoryTypeName`. If present, that key is used to populate the
61-
factory type registry above.
62-
63-
Note that the actual subclasses are not created here (none of
64-
these specify a `factoryTypeName`). Actual factory types are
65-
created in `java2python.compiler.block`. This is because we're
66-
after not templates, but visitors combined with templates, aka
67-
blocks. Refer to the `blocks` module for the specific factory
59+
`factoryName`. If present, that key is used to populate the
60+
type registry in the Factory class.
61+
62+
Note that the actual subclasses are not created here (templates and
63+
visitors do not specify a `factoryName`). Actual factory types are created
64+
in `java2python.compiler.block`. This is because we're after not
65+
templates or visitors, but rather visitors combined with templates (aka
66+
blocks). Refer to the `blocks` module for the specific factory
6867
type names.
68+
6969
"""
7070
def __init__(cls, name, bases, namespace):
7171
try:
72-
Factory.types[cls.factoryTypeName] = cls
72+
Factory.types[cls.factoryName] = cls
7373
except (AttributeError, ):
7474
pass
7575

@@ -95,7 +95,7 @@ class Base(object):
9595
* Configuration
9696
9797
This class provides utility methods for retrieving values from the
98-
run-time configuration. See the definition of `configHandler` and
98+
runtime configuration. See the definition of `configHandler` and
9999
`configHandlers` for details.
100100
101101
* Serialization
@@ -292,9 +292,8 @@ def wrapper(*a, **b):
292292

293293

294294
class Expression(Base):
295-
""" Expression -> formatting for Python expressions.
295+
""" Expression -> formatting for Python expressions. """
296296

297-
"""
298297
isExpression = True
299298

300299
def __init__(self, config, left='', right='', fs=FS.lr, parent=None, tail=''):
@@ -342,9 +341,8 @@ def isComment(self):
342341

343342

344343
class Comment(Expression):
345-
""" Comment -> formatting for Python comments.
344+
""" Comment -> formatting for Python comments. """
346345

347-
"""
348346
isComment = True
349347

350348
def __repr__(self):
@@ -356,9 +354,8 @@ def __repr__(self):
356354

357355

358356
class Statement(Base):
359-
""" Statement -> formatting for Python statements.
357+
""" Statement -> formatting for Python statements. """
360358

361-
"""
362359
isStatement = True
363360

364361
def __init__(self, config, keyword, fs=FS.lr, parent=None):
@@ -378,9 +375,7 @@ def iterPrologue(self):
378375

379376

380377
class Module(Base):
381-
""" Module -> formatting for Python modules.
382-
383-
"""
378+
""" Module -> formatting for Python modules. """
384379
isModule = True
385380

386381
def iterBody(self):
@@ -405,9 +400,7 @@ def iterPrologue(self):
405400

406401

407402
class Class(ClassMethodSharedMixin, Base):
408-
""" Class -> formatting for Python classes.
409-
410-
"""
403+
""" Class -> formatting for Python classes. """
411404
isClass = True
412405

413406
def iterBases(self):
@@ -444,26 +437,20 @@ def sprinkleBlanks(body):
444437

445438

446439
class Annotation(Class):
447-
""" Annotation -> formatting for annotations converted to Python classes.
448-
449-
"""
440+
""" Annotation -> formatting for annotations converted to Python classes. """
450441
isAnnotation = True
451442

452443
def __init__(self, config, name=None, type=None, parent=None):
453444
super(Annotation, self).__init__(config, name, type, parent)
454445

455446

456447
class Enum(Class):
457-
""" Enum -> formatting for enums converted to Python classes.
458-
459-
"""
448+
""" Enum -> formatting for enums converted to Python classes. """
460449
isEnum = True
461450

462451

463452
class Interface(Class):
464-
""" Interface -> formatting for interfaces converted to Python classes.
465-
466-
"""
453+
""" Interface -> formatting for interfaces converted to Python classes. """
467454
isInterface = True
468455

469456

@@ -472,9 +459,7 @@ class MethodContent(Base):
472459

473460

474461
class Method(ClassMethodSharedMixin, Base):
475-
""" Method -> formatting for Python methods.
476-
477-
"""
462+
""" Method -> formatting for Python methods. """
478463
isMethod = True
479464

480465
def __init__(self, config, name=None, type=None, parent=None):

0 commit comments

Comments
 (0)